Skip to content

Commit

Permalink
Support php 8.2 disjunctive normal form type
Browse files Browse the repository at this point in the history
  • Loading branch information
TysonAndre committed Aug 9, 2022
1 parent d373358 commit b646d90
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
Phan NEWS

??? ?? 2022, Phan 5.4.1 (dev)
-----------------------

New Features(Analysis):
- Support parsing php 8.2's disjunctive normal form types (e.g. `A|(B&C)` (https://wiki.php.net/rfc/dnf_types).

Aug 08 2022, Phan 5.4.0
-----------------------

Expand Down
4 changes: 3 additions & 1 deletion src/Phan/AST/UnionTypeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ public function visitTypeIntersection(Node $node): UnionType
}
$types[] = $this->visitName($c);
} else {
throw new AssertionError("Expected union type to be composed of types and names");
throw new AssertionError("Expected intersection type to be composed of types and names");
}
}
$result = [IntersectionType::createFromTypes($types, $this->code_base, $this->context)];
Expand Down Expand Up @@ -782,6 +782,8 @@ public function visitTypeUnion(Node $node): UnionType
}
}
$types[] = $this->visitName($c);
} elseif ($kind === ast\AST_TYPE_INTERSECTION) {
$types[] = $this->visitTypeIntersection($c);
} else {
throw new AssertionError("Expected union type to be composed of types and names");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Phan/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class CLI
/**
* This should be updated to x.y.z-dev after every release, and x.y.z before a release.
*/
public const PHAN_VERSION = '5.4.0';
public const PHAN_VERSION = '5.4.1-dev';

/**
* List of short flags passed to getopt
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%s:4 PhanPluginUnknownMethodReturnType Method \NS4\A::example() has no declared or inferred return type
%s:12 PhanDebugAnnotation @phan-debug-var requested for variable $param - it has union type (\NS4\B&\NS4\C)|\NS4\A(real=(\NS4\B&\NS4\C)|\NS4\A)
%s:14 PhanParamTooMany Call with 1 arg(s) to \NS4\A::example() which only takes 0 arg(s) defined at %s:4
%s:16 PhanTypeMismatchReturnSuperType Returning new B() of type \NS4\B but example() is declared to return (\NS4\B&\NS4\C)|\NS4\A (saw a supertype instead of a subtype)
%s:21 PhanTypeMismatchArgumentSuperType Argument 1 ($param) is new B() of type \NS4\B but \NS4\example() takes (\NS4\B&\NS4\C)|\NS4\A defined at %s:12 (expected type to be the same or a subtype, but saw a supertype instead)
%s:23 PhanTypeMismatchArgument Argument 1 ($param) is new \stdClass() of type \stdClass but \NS4\example() takes (\NS4\B&\NS4\C)|\NS4\A defined at %s:12
%s:24 PhanTypeMismatchArgumentReal Argument 1 ($param) is null of type null but \NS4\example() takes (\NS4\B&\NS4\C)|\NS4\A defined at %s:12
2 changes: 2 additions & 0 deletions tests/php82_files/expected/005_dnf_type_property.php.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%s:14 PhanTypeMismatchPropertyReal Assigning null of type null to property but \X5->value is (\Countable&\ArrayAccess)|\stdClass
%s:16 PhanTypeMismatchPropertyReal Assigning $v2 of type \X5 to property but \X5->value is (\Countable&\ArrayAccess)|\stdClass
24 changes: 24 additions & 0 deletions tests/php82_files/src/004_disjunctive_normal_form_param.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace NS4;
class A {
public function example() {
echo "done\n";
}
}
class B {
}
interface C {}
class D extends B implements C {}
function example(A|(B&C) $param): A|(B&C) {
'@phan-debug-var $param';
$param->example('extra');
if (random_int(0,1)) {
return new B();
}
return $param;
}
example(new A()); // valid
example(new B()); // invalid
example(new D()); // valid
example(new \stdClass()); // invalid
example(null); // invalid
16 changes: 16 additions & 0 deletions tests/php82_files/src/005_dnf_type_property.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
class X5 {
public stdClass|(Countable&ArrayAccess) $value;
}
class C5 implements Countable {
public function count() {
return 0;
}
}
$v = new X5();
$v2 = new X5();
$v->value = new C5(); // TODO: This should warn
$v2->value = new ArrayObject();
$v->value = null;
$v->value = $v2->value;
$v->value = $v2;

0 comments on commit b646d90

Please sign in to comment.