Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/PHPCfg/Op/Stmt/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Property extends Stmt implements AttributableOp

public ?Block $defaultBlock = null;

public Op\Type $declaredType;
public ?Op\Type $declaredType = null;

public function __construct(Operand $name, int $visiblity, bool $static, bool $readonly, bool $classConst, array $attrGroups, ?Op\Type $declaredType = null, ?Operand $defaultVar = null, ?Block $defaultBlock = null, array $attributes = [])
{
Expand Down
21 changes: 11 additions & 10 deletions lib/PHPCfg/Types/TypeReconstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ protected function resolveVarOp(Operand $var, Op $op, SplObjectStorage $resolved
if ($op->class->value === "static" || $op->class->value === "self") {
//todo change to static class
if (!$op->scope) {
var_dump($op);
throw new LogicException("Scope is null with a static call?");
}
$type = Helper::fromOpType($op->scope);
Expand All @@ -266,7 +265,7 @@ protected function resolveVarOp(Operand $var, Op $op, SplObjectStorage $resolved
// TODO: we may be able to determine these...
return null;
}
var_dump($op);

throw new LogicException("Unknown variable op found: " . $op->getType());
}

Expand Down Expand Up @@ -396,9 +395,9 @@ protected function resolveFuncCall(Operand $var, Operand\Literal $name, SplObjec
foreach ($this->state->functionLookup[$name] as $func) {
if ($func->returnType) {
$result[] = Helper::parseDecl($func->returnType->value);
} else {
} elseif ($func->getAttribute('doccomment')) {
// Check doc comment
$result[] = Helper::extractTypeFromComment("return", $func->getAttribute('doccomment'));
$result[] = Helper::parseComment(Helper::KIND_RETURN, $func->getAttribute('doccomment'));
}
}
return $result;
Expand Down Expand Up @@ -498,14 +497,15 @@ protected function resolveOp_Expr_ClassConstFetch(Operand $var, Op\Expr\ClassCon
if ($op->class instanceof Operand\Literal) {
$class = strtolower($op->class->value);
return $this->resolveClassConstant($class, $op, $resolved);
} elseif ($resolved->contains($op->class)) {
} elseif ($resolved->offsetExists($op->class)) {
$type = $resolved[$op->class];
if ($type->type !== Type::TYPE_OBJECT || empty($type->userType)) {
// give up
return null;
}
return $this->resolveClassConstant(strtolower($type->userType), $op, $resolved);
}

return null;
}

Expand Down Expand Up @@ -567,9 +567,9 @@ protected function resolveAllProperties(): void
foreach ($class->stmts->children as $stmt) {
if ($stmt instanceof Op\Stmt\Property) {
if ($stmt->declaredType) {
$stmt->type = Helper::parseDecl($stmt->declaredType->name);
} else {
$stmt->type = Helper::extractTypeFromComment("var", $stmt->getAttribute('doccomment'));
$stmt->type = Helper::fromOpType($stmt->declaredType);
} elseif ($stmt->getAttribute('doccomment')) {
$stmt->type = Helper::parseComment(Helper::KIND_VAR, $stmt->getAttribute('doccomment'));
}
}
}
Expand All @@ -592,8 +592,8 @@ protected function resolveClassConstant($class, $op, SplObjectStorage $resolved)
}
} elseif (isset($this->state->constants[$try])) {
foreach ($this->state->constants[$try] as $const) {
if ($resolved->offsetExists($const->value)) {
$types[] = $resolved[$const->value];
if ($resolved->offsetExists($const->defaultVar)) {
$types[] = $resolved[$const->defaultVar];
} else {
// Not every constant is computed yet
return null;
Expand All @@ -616,6 +616,7 @@ protected function resolveClassConstant($class, $op, SplObjectStorage $resolved)
}
}
}

return $types;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/PHPCfg/Visitor/DeclarationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function enterOp(Op $op, Block $block): void
$this->methods[] = $op;
} elseif ($op instanceof Op\Stmt\Function_) {
$this->functions[] = $op;
} elseif ($op instanceof Op\Terminal\Const_) {
} elseif ($op instanceof Op\Stmt\Property && $op->isClassConst()) {
$name = $op->name->value;
if ($op->scope) {
$name = strtolower($op->scope->name) . '::' . $name;
Expand Down
2 changes: 1 addition & 1 deletion test/CodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testTypeReconstruction($code, $expectedDump, $file)
$result = $e->getMessage();
}

// file_put_contents($file, $code . "\n-----" . $result . "\n");
// file_put_contents($file, CodeTest::canonicalize($code) . "\n-----\n" . CodeTest::canonicalize($result) . "\n");

$this->assertEquals(
$this->canonicalize($expectedDump),
Expand Down
2 changes: 0 additions & 2 deletions test/type_reconstruction/basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

$x = 1;
$y = 2;

-----
Block#1
Expr_Assign
Expand All @@ -14,4 +13,3 @@ Block#1
expr: LITERAL<int>(2)
result: TEMP<int>(#4)
Terminal_Return

2 changes: 0 additions & 2 deletions test/type_reconstruction/bitwise_not.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

~1;
~"";

-----
Block#1
Expr_BitwiseNot
Expand All @@ -12,4 +11,3 @@ Block#1
expr: LITERAL<string>('')
result: TEMP<string>(#2)
Terminal_Return

2 changes: 0 additions & 2 deletions test/type_reconstruction/class.test
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class A {
return floor($def);
}
}

-----
Block#1
Stmt_Class
Expand Down Expand Up @@ -51,4 +50,3 @@ Block#1
result: TEMP<int>(#2)
Terminal_Return
expr: TEMP<int>(#2)

20 changes: 10 additions & 10 deletions test/type_reconstruction/class_const.test
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ interface C {
}

class D extends B implements C {}



-----
Block#1
Stmt_Class
Expand All @@ -57,10 +54,12 @@ Block#1
Terminal_Return

Block#2
Terminal_Const
Stmt_Property
declaredType: mixed
flags: const|public
name: LITERAL<string>('FOO')
value: LITERAL<int>(1)
valueBlock: Block#6
defaultVar: LITERAL<int>(1)
defaultBlock: Block#6
Stmt_ClassMethod
name: foo
flags: public
Expand All @@ -83,10 +82,12 @@ Block#2
Block#3

Block#4
Terminal_Const
Stmt_Property
declaredType: mixed
flags: const|public
name: LITERAL<string>('BAR')
value: LITERAL<int>(1)
valueBlock: Block#7
defaultVar: LITERAL<int>(1)
defaultBlock: Block#7

Block#5

Expand Down Expand Up @@ -147,4 +148,3 @@ Block#1
result: TEMP<int>(#1)
Terminal_Return
expr: TEMP<int>(#1)

3 changes: 0 additions & 3 deletions test/type_reconstruction/class_union.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
if ($a instanceof RuntimeException || $a instanceof LogicException) {
echo $a->getMessage();
}


-----
Block#1
Expr_InstanceOf
Expand Down Expand Up @@ -87,4 +85,3 @@ Block#7
Parent: Block#5
Parent: Block#6
Terminal_Return

4 changes: 1 addition & 3 deletions test/type_reconstruction/exceptions.test
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function foo(Bar $bar)
echo "1";
}
return 8;

} catch (Error|RuntimeException $e) {
echo $e->getMessage();
return 8;
Expand All @@ -20,7 +20,6 @@ function foo(Bar $bar)
return 10;
}
}

-----
Block#1
Stmt_Function
Expand Down Expand Up @@ -139,4 +138,3 @@ Block#8
finallyTarget: Block#5
Terminal_Return
expr: LITERAL<int>(8)

2 changes: 0 additions & 2 deletions test/type_reconstruction/type_assert.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ function foo($a) {
}
echo $a;
}

-----
Block#1
Stmt_Function
Expand Down Expand Up @@ -58,4 +57,3 @@ Block#4
Terminal_Echo
expr: TEMP<string|null|bool|int|float|object|array|callable>(#5 <VARIABLE($a)>)
Terminal_Return

Loading