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
5 changes: 5 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ jobs:
echo "$OUTPUT"
../bashunit -a matches "Note: Using configuration file .+phpstan.neon." "$OUTPUT"
../bashunit -a contains 'Result cache not used because the metadata do not match: metaExtensions' "$OUTPUT"
- script: |
cd e2e/result-cache-restore-without-reflection
composer install
../../bin/phpstan -vvv
../../bin/phpstan -vvv
- script: |
cd e2e/bug-12606
export CONFIGTEST=test
Expand Down
2 changes: 2 additions & 0 deletions e2e/result-cache-restore-without-reflection/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
composer.lock
24 changes: 24 additions & 0 deletions e2e/result-cache-restore-without-reflection/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"require-dev": {
"phpstan/phpstan-symfony": "@dev",
"phpstan/extension-installer": "^1.4",
"phpstan/phpstan-doctrine": "@dev",
"phpstan/phpstan-beberlei-assert": "@dev",
"phpstan/phpstan-phpunit": "@dev",
"phpstan/phpstan-webmozart-assert": "@dev",
"phpstan/phpstan-mockery": "@dev",
"phpstan/phpstan-nette": "@dev",
"phpstan/phpstan-dibi": "@dev",
"php-standard-library/phpstan-extension": "@dev"
},
"autoload": {
"classmap": [
"lib/"
]
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace E2EFixture;

use LogicException;
use PHPStan\BetterReflection\Identifier\Identifier;
use PHPStan\BetterReflection\Identifier\IdentifierType;
use PHPStan\BetterReflection\Reflection\Reflection;
use PHPStan\BetterReflection\Reflector\Reflector;
use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator;

final class ThrowingSourceLocator implements SourceLocator
{

public function locateIdentifier(Reflector $reflector, Identifier $identifier): ?Reflection
{
throw new LogicException('SourceLocator::locateIdentifier must not be called during result cache construction');
}

public function locateIdentifiersByType(Reflector $reflector, IdentifierType $identifierType): array
{
throw new LogicException('SourceLocator::locateIdentifiersByType must not be called during result cache construction');
}

}
10 changes: 10 additions & 0 deletions e2e/result-cache-restore-without-reflection/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
parameters:
level: 8
paths:
- src

services:
betterReflectionSourceLocator:
class: PHPStan\BetterReflection\SourceLocator\Type\SourceLocator
factory: E2EFixture\ThrowingSourceLocator
autowired: false
4 changes: 4 additions & 0 deletions e2e/result-cache-restore-without-reflection/src/foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php

echo "";

1 change: 1 addition & 0 deletions src/Node/Expr/AlwaysRememberedExpr.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPStan\Node\VirtualNode;
use PHPStan\Type\Type;

/** Wraps an expression so its type is always remembered in the scope, bypassing impurity checks. */
final class AlwaysRememberedExpr extends Expr implements VirtualNode
{

Expand Down
9 changes: 6 additions & 3 deletions src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\AlwaysRememberedExpr;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BooleanType;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
Expand Down Expand Up @@ -47,10 +49,11 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$args = $node->getArgs();
$argType = $scope->getType($args[0]->value);
if ($argType instanceof ConstantStringType) {
$funcCall = new FuncCall(new FullyQualified('class_exists'), [
new Arg(new String_(ltrim($argType->getValue(), '\\'))),
]);
return $this->typeSpecifier->create(
new FuncCall(new FullyQualified('class_exists'), [
new Arg(new String_(ltrim($argType->getValue(), '\\'))),
]),
new AlwaysRememberedExpr($funcCall, new BooleanType(), new BooleanType()),
new ConstantBooleanType(true),
$context,
$scope,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,50 @@ function test(): void
assertType('int', impure());
}
}

function testClassExistsRemembered(): void
{
if (\class_exists('Bug8579RememberedA')) {
assertType('true', \class_exists('Bug8579RememberedA'));
} else {
assertType('bool', \class_exists('Bug8579RememberedA'));
}

assertType('bool', \class_exists('Bug8579RememberedA'));
}

function testClassExistsFalseNotRemembered(): void
{
if (!\class_exists('Bug8579FalseNotRememberedA')) {
assertType('bool', \class_exists('Bug8579FalseNotRememberedA'));
}

assertType('bool', \class_exists('Bug8579FalseNotRememberedA'));
}

function testInterfaceExistsFalseNotRemembered(): void
{
if (!\interface_exists('Bug8579FalseNotRememberedC')) {
assertType('bool', \interface_exists('Bug8579FalseNotRememberedC'));
}

assertType('bool', \interface_exists('Bug8579FalseNotRememberedC'));
}

function testTraitExistsFalseNotRemembered(): void
{
if (!\trait_exists('Bug8579FalseNotRememberedD')) {
assertType('bool', \trait_exists('Bug8579FalseNotRememberedD'));
}

assertType('bool', \trait_exists('Bug8579FalseNotRememberedD'));
}

function testEnumExistsFalseNotRemembered(): void
{
if (!\enum_exists('Bug8579FalseNotRememberedE')) {
assertType('bool', \enum_exists('Bug8579FalseNotRememberedE'));
}

assertType('bool', \enum_exists('Bug8579FalseNotRememberedE'));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Classes;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use function array_merge;

/**
* @extends RuleTestCase<InstantiationRule>
*/
class InstantiationDoNotRememberPossiblyImpureValuesRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return self::getContainer()->getByType(InstantiationRule::class);
}

public function testBug8579(): void
{
$this->analyse([__DIR__ . '/data/bug-8579.php'], []);
}

public static function getAdditionalConfigFiles(): array
{
return array_merge(
parent::getAdditionalConfigFiles(),
[
__DIR__ . '/doNotRememberPossiblyImpureValues.neon',
],
);
}

}
13 changes: 13 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-8579.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

if (!class_exists('NonexistentClassBug8579')) throw new \Exception('nonexistentclass');
$x = new \NonexistentClassBug8579();

if (!interface_exists('NonexistentInterfaceBug8579')) throw new \Exception('nonexistentinterface');
$x = new \NonexistentInterfaceBug8579();

if (!trait_exists('NonexistentTraitBug8579')) throw new \Exception('nonexistenttrait');
$x = new \NonexistentTraitBug8579();

if (!enum_exists('NonexistentEnumBug8579')) throw new \Exception('nonexistentenum');
$x = new \NonexistentEnumBug8579();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
parameters:
rememberPossiblyImpureFunctionValues: false
Loading