Skip to content

Commit

Permalink
Merge pull request phan#4535 from TysonAndre/upgrade-instruction
Browse files Browse the repository at this point in the history
Add upgrade instructions for php-ast, fix false positive in MoreSpecificElementTypePlugin
  • Loading branch information
TysonAndre committed Sep 8, 2021
2 parents 6518709 + 639498e commit 0fbd376
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .phan/plugins/MoreSpecificElementTypePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ private static function shouldWarnAboutMoreSpecificType(CodeBase $code_base, Uni
if ($declared_return_type->isStrictSubtypeOf($code_base, $actual_type)) {
return false;
}
if (!$actual_type->isStrictSubtypeOf($code_base, $declared_return_type)) {
return false;
}
if (!$actual_type->canCastToUnionType($declared_return_type, $code_base)) {
// Don't warn here about type mismatches such as int->string or object->array, but do warn about SubClass->BaseClass.
// Phan should warn elsewhere about those mismatches
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ New Features:
Plugins:
- Emit a proper warning when `InvokePHPNativeSyntaxCheckPlugin` is passed a path to a php binary that is missing or invalid (or if the syntax check crashed). (#4116)
Previously, Phan would crash with an error such as `fwrite(): write of 8196 bytes failed with errno=32 Broken pipe`
- Fix false positive `PhanPluginMoreSpecificActualReturnType` for phpdoc array shape return type and returned generic array. (#4531)

Bug fixes:
- Fix type inference logic that was looking for array specializations rather than array or any array subtype (#4512)
- Fix false positive `PhanUnreferencedClosure`/`PhanUnreferencedFunction` seen when a closure/function name was passed to a function such as `uasort` that already had a plugin analyzing calls of the closure. (#4090, #4519)

Maintenance:
- Fix old return type signature for `get_headers` (#3273)
- Print instructions on how to upgrade php-ast to 1.0.11+ if an outdated version is installed. (#4532)

Aug 26 2021, Phan 5.2.0
-----------------------
Expand Down
5 changes: 5 additions & 0 deletions src/Phan/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,11 @@ private static function exitIfAstVersionIsInvalid(): void
}
if (\version_compare($ast_version, '1.0.11') < 0) {
CLI::printWarningToStderr(sprintf("php-ast %s is being used with Phan 5. php-ast 1.0.11 or newer is recommended for compatibility with plugins and support for AST version 85.\n", $ast_version));
// Reuse PHAN_SUPPRESS_AST_DEPRECATION for this purpose as well.
if (!getenv('PHAN_SUPPRESS_AST_DEPRECATION')) {
\phan_output_ast_installation_instructions();
fwrite(STDERR, "(Set PHAN_SUPPRESS_AST_DEPRECATION=1 to suppress this message)" . PHP_EOL);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/005_more_specific.php:3 PhanReadOnlyPublicProperty Possibly zero write references to public property \TestSpecific->m
42 changes: 42 additions & 0 deletions tests/infer_missing_types_test/src/005_more_specific.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
class TestSpecific {
public $m=5;
public $validList=false;
/**
* @return array{s:?int,e:?int}
*/
public function item() {
$s=rand(0,$this->m);$e=rand(0,$this->m);
if($s==0)$s=null;
if($e==0)$e=null;
return ['s'=>$s,'e'=>$e];
}

/**
* @return array<array{s:?int,e:?int}>
*/
public function list() {
$result=[];
$l=[0,1,2,3,4,5]; // Array to use foreach to be close to project case
foreach($l as $_) {
$result[]=$this->item();
}
return $result;
}


/**
* @return array<array{s:?int,e:?int}>
*/
public function validList() {
$result=[];
if($this->validList) {
$result=$this->list();
}
return $result;
}
}
$t = new TestSpecific();
var_dump($t->validList());
$t->validList=true;
var_dump($t->validList());

0 comments on commit 0fbd376

Please sign in to comment.