Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support define() and defined() in local scope
- Loading branch information
1 parent
ffd57a3
commit a4e248e
Showing
11 changed files
with
213 additions
and
30 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
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
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,61 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Analyser\SpecifiedTypes; | ||
use PHPStan\Analyser\TypeSpecifier; | ||
use PHPStan\Analyser\TypeSpecifierAwareExtension; | ||
use PHPStan\Analyser\TypeSpecifierContext; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
|
||
class DefineConstantTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
{ | ||
|
||
/** @var TypeSpecifier */ | ||
private $typeSpecifier; | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
public function isFunctionSupported( | ||
FunctionReflection $functionReflection, | ||
FuncCall $node, | ||
TypeSpecifierContext $context | ||
): bool | ||
{ | ||
return $functionReflection->getName() === 'define' | ||
&& $context->null() | ||
&& count($node->args) >= 2; | ||
} | ||
|
||
public function specifyTypes( | ||
FunctionReflection $functionReflection, | ||
FuncCall $node, | ||
Scope $scope, | ||
TypeSpecifierContext $context | ||
): SpecifiedTypes | ||
{ | ||
$constantName = $scope->getType($node->args[0]->value); | ||
if ( | ||
!$constantName instanceof ConstantStringType | ||
|| $constantName->getValue() === '' | ||
) { | ||
return new SpecifiedTypes([], []); | ||
} | ||
|
||
return $this->typeSpecifier->create( | ||
new \PhpParser\Node\Expr\ConstFetch( | ||
new \PhpParser\Node\Name\FullyQualified($constantName->getValue()) | ||
), | ||
$scope->getType($node->args[1]->value), | ||
TypeSpecifierContext::createTruthy() | ||
); | ||
} | ||
|
||
} |
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,62 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Analyser\SpecifiedTypes; | ||
use PHPStan\Analyser\TypeSpecifier; | ||
use PHPStan\Analyser\TypeSpecifierAwareExtension; | ||
use PHPStan\Analyser\TypeSpecifierContext; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
use PHPStan\Type\FunctionTypeSpecifyingExtension; | ||
use PHPStan\Type\MixedType; | ||
|
||
class DefinedConstantTypeSpecifyingExtension implements FunctionTypeSpecifyingExtension, TypeSpecifierAwareExtension | ||
{ | ||
|
||
/** @var TypeSpecifier */ | ||
private $typeSpecifier; | ||
|
||
public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void | ||
{ | ||
$this->typeSpecifier = $typeSpecifier; | ||
} | ||
|
||
public function isFunctionSupported( | ||
FunctionReflection $functionReflection, | ||
FuncCall $node, | ||
TypeSpecifierContext $context | ||
): bool | ||
{ | ||
return $functionReflection->getName() === 'defined' | ||
&& count($node->args) >= 1 | ||
&& !$context->null(); | ||
} | ||
|
||
public function specifyTypes( | ||
FunctionReflection $functionReflection, | ||
FuncCall $node, | ||
Scope $scope, | ||
TypeSpecifierContext $context | ||
): SpecifiedTypes | ||
{ | ||
$constantName = $scope->getType($node->args[0]->value); | ||
if ( | ||
!$constantName instanceof ConstantStringType | ||
|| $constantName->getValue() === '' | ||
) { | ||
return new SpecifiedTypes([], []); | ||
} | ||
|
||
return $this->typeSpecifier->create( | ||
new \PhpParser\Node\Expr\ConstFetch( | ||
new \PhpParser\Node\Name\FullyQualified($constantName->getValue()) | ||
), | ||
new MixedType(), | ||
$context | ||
); | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -4,4 +4,8 @@ | |
|
||
$foo = FOO_CONSTANT; | ||
|
||
die; | ||
define('BAR_CONSTANT', 'bar'); | ||
|
||
if (defined('BAZ_CONSTANT')) { | ||
die; | ||
} |
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
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