-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace highlight_string() stub with a return type extension
- Loading branch information
1 parent
f670288
commit bb4fdfc
Showing
6 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/Type/Php/HighlightStringDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\BooleanType; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use function count; | ||
|
||
final class HighlightStringDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function __construct(private PhpVersion $phpVersion) | ||
{ | ||
} | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'highlight_string'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
$args = $functionCall->getArgs(); | ||
if (count($args) < 2) { | ||
if ($this->phpVersion->highlightStringDoesNotReturnFalse()) { | ||
return new ConstantBooleanType(true); | ||
} | ||
|
||
return new BooleanType(); | ||
} | ||
|
||
$returnType = $scope->getType($args[1]->value); | ||
if ($returnType->isTrue()->yes()) { | ||
return new StringType(); | ||
} | ||
|
||
if ($this->phpVersion->highlightStringDoesNotReturnFalse()) { | ||
return new ConstantBooleanType(true); | ||
} | ||
|
||
return new BooleanType(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php // lint >= 8.4 | ||
|
||
namespace Pr1244Php84; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
function foo() { | ||
/** @var string $string */ | ||
$string = doFoo(); | ||
|
||
assertType('null', var_export()); | ||
assertType('null', var_export($string)); | ||
assertType('null', var_export($string, false)); | ||
assertType('string', var_export($string, true)); | ||
|
||
assertType('true', highlight_string()); | ||
assertType('true', highlight_string($string)); | ||
assertType('true', highlight_string($string, false)); | ||
assertType('string', highlight_string($string, true)); | ||
|
||
assertType('bool', highlight_file()); | ||
assertType('bool', highlight_file($string)); | ||
assertType('bool', highlight_file($string, false)); | ||
assertType('string', highlight_file($string, true)); | ||
|
||
assertType('bool', show_source()); | ||
assertType('bool', show_source($string)); | ||
assertType('bool', show_source($string, false)); | ||
assertType('string', show_source($string, true)); | ||
|
||
assertType('true', print_r()); | ||
assertType('true', print_r($string)); | ||
assertType('true', print_r($string, false)); | ||
assertType('string', print_r($string, true)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?php | ||
<?php // lint < 8.4 | ||
|
||
namespace Pr1244; | ||
|
||
|