diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index 2be9b30b325..8b4befaff58 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -338,6 +338,10 @@ jobs: ../../bin/phpstan clear-result-cache ../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-baseline.neon" ../bashunit -a exit_code "0" "../../bin/phpstan analyse --error-format=raw -c with-baseline.neon" + - script: | + cd e2e/bug-12585 + composer install + ../../bin/phpstan - script: | cd e2e/result-cache-meta-extension composer install @@ -634,6 +638,10 @@ jobs: echo "FAIL: tmp was created by a worker, meaning sys_temp_dir='tmp~1' was incorrectly evaluated to 'tmp'" exit 1 fi + - script: | + cd e2e/parameter-type-extension + composer install + ../../bin/phpstan analyze steps: - name: Harden the runner (Audit all outbound calls) diff --git a/e2e/bug-12585/.gitignore b/e2e/bug-12585/.gitignore new file mode 100644 index 00000000000..8b7ef350326 --- /dev/null +++ b/e2e/bug-12585/.gitignore @@ -0,0 +1,2 @@ +/vendor +composer.lock diff --git a/e2e/bug-12585/composer.json b/e2e/bug-12585/composer.json new file mode 100644 index 00000000000..a072011fe86 --- /dev/null +++ b/e2e/bug-12585/composer.json @@ -0,0 +1,5 @@ +{ + "autoload-dev": { + "classmap": ["src/"] + } +} diff --git a/e2e/bug-12585/phpstan.neon b/e2e/bug-12585/phpstan.neon new file mode 100644 index 00000000000..a463d651063 --- /dev/null +++ b/e2e/bug-12585/phpstan.neon @@ -0,0 +1,10 @@ +parameters: + level: 8 + paths: + - src + +services: + - + class: Bug12585\EloquentBuilderRelationParameterExtension + tags: + - phpstan.dynamicMethodParameterTypeExtension diff --git a/e2e/bug-12585/src/extension.php b/e2e/bug-12585/src/extension.php new file mode 100644 index 00000000000..285e732b1a3 --- /dev/null +++ b/e2e/bug-12585/src/extension.php @@ -0,0 +1,255 @@ + */ + private array $methods = ['whereHas', 'withWhereHas']; + + public function __construct(private ReflectionProvider $reflectionProvider) + { + } + + public function isMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool + { + if (! $methodReflection->getDeclaringClass()->is(Builder::class)) { + return false; + } + + if (! in_array($methodReflection->getName(), $this->methods, strict: true)) { + return false; + } + + return $parameter->getName() === 'callback'; + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, ParameterReflection $parameter, Scope $scope): Type|null + { + $method = $methodReflection->getName(); + $relations = $this->getRelationsFromMethodCall($methodCall, $scope); + $models = $this->getModelsFromRelations($relations); + + if (count($models) === 0) { + return null; + } + + $type = $this->getBuilderTypeForModels($models); + + if ($method === 'withWhereHas') { + $type = TypeCombinator::union($type, ...$relations); + } + + return new ClosureType([new ClosureQueryParameter('query', $type)], new MixedType(), false); + } + + /** + * @param array $relations + * @return array + */ + private function getModelsFromRelations(array $relations): array + { + $models = []; + + foreach ($relations as $relation) { + $classNames = $relation->getTemplateType(Relation::class, 'TRelatedModel')->getObjectClassNames(); + foreach ($classNames as $className) { + $models[] = $className; + } + } + + return $models; + } + + /** @return array */ + private function getRelationsFromMethodCall(MethodCall $methodCall, Scope $scope): array + { + $relationType = null; + + foreach ($methodCall->args as $arg) { + if ($arg instanceof VariadicPlaceholder) { + continue; + } + + if ($arg->name === null || $arg->name->toString() === 'relation') { + $relationType = $scope->getType($arg->value); + break; + } + } + + if ($relationType === null) { + return []; + } + + $calledOnModels = $scope->getType($methodCall->var) + ->getTemplateType(Builder::class, 'TModel') + ->getObjectClassNames(); + + $values = array_map(fn ($type) => $type->getValue(), $relationType->getConstantStrings()); + $relationTypes = [$relationType]; + + foreach ($values as $relation) { + $relationTypes = array_merge( + $relationTypes, + $this->getRelationTypeFromString($calledOnModels, explode('.', $relation), $scope) + ); + } + + return array_values(array_filter( + $relationTypes, + static fn ($r) => (new ObjectType(Relation::class))->isSuperTypeOf($r)->yes() + )); + } + + /** + * @param list $calledOnModels + * @param list $relationParts + * @return list + */ + private function getRelationTypeFromString(array $calledOnModels, array $relationParts, Scope $scope): array + { + $relations = []; + + while ($relationName = array_shift($relationParts)) { + $relations = []; + $relatedModels = []; + + foreach ($calledOnModels as $model) { + $modelType = new ObjectType($model); + + if (! $modelType->hasMethod($relationName)->yes()) { + continue; + } + + $relationType = $modelType->getMethod($relationName, $scope)->getVariants()[0]->getReturnType(); + + if (! (new ObjectType(Relation::class))->isSuperTypeOf($relationType)->yes()) { + continue; + } + + $relations[] = $relationType; + + array_push($relatedModels, ...$relationType->getTemplateType(Relation::class, 'TRelatedModel')->getObjectClassNames()); + } + + $calledOnModels = $relatedModels; + } + + return $relations; + } + + private function determineBuilderName(string $modelClassName): string + { + $method = $this->reflectionProvider->getClass($modelClassName)->getNativeMethod('query'); + + $returnType = $method->getVariants()[0]->getReturnType(); + + if (in_array(Builder::class, $returnType->getReferencedClasses(), true)) { + return Builder::class; + } + + $classNames = $returnType->getObjectClassNames(); + + if (count($classNames) === 1) { + return $classNames[0]; + } + + return $returnType->describe(VerbosityLevel::value()); + } + + /** + * @param array|string|TypeWithClassName $models + * @return ($models is array ? Type : ObjectType) + */ + private function getBuilderTypeForModels(array|string|TypeWithClassName $models): Type + { + $models = is_array($models) ? $models : [$models]; + $models = array_unique($models, SORT_REGULAR); + + $mappedModels = []; + foreach ($models as $model) { + if (is_string($model)) { + $mappedModels[$model] = new ObjectType($model); + } else { + $mappedModels[$model->getClassName()] = $model; + } + } + + $groupedByBuilder = []; + foreach ($mappedModels as $class => $type) { + $builderName = $this->determineBuilderName($class); + $groupedByBuilder[$builderName][] = $type; + } + + $builderTypes = []; + foreach ($groupedByBuilder as $builder => $models) { + $builderReflection = $this->reflectionProvider->getClass($builder); + + $builderTypes[] = $builderReflection->isGeneric() + ? new GenericObjectType($builder, [TypeCombinator::union(...$models)]) + : new ObjectType($builder); + } + + return TypeCombinator::union(...$builderTypes); + } +} + +final class ClosureQueryParameter implements ParameterReflection +{ + public function __construct(private string $name, private Type $type) + { + } + + public function getName(): string + { + return $this->name; + } + + public function isOptional(): bool + { + return false; + } + + public function getType(): Type + { + return $this->type; + } + + public function passedByReference(): PassedByReference + { + return PassedByReference::createNo(); + } + + public function isVariadic(): bool + { + return false; + } + + public function getDefaultValue(): Type|null + { + return null; + } +} diff --git a/e2e/bug-12585/src/test.php b/e2e/bug-12585/src/test.php new file mode 100644 index 00000000000..f665ac47c2c --- /dev/null +++ b/e2e/bug-12585/src/test.php @@ -0,0 +1,199 @@ + $related + * @return BelongsTo + */ + public function belongsTo(string $related): BelongsTo + { + return new BelongsTo(); // @phpstan-ignore return.type + } + + /** + * @template T of Model + * @param class-string $related + * @return HasMany + */ + public function hasMany(string $related): HasMany + { + return new HasMany(); // @phpstan-ignore return.type + } + + /** @return Builder */ + public static function query(): Builder + { + return new Builder(new static()); + } +} + +/** @template TModel of Model */ +class Builder +{ + /** @param TModel $model */ + final public function __construct(protected Model $model) + { + } + + /** + * @param (\Closure(static): mixed)|string $column + * @return $this + */ + public function where(Closure|string $column, mixed $value = null) + { + return $this; + } + + /** + * @template TRelatedModel of Model + * + * @param Relation|string $relation + * @param (\Closure(Builder): mixed)|null $callback + * @return $this + */ + public function whereHas($relation, ?Closure $callback = null) + { + return $this; + } + + /** + * @param string $relation + * @param (\Closure(Builder<*>|Relation<*, *>): mixed)|null $callback + * @return $this + */ + public function withWhereHas($relation, ?Closure $callback = null) + { + return $this; + } + + /** + * @template T of Model + * @param T $model + * @return self + */ + public static function create(Model $model): self + { + return new self($model); + } +} + +/** + * @template TRelatedModel of Model + * @template TDeclaringModel of Model + * @mixin Builder + */ +abstract class Relation +{ +} + +/** + * @template TRelatedModel of Model + * @template TDeclaringModel of Model + * @extends Relation + */ +class BelongsTo extends Relation +{ +} + +/** + * @template TRelatedModel of Model + * @template TDeclaringModel of Model + * @extends Relation + */ +class HasMany extends Relation +{ +} + +final class User extends Model +{ + /** @return HasMany */ + public function posts(): HasMany + { + return $this->hasMany(Post::class); + } + +} + +final class Post extends Model +{ + /** @return BelongsTo */ + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } + + public static function query(): PostBuilder + { + return new PostBuilder(new self()); + } +} + +/** @extends Builder */ +class PostBuilder extends Builder +{ +} + +function test(): void +{ + User::query()->whereHas('posts', function ($query) { + assertType('Bug12585\PostBuilder', $query); + }); + User::query()->whereHas('posts', function (Builder $query) { + return $query->where('name', 'test'); + }); + User::query()->whereHas('posts', function (PostBuilder $query) { + return $query->where('name', 'test'); + }); + User::query()->whereHas('posts', fn (Builder $q) => $q->where('name', 'test')); + User::query()->whereHas('posts', fn (PostBuilder $q) => $q->where('name', 'test')); + + Post::query()->whereHas('user', fn ($q) => $q->where('name', 'test')); + Post::query()->withWhereHas('user', function ($query) { + assertType('Bug12585\BelongsTo|Bug12585\Builder', $query); + }); + Post::query()->withWhereHas('user', fn (Builder|Relation $q) => $q->where('name', 'test')); + Post::query()->withWhereHas('user', fn (Builder|BelongsTo $q) => $q->where('name', 'test')); +} + +function testNullsafeMethodCall(): void +{ + $userOrNull = rand() ? User::query() : null; + $userOrNull?->whereHas('posts', function ($query) { + assertType('Bug12585\PostBuilder', $query); + }); +} + +function testNullOrObject(): void +{ + /** @var Builder|null $builderOrNull */ + $builderOrNull = null; + $builderOrNull?->whereHas('posts', function ($query) { + assertType('Bug12585\PostBuilder', $query); + }); +} + +function testNew(): void +{ + (new Builder(new User()))->whereHas('posts', function ($query) { + assertType('Bug12585\PostBuilder', $query); + }); +} + +function testStaticCall(): void +{ + Builder::create(new User())->whereHas('posts', function ($query) { + assertType('Bug12585\PostBuilder', $query); + }); +} diff --git a/e2e/parameter-type-extension/.gitignore b/e2e/parameter-type-extension/.gitignore new file mode 100644 index 00000000000..de4a392c331 --- /dev/null +++ b/e2e/parameter-type-extension/.gitignore @@ -0,0 +1,2 @@ +/vendor +/composer.lock diff --git a/e2e/parameter-type-extension/composer.json b/e2e/parameter-type-extension/composer.json new file mode 100644 index 00000000000..f8a4e6ebedf --- /dev/null +++ b/e2e/parameter-type-extension/composer.json @@ -0,0 +1,7 @@ +{ + "autoload": { + "psr-4": { + "App\\": "src/" + } + } +} diff --git a/e2e/parameter-type-extension/composer.lock b/e2e/parameter-type-extension/composer.lock new file mode 100644 index 00000000000..b383d88ac57 --- /dev/null +++ b/e2e/parameter-type-extension/composer.lock @@ -0,0 +1,18 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "d751713988987e9331980363e24189ce", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": {}, + "prefer-stable": false, + "prefer-lowest": false, + "platform": {}, + "platform-dev": {}, + "plugin-api-version": "2.6.0" +} diff --git a/e2e/parameter-type-extension/phpstan.neon.dist b/e2e/parameter-type-extension/phpstan.neon.dist new file mode 100644 index 00000000000..399cb981817 --- /dev/null +++ b/e2e/parameter-type-extension/phpstan.neon.dist @@ -0,0 +1,9 @@ +parameters: + level: 9 + paths: + - src +services: + - + class: App\ParameterTypeExtension + tags: + - phpstan.dynamicMethodParameterTypeExtension diff --git a/e2e/parameter-type-extension/src/ParameterTypeExtension.php b/e2e/parameter-type-extension/src/ParameterTypeExtension.php new file mode 100644 index 00000000000..d7ad9d149e1 --- /dev/null +++ b/e2e/parameter-type-extension/src/ParameterTypeExtension.php @@ -0,0 +1,110 @@ +getDeclaringClass()->is(Builder::class)) { + return false; + } + + return $methodReflection->getName() === 'with'; + } + + public function getTypeFromMethodCall( + MethodReflection $methodReflection, + MethodCall $methodCall, + ParameterReflection $parameter, + Scope $scope, + ): Type|null { + $arg = $methodCall->getArgs()[0] ?? null; + if (!$arg) { + return null; + } + + $type = $scope->getType($arg->value)->getConstantArrays()[0] ?? null; + if (!$type) { + return null; + } + + $model = $scope->getType($methodCall->var) + ->getTemplateType(Builder::class, 'TModel') + ->getObjectClassNames()[0] ?? null; + if (!$model) { + return null; + } + + foreach ($type->getKeyTypes() as $keyType) { + $relationType = $this->getRelationTypeFromModel($model, (string) $keyType->getValue(), $scope); + if (!$relationType) { + continue; + } + + $newType = new ClosureType([ + new class('test', $relationType) implements ParameterReflection { + public function __construct(private string $name, private Type $type) {} + public function getName(): string + { + return $this->name; + } + public function isOptional(): bool + { + return false; + } + public function getType(): Type + { + return $this->type; + } + public function passedByReference(): PassedByReference + { + return PassedByReference::createNo(); + } + public function isVariadic(): bool + { + return false; + } + public function getDefaultValue(): ?Type + { + return null; + } + }, + ], new MixedType(), false); + + $type = $type->setOffsetValueType($keyType, $newType, false); + } + + return $type; + } + + public function getRelationTypeFromModel(string $model, string $relation, Scope $scope): ?Type + { + $modelType = new ObjectType($model); + + if (! $modelType->hasMethod($relation)->yes()) { + return null; + } + + $relationType = $modelType->getMethod($relation, $scope)->getVariants()[0]->getReturnType(); + + if (! (new ObjectType(Relation::class))->isSuperTypeOf($relationType)->yes()) { + return null; + } + + return $relationType; + } +} diff --git a/e2e/parameter-type-extension/src/test.php b/e2e/parameter-type-extension/src/test.php new file mode 100644 index 00000000000..51fc3c2b635 --- /dev/null +++ b/e2e/parameter-type-extension/src/test.php @@ -0,0 +1,88 @@ + */ + public function car(): HasOne + { + return new HasOne(); // @phpstan-ignore return.type + } + + /** @return MorphTo */ + public function monitorable(): MorphTo + { + return new MorphTo(); // @phpstan-ignore return.type + } +} + +/** + * @template TRelatedModel of Model + * @template TDeclaringModel of Model + * @template TResult + */ +class Relation { + /** + * @param list $columns + * @return $this + */ + public function select(array $columns): static + { + return $this; + } +} + +/** + * @template TRelatedModel of Model + * @template TDeclaringModel of Model + * @extends Relation + */ +class HasOne extends Relation {} + +/** + * @template TRelatedModel of Model + * @template TDeclaringModel of Model + * @extends Relation + */ +class MorphTo extends Relation { + /** @return $this */ + public function morphWith(): static + { + return $this; + } +} + +/** @template TModel of Model */ +class Builder +{ + /** + * @param array): mixed> $relations + * @return $this + */ + public function with(array $relations): static + { + return $this; + } +} + +/** @param Builder $query */ +function test(Builder $query): void +{ + $query->with([ + 'car' => function ($r) { assertType('App\HasOne', $r); }, + 'monitorable' => function ($r) { assertType('App\MorphTo', $r); }, + ]); + $query->with([ + 'car' => fn (HasOne $q) => $q->select(['id']), + 'monitorable' => fn (MorphTo $q) => $q->morphWith(), + ]); +} diff --git a/e2e/parameter-type-extension/vendor/autoload.php b/e2e/parameter-type-extension/vendor/autoload.php new file mode 100644 index 00000000000..a49f76ab8e7 --- /dev/null +++ b/e2e/parameter-type-extension/vendor/autoload.php @@ -0,0 +1,22 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Autoload; + +/** + * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. + * + * $loader = new \Composer\Autoload\ClassLoader(); + * + * // register classes with namespaces + * $loader->add('Symfony\Component', __DIR__.'/component'); + * $loader->add('Symfony', __DIR__.'/framework'); + * + * // activate the autoloader + * $loader->register(); + * + * // to enable searching the include path (eg. for PEAR packages) + * $loader->setUseIncludePath(true); + * + * In this example, if you try to use a class in the Symfony\Component + * namespace or one of its children (Symfony\Component\Console for instance), + * the autoloader will first look for the class under the component/ + * directory, and it will then fallback to the framework/ directory if not + * found before giving up. + * + * This class is loosely based on the Symfony UniversalClassLoader. + * + * @author Fabien Potencier + * @author Jordi Boggiano + * @see https://www.php-fig.org/psr/psr-0/ + * @see https://www.php-fig.org/psr/psr-4/ + */ +class ClassLoader +{ + /** @var \Closure(string):void */ + private static $includeFile; + + /** @var string|null */ + private $vendorDir; + + // PSR-4 + /** + * @var array> + */ + private $prefixLengthsPsr4 = array(); + /** + * @var array> + */ + private $prefixDirsPsr4 = array(); + /** + * @var list + */ + private $fallbackDirsPsr4 = array(); + + // PSR-0 + /** + * List of PSR-0 prefixes + * + * Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2'))) + * + * @var array>> + */ + private $prefixesPsr0 = array(); + /** + * @var list + */ + private $fallbackDirsPsr0 = array(); + + /** @var bool */ + private $useIncludePath = false; + + /** + * @var array + */ + private $classMap = array(); + + /** @var bool */ + private $classMapAuthoritative = false; + + /** + * @var array + */ + private $missingClasses = array(); + + /** @var string|null */ + private $apcuPrefix; + + /** + * @var array + */ + private static $registeredLoaders = array(); + + /** + * @param string|null $vendorDir + */ + public function __construct($vendorDir = null) + { + $this->vendorDir = $vendorDir; + self::initializeIncludeClosure(); + } + + /** + * @return array> + */ + public function getPrefixes() + { + if (!empty($this->prefixesPsr0)) { + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); + } + + return array(); + } + + /** + * @return array> + */ + public function getPrefixesPsr4() + { + return $this->prefixDirsPsr4; + } + + /** + * @return list + */ + public function getFallbackDirs() + { + return $this->fallbackDirsPsr0; + } + + /** + * @return list + */ + public function getFallbackDirsPsr4() + { + return $this->fallbackDirsPsr4; + } + + /** + * @return array Array of classname => path + */ + public function getClassMap() + { + return $this->classMap; + } + + /** + * @param array $classMap Class to filename map + * + * @return void + */ + public function addClassMap(array $classMap) + { + if ($this->classMap) { + $this->classMap = array_merge($this->classMap, $classMap); + } else { + $this->classMap = $classMap; + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, either + * appending or prepending to the ones previously set for this prefix. + * + * @param string $prefix The prefix + * @param list|string $paths The PSR-0 root directories + * @param bool $prepend Whether to prepend the directories + * + * @return void + */ + public function add($prefix, $paths, $prepend = false) + { + $paths = (array) $paths; + if (!$prefix) { + if ($prepend) { + $this->fallbackDirsPsr0 = array_merge( + $paths, + $this->fallbackDirsPsr0 + ); + } else { + $this->fallbackDirsPsr0 = array_merge( + $this->fallbackDirsPsr0, + $paths + ); + } + + return; + } + + $first = $prefix[0]; + if (!isset($this->prefixesPsr0[$first][$prefix])) { + $this->prefixesPsr0[$first][$prefix] = $paths; + + return; + } + if ($prepend) { + $this->prefixesPsr0[$first][$prefix] = array_merge( + $paths, + $this->prefixesPsr0[$first][$prefix] + ); + } else { + $this->prefixesPsr0[$first][$prefix] = array_merge( + $this->prefixesPsr0[$first][$prefix], + $paths + ); + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, either + * appending or prepending to the ones previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param list|string $paths The PSR-4 base directories + * @param bool $prepend Whether to prepend the directories + * + * @throws \InvalidArgumentException + * + * @return void + */ + public function addPsr4($prefix, $paths, $prepend = false) + { + $paths = (array) $paths; + if (!$prefix) { + // Register directories for the root namespace. + if ($prepend) { + $this->fallbackDirsPsr4 = array_merge( + $paths, + $this->fallbackDirsPsr4 + ); + } else { + $this->fallbackDirsPsr4 = array_merge( + $this->fallbackDirsPsr4, + $paths + ); + } + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { + // Register directories for a new namespace. + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = $paths; + } elseif ($prepend) { + // Prepend directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + $paths, + $this->prefixDirsPsr4[$prefix] + ); + } else { + // Append directories for an already registered namespace. + $this->prefixDirsPsr4[$prefix] = array_merge( + $this->prefixDirsPsr4[$prefix], + $paths + ); + } + } + + /** + * Registers a set of PSR-0 directories for a given prefix, + * replacing any others previously set for this prefix. + * + * @param string $prefix The prefix + * @param list|string $paths The PSR-0 base directories + * + * @return void + */ + public function set($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr0 = (array) $paths; + } else { + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; + } + } + + /** + * Registers a set of PSR-4 directories for a given namespace, + * replacing any others previously set for this namespace. + * + * @param string $prefix The prefix/namespace, with trailing '\\' + * @param list|string $paths The PSR-4 base directories + * + * @throws \InvalidArgumentException + * + * @return void + */ + public function setPsr4($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirsPsr4 = (array) $paths; + } else { + $length = strlen($prefix); + if ('\\' !== $prefix[$length - 1]) { + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); + } + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; + $this->prefixDirsPsr4[$prefix] = (array) $paths; + } + } + + /** + * Turns on searching the include path for class files. + * + * @param bool $useIncludePath + * + * @return void + */ + public function setUseIncludePath($useIncludePath) + { + $this->useIncludePath = $useIncludePath; + } + + /** + * Can be used to check if the autoloader uses the include path to check + * for classes. + * + * @return bool + */ + public function getUseIncludePath() + { + return $this->useIncludePath; + } + + /** + * Turns off searching the prefix and fallback directories for classes + * that have not been registered with the class map. + * + * @param bool $classMapAuthoritative + * + * @return void + */ + public function setClassMapAuthoritative($classMapAuthoritative) + { + $this->classMapAuthoritative = $classMapAuthoritative; + } + + /** + * Should class lookup fail if not found in the current class map? + * + * @return bool + */ + public function isClassMapAuthoritative() + { + return $this->classMapAuthoritative; + } + + /** + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. + * + * @param string|null $apcuPrefix + * + * @return void + */ + public function setApcuPrefix($apcuPrefix) + { + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; + } + + /** + * The APCu prefix in use, or null if APCu caching is not enabled. + * + * @return string|null + */ + public function getApcuPrefix() + { + return $this->apcuPrefix; + } + + /** + * Registers this instance as an autoloader. + * + * @param bool $prepend Whether to prepend the autoloader or not + * + * @return void + */ + public function register($prepend = false) + { + spl_autoload_register(array($this, 'loadClass'), true, $prepend); + + if (null === $this->vendorDir) { + return; + } + + if ($prepend) { + self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; + } else { + unset(self::$registeredLoaders[$this->vendorDir]); + self::$registeredLoaders[$this->vendorDir] = $this; + } + } + + /** + * Unregisters this instance as an autoloader. + * + * @return void + */ + public function unregister() + { + spl_autoload_unregister(array($this, 'loadClass')); + + if (null !== $this->vendorDir) { + unset(self::$registeredLoaders[$this->vendorDir]); + } + } + + /** + * Loads the given class or interface. + * + * @param string $class The name of the class + * @return true|null True if loaded, null otherwise + */ + public function loadClass($class) + { + if ($file = $this->findFile($class)) { + $includeFile = self::$includeFile; + $includeFile($file); + + return true; + } + + return null; + } + + /** + * Finds the path to the file where the class is defined. + * + * @param string $class The name of the class + * + * @return string|false The path if found, false otherwise + */ + public function findFile($class) + { + // class map lookup + if (isset($this->classMap[$class])) { + return $this->classMap[$class]; + } + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { + return false; + } + if (null !== $this->apcuPrefix) { + $file = apcu_fetch($this->apcuPrefix.$class, $hit); + if ($hit) { + return $file; + } + } + + $file = $this->findFileWithExtension($class, '.php'); + + // Search for Hack files if we are running on HHVM + if (false === $file && defined('HHVM_VERSION')) { + $file = $this->findFileWithExtension($class, '.hh'); + } + + if (null !== $this->apcuPrefix) { + apcu_add($this->apcuPrefix.$class, $file); + } + + if (false === $file) { + // Remember that this class does not exist. + $this->missingClasses[$class] = true; + } + + return $file; + } + + /** + * Returns the currently registered loaders keyed by their corresponding vendor directories. + * + * @return array + */ + public static function getRegisteredLoaders() + { + return self::$registeredLoaders; + } + + /** + * @param string $class + * @param string $ext + * @return string|false + */ + private function findFileWithExtension($class, $ext) + { + // PSR-4 lookup + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; + + $first = $class[0]; + if (isset($this->prefixLengthsPsr4[$first])) { + $subPath = $class; + while (false !== $lastPos = strrpos($subPath, '\\')) { + $subPath = substr($subPath, 0, $lastPos); + $search = $subPath . '\\'; + if (isset($this->prefixDirsPsr4[$search])) { + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); + foreach ($this->prefixDirsPsr4[$search] as $dir) { + if (file_exists($file = $dir . $pathEnd)) { + return $file; + } + } + } + } + } + + // PSR-4 fallback dirs + foreach ($this->fallbackDirsPsr4 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { + return $file; + } + } + + // PSR-0 lookup + if (false !== $pos = strrpos($class, '\\')) { + // namespaced class name + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); + } else { + // PEAR-like class name + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; + } + + if (isset($this->prefixesPsr0[$first])) { + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { + if (0 === strpos($class, $prefix)) { + foreach ($dirs as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + } + } + } + + // PSR-0 fallback dirs + foreach ($this->fallbackDirsPsr0 as $dir) { + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { + return $file; + } + } + + // PSR-0 include paths. + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { + return $file; + } + + return false; + } + + /** + * @return void + */ + private static function initializeIncludeClosure() + { + if (self::$includeFile !== null) { + return; + } + + /** + * Scope isolated include. + * + * Prevents access to $this/self from included files. + * + * @param string $file + * @return void + */ + self::$includeFile = \Closure::bind(static function($file) { + include $file; + }, null, null); + } +} diff --git a/e2e/parameter-type-extension/vendor/composer/InstalledVersions.php b/e2e/parameter-type-extension/vendor/composer/InstalledVersions.php new file mode 100644 index 00000000000..2052022fd8e --- /dev/null +++ b/e2e/parameter-type-extension/vendor/composer/InstalledVersions.php @@ -0,0 +1,396 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer; + +use Composer\Autoload\ClassLoader; +use Composer\Semver\VersionParser; + +/** + * This class is copied in every Composer installed project and available to all + * + * See also https://getcomposer.org/doc/07-runtime.md#installed-versions + * + * To require its presence, you can require `composer-runtime-api ^2.0` + * + * @final + */ +class InstalledVersions +{ + /** + * @var string|null if set (by reflection by Composer), this should be set to the path where this class is being copied to + * @internal + */ + private static $selfDir = null; + + /** + * @var mixed[]|null + * @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array}|array{}|null + */ + private static $installed; + + /** + * @var bool + */ + private static $installedIsLocalDir; + + /** + * @var bool|null + */ + private static $canGetVendors; + + /** + * @var array[] + * @psalm-var array}> + */ + private static $installedByVendor = array(); + + /** + * Returns a list of all package names which are present, either by being installed, replaced or provided + * + * @return string[] + * @psalm-return list + */ + public static function getInstalledPackages() + { + $packages = array(); + foreach (self::getInstalled() as $installed) { + $packages[] = array_keys($installed['versions']); + } + + if (1 === \count($packages)) { + return $packages[0]; + } + + return array_keys(array_flip(\call_user_func_array('array_merge', $packages))); + } + + /** + * Returns a list of all package names with a specific type e.g. 'library' + * + * @param string $type + * @return string[] + * @psalm-return list + */ + public static function getInstalledPackagesByType($type) + { + $packagesByType = array(); + + foreach (self::getInstalled() as $installed) { + foreach ($installed['versions'] as $name => $package) { + if (isset($package['type']) && $package['type'] === $type) { + $packagesByType[] = $name; + } + } + } + + return $packagesByType; + } + + /** + * Checks whether the given package is installed + * + * This also returns true if the package name is provided or replaced by another package + * + * @param string $packageName + * @param bool $includeDevRequirements + * @return bool + */ + public static function isInstalled($packageName, $includeDevRequirements = true) + { + foreach (self::getInstalled() as $installed) { + if (isset($installed['versions'][$packageName])) { + return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === false; + } + } + + return false; + } + + /** + * Checks whether the given package satisfies a version constraint + * + * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call: + * + * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3') + * + * @param VersionParser $parser Install composer/semver to have access to this class and functionality + * @param string $packageName + * @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package + * @return bool + */ + public static function satisfies(VersionParser $parser, $packageName, $constraint) + { + $constraint = $parser->parseConstraints((string) $constraint); + $provided = $parser->parseConstraints(self::getVersionRanges($packageName)); + + return $provided->matches($constraint); + } + + /** + * Returns a version constraint representing all the range(s) which are installed for a given package + * + * It is easier to use this via isInstalled() with the $constraint argument if you need to check + * whether a given version of a package is installed, and not just whether it exists + * + * @param string $packageName + * @return string Version constraint usable with composer/semver + */ + public static function getVersionRanges($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + $ranges = array(); + if (isset($installed['versions'][$packageName]['pretty_version'])) { + $ranges[] = $installed['versions'][$packageName]['pretty_version']; + } + if (array_key_exists('aliases', $installed['versions'][$packageName])) { + $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']); + } + if (array_key_exists('replaced', $installed['versions'][$packageName])) { + $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']); + } + if (array_key_exists('provided', $installed['versions'][$packageName])) { + $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']); + } + + return implode(' || ', $ranges); + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @param string $packageName + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present + */ + public static function getVersion($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + if (!isset($installed['versions'][$packageName]['version'])) { + return null; + } + + return $installed['versions'][$packageName]['version']; + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @param string $packageName + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present + */ + public static function getPrettyVersion($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + if (!isset($installed['versions'][$packageName]['pretty_version'])) { + return null; + } + + return $installed['versions'][$packageName]['pretty_version']; + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @param string $packageName + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference + */ + public static function getReference($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + if (!isset($installed['versions'][$packageName]['reference'])) { + return null; + } + + return $installed['versions'][$packageName]['reference']; + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @param string $packageName + * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path. + */ + public static function getInstallPath($packageName) + { + foreach (self::getInstalled() as $installed) { + if (!isset($installed['versions'][$packageName])) { + continue; + } + + return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null; + } + + throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed'); + } + + /** + * @return array + * @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool} + */ + public static function getRootPackage() + { + $installed = self::getInstalled(); + + return $installed[0]['root']; + } + + /** + * Returns the raw installed.php data for custom implementations + * + * @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect. + * @return array[] + * @psalm-return array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array} + */ + public static function getRawData() + { + @trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED); + + if (null === self::$installed) { + // only require the installed.php file if this file is loaded from its dumped location, + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 + if (substr(__DIR__, -8, 1) !== 'C') { + self::$installed = include __DIR__ . '/installed.php'; + } else { + self::$installed = array(); + } + } + + return self::$installed; + } + + /** + * Returns the raw data of all installed.php which are currently loaded for custom implementations + * + * @return array[] + * @psalm-return list}> + */ + public static function getAllRawData() + { + return self::getInstalled(); + } + + /** + * Lets you reload the static array from another file + * + * This is only useful for complex integrations in which a project needs to use + * this class but then also needs to execute another project's autoloader in process, + * and wants to ensure both projects have access to their version of installed.php. + * + * A typical case would be PHPUnit, where it would need to make sure it reads all + * the data it needs from this class, then call reload() with + * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure + * the project in which it runs can then also use this class safely, without + * interference between PHPUnit's dependencies and the project's dependencies. + * + * @param array[] $data A vendor/composer/installed.php data set + * @return void + * + * @psalm-param array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array} $data + */ + public static function reload($data) + { + self::$installed = $data; + self::$installedByVendor = array(); + + // when using reload, we disable the duplicate protection to ensure that self::$installed data is + // always returned, but we cannot know whether it comes from the installed.php in __DIR__ or not, + // so we have to assume it does not, and that may result in duplicate data being returned when listing + // all installed packages for example + self::$installedIsLocalDir = false; + } + + /** + * @return string + */ + private static function getSelfDir() + { + if (self::$selfDir === null) { + self::$selfDir = strtr(__DIR__, '\\', '/'); + } + + return self::$selfDir; + } + + /** + * @return array[] + * @psalm-return list}> + */ + private static function getInstalled() + { + if (null === self::$canGetVendors) { + self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders'); + } + + $installed = array(); + $copiedLocalDir = false; + + if (self::$canGetVendors) { + $selfDir = self::getSelfDir(); + foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) { + $vendorDir = strtr($vendorDir, '\\', '/'); + if (isset(self::$installedByVendor[$vendorDir])) { + $installed[] = self::$installedByVendor[$vendorDir]; + } elseif (is_file($vendorDir.'/composer/installed.php')) { + /** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array} $required */ + $required = require $vendorDir.'/composer/installed.php'; + self::$installedByVendor[$vendorDir] = $required; + $installed[] = $required; + if (self::$installed === null && $vendorDir.'/composer' === $selfDir) { + self::$installed = $required; + self::$installedIsLocalDir = true; + } + } + if (self::$installedIsLocalDir && $vendorDir.'/composer' === $selfDir) { + $copiedLocalDir = true; + } + } + } + + if (null === self::$installed) { + // only require the installed.php file if this file is loaded from its dumped location, + // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 + if (substr(__DIR__, -8, 1) !== 'C') { + /** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array} $required */ + $required = require __DIR__ . '/installed.php'; + self::$installed = $required; + } else { + self::$installed = array(); + } + } + + if (self::$installed !== array() && !$copiedLocalDir) { + $installed[] = self::$installed; + } + + return $installed; + } +} diff --git a/e2e/parameter-type-extension/vendor/composer/LICENSE b/e2e/parameter-type-extension/vendor/composer/LICENSE new file mode 100644 index 00000000000..f27399a042d --- /dev/null +++ b/e2e/parameter-type-extension/vendor/composer/LICENSE @@ -0,0 +1,21 @@ + +Copyright (c) Nils Adermann, Jordi Boggiano + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished +to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/e2e/parameter-type-extension/vendor/composer/autoload_classmap.php b/e2e/parameter-type-extension/vendor/composer/autoload_classmap.php new file mode 100644 index 00000000000..0fb0a2c194b --- /dev/null +++ b/e2e/parameter-type-extension/vendor/composer/autoload_classmap.php @@ -0,0 +1,10 @@ + $vendorDir . '/composer/InstalledVersions.php', +); diff --git a/e2e/parameter-type-extension/vendor/composer/autoload_namespaces.php b/e2e/parameter-type-extension/vendor/composer/autoload_namespaces.php new file mode 100644 index 00000000000..15a2ff3ad6d --- /dev/null +++ b/e2e/parameter-type-extension/vendor/composer/autoload_namespaces.php @@ -0,0 +1,9 @@ + array($baseDir . '/src'), +); diff --git a/e2e/parameter-type-extension/vendor/composer/autoload_real.php b/e2e/parameter-type-extension/vendor/composer/autoload_real.php new file mode 100644 index 00000000000..4f0c8d559f2 --- /dev/null +++ b/e2e/parameter-type-extension/vendor/composer/autoload_real.php @@ -0,0 +1,36 @@ +register(true); + + return $loader; + } +} diff --git a/e2e/parameter-type-extension/vendor/composer/autoload_static.php b/e2e/parameter-type-extension/vendor/composer/autoload_static.php new file mode 100644 index 00000000000..0da183802cb --- /dev/null +++ b/e2e/parameter-type-extension/vendor/composer/autoload_static.php @@ -0,0 +1,36 @@ + + array ( + 'App\\' => 4, + ), + ); + + public static $prefixDirsPsr4 = array ( + 'App\\' => + array ( + 0 => __DIR__ . '/../..' . '/src', + ), + ); + + public static $classMap = array ( + 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', + ); + + public static function getInitializer(ClassLoader $loader) + { + return \Closure::bind(function () use ($loader) { + $loader->prefixLengthsPsr4 = ComposerStaticInitd751713988987e9331980363e24189ce::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInitd751713988987e9331980363e24189ce::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInitd751713988987e9331980363e24189ce::$classMap; + + }, null, ClassLoader::class); + } +} diff --git a/e2e/parameter-type-extension/vendor/composer/installed.json b/e2e/parameter-type-extension/vendor/composer/installed.json new file mode 100644 index 00000000000..87fda747e6c --- /dev/null +++ b/e2e/parameter-type-extension/vendor/composer/installed.json @@ -0,0 +1,5 @@ +{ + "packages": [], + "dev": true, + "dev-package-names": [] +} diff --git a/e2e/parameter-type-extension/vendor/composer/installed.php b/e2e/parameter-type-extension/vendor/composer/installed.php new file mode 100644 index 00000000000..99aa7db51cb --- /dev/null +++ b/e2e/parameter-type-extension/vendor/composer/installed.php @@ -0,0 +1,23 @@ + array( + 'name' => '__root__', + 'pretty_version' => '1.0.0+no-version-set', + 'version' => '1.0.0.0', + 'reference' => null, + 'type' => 'library', + 'install_path' => __DIR__ . '/../../', + 'aliases' => array(), + 'dev' => true, + ), + 'versions' => array( + '__root__' => array( + 'pretty_version' => '1.0.0+no-version-set', + 'version' => '1.0.0.0', + 'reference' => null, + 'type' => 'library', + 'install_path' => __DIR__ . '/../../', + 'aliases' => array(), + 'dev_requirement' => false, + ), + ), +); diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 02b99ed61bb..9a67c2ba853 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -90,12 +90,102 @@ parameters: count: 1 path: src/Analyser/MutatingScope.php + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Call to method getTypeFromFunctionCall() of deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension.' + identifier: method.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Call to method getTypeFromMethodCall() of deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension.' + identifier: method.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Call to method getTypeFromStaticMethodCall() of deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension.' + identifier: method.deprecatedInterface + count: 2 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Call to method isFunctionSupported() of deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension.' + identifier: method.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Call to method isMethodSupported() of deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension.' + identifier: method.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Call to method isStaticMethodSupported() of deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension.' + identifier: method.deprecatedInterface + count: 2 + path: src/Analyser/NodeScopeResolver.php + - rawMessage: 'Parameter #2 $node of method PHPStan\BetterReflection\SourceLocator\Ast\Strategy\NodeToReflection::__invoke() expects PhpParser\Node\Expr\ArrowFunction|PhpParser\Node\Expr\Closure|PhpParser\Node\Expr\FuncCall|PhpParser\Node\Stmt\Class_|PhpParser\Node\Stmt\Const_|PhpParser\Node\Stmt\Enum_|PhpParser\Node\Stmt\Function_|PhpParser\Node\Stmt\Interface_|PhpParser\Node\Stmt\Trait_, PhpParser\Node\Stmt\ClassLike given.' identifier: argument.type count: 1 path: src/Analyser/NodeScopeResolver.php + - + rawMessage: 'Parameter $functionParameterClosureTypeExtensions of method PHPStan\Analyser\NodeScopeResolver::__construct() has typehint with deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension.' + identifier: parameter.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Parameter $methodParameterClosureTypeExtensions of method PHPStan\Analyser\NodeScopeResolver::__construct() has typehint with deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension.' + identifier: parameter.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Parameter $staticMethodParameterClosureTypeExtensions of method PHPStan\Analyser\NodeScopeResolver::__construct() has typehint with deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension.' + identifier: parameter.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: Property $functionParameterClosureTypeExtensions references deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension in its type. + identifier: property.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: Property $methodParameterClosureTypeExtensions references deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension in its type. + identifier: property.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: Property $staticMethodParameterClosureTypeExtensions references deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension in its type. + identifier: property.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + - rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantBooleanType is error-prone and deprecated. Use Type::isTrue() or Type::isFalse() instead.' identifier: phpstanApi.instanceofType @@ -759,6 +849,42 @@ parameters: count: 1 path: src/Testing/LevelsTestCase.php + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Testing/RuleTestCase.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Testing/RuleTestCase.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Testing/RuleTestCase.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Testing/TypeInferenceTestCase.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Testing/TypeInferenceTestCase.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Testing/TypeInferenceTestCase.php + - rawMessage: 'Doing instanceof PHPStan\Type\ConstantScalarType is error-prone and deprecated. Use Type::isConstantScalarValue() or Type::getConstantScalarTypes() or Type::getConstantScalarValues() instead.' identifier: phpstanApi.instanceofType @@ -1785,6 +1911,24 @@ parameters: count: 2 path: src/Type/VoidType.php + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: tests/PHPStan/Analyser/AnalyserTest.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: tests/PHPStan/Analyser/AnalyserTest.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: tests/PHPStan/Analyser/AnalyserTest.php + - rawMessage: 'Class PHPStan\Analyser\AnonymousClassNameRuleTest extends generic class PHPStan\Testing\RuleTestCase but does not specify its types: TRule' identifier: missingType.generics @@ -1809,6 +1953,42 @@ parameters: count: 1 path: tests/PHPStan/Analyser/EvaluationOrderTest.php + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverRuleTest.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverRuleTest.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverRuleTest.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverTest.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverTest.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverTest.php + - rawMessage: Constant SOME_CONSTANT_IN_AUTOLOAD_FILE not found. identifier: constant.notFound diff --git a/src/Analyser/ExprHandler.php b/src/Analyser/ExprHandler.php index 5e57b94e2e0..b049745128c 100644 --- a/src/Analyser/ExprHandler.php +++ b/src/Analyser/ExprHandler.php @@ -32,6 +32,7 @@ public function processExpr( ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, + ?Type $overriddenType, ): ExpressionResult; /** diff --git a/src/Analyser/ExprHandler/ArrayDimFetchHandler.php b/src/Analyser/ExprHandler/ArrayDimFetchHandler.php index b52587be917..65110c190a2 100644 --- a/src/Analyser/ExprHandler/ArrayDimFetchHandler.php +++ b/src/Analyser/ExprHandler/ArrayDimFetchHandler.php @@ -79,11 +79,11 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type ); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; if ($expr->dim === null) { - $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep()); + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $varResult->getScope(); return $this->expressionResultFactory->create( @@ -98,8 +98,8 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex ); } - $dimResult = $nodeScopeResolver->processExprNode($stmt, $expr->dim, $scope, $storage, $nodeCallback, $context->enterDeep()); - $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $dimResult->getScope(), $storage, $nodeCallback, $context->enterDeep()); + $dimResult = $nodeScopeResolver->processExprNode($stmt, $expr->dim, $scope, $storage, $nodeCallback, $context->enterDeep(), null); + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $dimResult->getScope(), $storage, $nodeCallback, $context->enterDeep(), null); $throwPoints = array_merge($dimResult->getThrowPoints(), $varResult->getThrowPoints()); $impurePoints = array_merge($dimResult->getImpurePoints(), $varResult->getImpurePoints()); $scope = $varResult->getScope(); @@ -113,6 +113,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $storage, new NoopNodeCallback(), $context, + null, )->getThrowPoints()); } diff --git a/src/Analyser/ExprHandler/ArrayHandler.php b/src/Analyser/ExprHandler/ArrayHandler.php index a8e58b932a4..9ccb7f1f5e0 100644 --- a/src/Analyser/ExprHandler/ArrayHandler.php +++ b/src/Analyser/ExprHandler/ArrayHandler.php @@ -24,6 +24,7 @@ use PHPStan\Node\LiteralArrayNode; use PHPStan\Reflection\InitializerExprTypeResolver; use PHPStan\Type\CallableType; +use PHPStan\Type\Constant\ConstantIntegerType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use function array_merge; @@ -72,7 +73,7 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type return $type; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $itemNodes = []; @@ -80,11 +81,14 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $throwPoints = []; $impurePoints = []; $isAlwaysTerminating = false; + $nextAutoIndex = 0; foreach ($expr->items as $arrayItem) { $itemNodes[] = new LiteralArrayItem($scope, $arrayItem); $nodeScopeResolver->callNodeCallback($nodeCallback, $arrayItem, $scope, $storage); + $keyType = new ConstantIntegerType($nextAutoIndex); if ($arrayItem->key !== null) { - $keyResult = $nodeScopeResolver->processExprNode($stmt, $arrayItem->key, $scope, $storage, $nodeCallback, $context->enterDeep()); + $keyType = $scope->getType($arrayItem->key); + $keyResult = $nodeScopeResolver->processExprNode($stmt, $arrayItem->key, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $keyResult->hasYield(); $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); @@ -92,7 +96,18 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $scope = $keyResult->getScope(); } - $valueResult = $nodeScopeResolver->processExprNode($stmt, $arrayItem->value, $scope, $storage, $nodeCallback, $context->enterDeep()); + $overriddenValueType = null; + if ($overriddenType !== null && $overriddenType->hasOffsetValueType($keyType)->yes()) { + $overriddenValueType = $overriddenType->getOffsetValueType($keyType); + } + + if ($arrayItem->key === null) { + $nextAutoIndex++; + } elseif ($keyType instanceof ConstantIntegerType) { + $nextAutoIndex = $keyType->getValue() + 1; + } + + $valueResult = $nodeScopeResolver->processExprNode($stmt, $arrayItem->value, $scope, $storage, $nodeCallback, $context->enterDeep(), $overriddenValueType); $hasYield = $hasYield || $valueResult->hasYield(); $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints()); diff --git a/src/Analyser/ExprHandler/ArrowFunctionHandler.php b/src/Analyser/ExprHandler/ArrowFunctionHandler.php index 5390ee8e212..dad883c60c1 100644 --- a/src/Analyser/ExprHandler/ArrowFunctionHandler.php +++ b/src/Analyser/ExprHandler/ArrowFunctionHandler.php @@ -39,9 +39,9 @@ public function supports(Expr $expr): bool return $expr instanceof ArrowFunction; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $result = $nodeScopeResolver->processArrowFunctionNode($stmt, $expr, $scope, $storage, $nodeCallback, null); + $result = $nodeScopeResolver->processArrowFunctionNode($stmt, $expr, $scope, $storage, $nodeCallback, $overriddenType); return $this->expressionResultFactory->create( $result->getScope(), diff --git a/src/Analyser/ExprHandler/AssignHandler.php b/src/Analyser/ExprHandler/AssignHandler.php index f5b30e63bbe..9f9579114f7 100644 --- a/src/Analyser/ExprHandler/AssignHandler.php +++ b/src/Analyser/ExprHandler/AssignHandler.php @@ -290,7 +290,7 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e return $specifiedTypes; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $result = $this->processAssignVar( @@ -331,7 +331,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto ); } - $result = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $result = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); @@ -449,7 +449,7 @@ public function processAssignVar( if ($if === null) { $if = $assignedExpr->cond; } - $condScope = $nodeScopeResolver->processExprNode($stmt, $assignedExpr->cond, $scope, $storage->duplicate(), new NoopNodeCallback(), ExpressionContext::createDeep())->getScope(); + $condScope = $nodeScopeResolver->processExprNode($stmt, $assignedExpr->cond, $scope, $storage->duplicate(), new NoopNodeCallback(), ExpressionContext::createDeep(), null)->getScope(); $truthySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($condScope, $assignedExpr->cond, TypeSpecifierContext::createTruthy()); $falseySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($condScope, $assignedExpr->cond, TypeSpecifierContext::createFalsey()); $truthyScope = $condScope->filterBySpecifiedTypes($truthySpecifiedTypes); @@ -537,7 +537,7 @@ public function processAssignVar( $scope = $this->processArrayByRefItems($scope, $var->name, $assignedExpr, new Variable($var->name)); } } else { - $nameExprResult = $nodeScopeResolver->processExprNode($stmt, $var->name, $scope, $storage, $nodeCallback, $context); + $nameExprResult = $nodeScopeResolver->processExprNode($stmt, $var->name, $scope, $storage, $nodeCallback, $context, null); $hasYield = $hasYield || $nameExprResult->hasYield(); $throwPoints = array_merge($throwPoints, $nameExprResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $nameExprResult->getImpurePoints()); @@ -582,7 +582,7 @@ public function processAssignVar( if ($enterExpressionAssign) { $scope = $scope->enterExpressionAssign($var, false); } - $result = $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, $context->enterDeep()); + $result = $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); @@ -631,7 +631,7 @@ public function processAssignVar( throwPoints: [], impurePoints: [], )); - $result = $nodeScopeResolver->processExprNode($stmt, $dimExpr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $result = $nodeScopeResolver->processExprNode($stmt, $dimExpr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $offsetTypes[] = [$result->getType(), $dimFetch]; $offsetNativeTypes[] = [$result->getNativeType(), $dimFetch]; $hasYield = $hasYield || $result->hasYield(); @@ -751,10 +751,11 @@ public function processAssignVar( $storage, new NoopNodeCallback(), $context, + null, )->getThrowPoints()); } } elseif ($var instanceof PropertyFetch) { - $objectResult = $nodeScopeResolver->processExprNode($stmt, $var->var, $scope, $storage, $nodeCallback, $context); + $objectResult = $nodeScopeResolver->processExprNode($stmt, $var->var, $scope, $storage, $nodeCallback, $context, null); $hasYield = $objectResult->hasYield(); $throwPoints = $objectResult->getThrowPoints(); $impurePoints = $objectResult->getImpurePoints(); @@ -765,7 +766,7 @@ public function processAssignVar( if ($var->name instanceof Node\Identifier) { $propertyName = $var->name->name; } else { - $propertyNameResult = $nodeScopeResolver->processExprNode($stmt, $var->name, $scope, $storage, $nodeCallback, $context); + $propertyNameResult = $nodeScopeResolver->processExprNode($stmt, $var->name, $scope, $storage, $nodeCallback, $context, null); $hasYield = $hasYield || $propertyNameResult->hasYield(); $throwPoints = array_merge($throwPoints, $propertyNameResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $propertyNameResult->getImpurePoints()); @@ -861,6 +862,7 @@ public function processAssignVar( $storage, new NoopNodeCallback(), $context, + null, )->getThrowPoints()); } } @@ -869,7 +871,7 @@ public function processAssignVar( if ($var->class instanceof Node\Name) { $propertyHolderType = $scope->resolveTypeByName($var->class); } else { - $nodeScopeResolver->processExprNode($stmt, $var->class, $scope, $storage, $nodeCallback, $context); + $nodeScopeResolver->processExprNode($stmt, $var->class, $scope, $storage, $nodeCallback, $context, null); $propertyHolderType = $scope->getType($var->class); } @@ -877,7 +879,7 @@ public function processAssignVar( if ($var->name instanceof Node\Identifier) { $propertyName = $var->name->name; } else { - $propertyNameResult = $nodeScopeResolver->processExprNode($stmt, $var->name, $scope, $storage, $nodeCallback, $context); + $propertyNameResult = $nodeScopeResolver->processExprNode($stmt, $var->name, $scope, $storage, $nodeCallback, $context, null); $hasYield = $propertyNameResult->hasYield(); $throwPoints = $propertyNameResult->getThrowPoints(); $impurePoints = $propertyNameResult->getImpurePoints(); @@ -949,7 +951,7 @@ public function processAssignVar( $itemScope = $nodeScopeResolver->lookForSetAllowedUndefinedExpressions($itemScope, $arrayItem->value); $nodeScopeResolver->callNodeCallback($nodeCallback, $arrayItem, $itemScope, $storage); if ($arrayItem->key !== null) { - $keyResult = $nodeScopeResolver->processExprNode($stmt, $arrayItem->key, $itemScope, $storage, $nodeCallback, $context->enterDeep()); + $keyResult = $nodeScopeResolver->processExprNode($stmt, $arrayItem->key, $itemScope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $keyResult->hasYield(); $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); @@ -1001,13 +1003,13 @@ public function processAssignVar( // the chain is usually a clone of AST nodes already processed elsewhere // (see Unset_ handling) - process it with a noop callback so that // results for its nodes are stored without invoking rules twice - $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, new NoopNodeCallback(), $context->enterDeep()); + $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, new NoopNodeCallback(), $context->enterDeep(), null); $offsetTypes = []; $offsetNativeTypes = []; foreach (array_reverse($dimFetchStack) as $dimFetch) { $dimExpr = $dimFetch->getDim(); - $nodeScopeResolver->processExprNode($stmt, $dimExpr, $scope, $storage, new NoopNodeCallback(), $context->enterDeep()); + $nodeScopeResolver->processExprNode($stmt, $dimExpr, $scope, $storage, new NoopNodeCallback(), $context->enterDeep(), null); $offsetTypes[] = [$scope->getType($dimExpr), $dimFetch]; $offsetNativeTypes[] = [$scope->getNativeType($dimExpr), $dimFetch]; } @@ -1055,7 +1057,7 @@ public function processAssignVar( ); } } else { - $varResult = $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, $context); + $varResult = $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, $context, null); $hasYield = $varResult->hasYield(); $throwPoints = array_merge($throwPoints, $varResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $varResult->getImpurePoints()); diff --git a/src/Analyser/ExprHandler/AssignOpHandler.php b/src/Analyser/ExprHandler/AssignOpHandler.php index fb25464e4b5..145d8533c34 100644 --- a/src/Analyser/ExprHandler/AssignOpHandler.php +++ b/src/Analyser/ExprHandler/AssignOpHandler.php @@ -54,7 +54,7 @@ public function supports(Expr $expr): bool return $expr instanceof AssignOp; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $assignResult = $this->assignHandler->processAssignVar( @@ -81,7 +81,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context, $sto } } - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); if ($expr instanceof Expr\AssignOp\Coalesce) { $isAlwaysTerminating = $exprResult->isAlwaysTerminating() && $originalScope->getType($expr->var)->isNull()->yes(); return $this->expressionResultFactory->create( diff --git a/src/Analyser/ExprHandler/BinaryOpHandler.php b/src/Analyser/ExprHandler/BinaryOpHandler.php index f8df18444ef..ec4e6b919e9 100644 --- a/src/Analyser/ExprHandler/BinaryOpHandler.php +++ b/src/Analyser/ExprHandler/BinaryOpHandler.php @@ -83,11 +83,11 @@ public function supports(Expr $expr): bool && !$expr instanceof BinaryOp\Pipe; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep()); - $rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $leftResult->getScope(), $storage, $nodeCallback, $context->enterDeep()); + $leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep(), null); + $rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $leftResult->getScope(), $storage, $nodeCallback, $context->enterDeep(), null); $throwPoints = array_merge($leftResult->getThrowPoints(), $rightResult->getThrowPoints()); $impurePoints = array_merge($leftResult->getImpurePoints(), $rightResult->getImpurePoints()); if ( diff --git a/src/Analyser/ExprHandler/BitwiseNotHandler.php b/src/Analyser/ExprHandler/BitwiseNotHandler.php index 4b6b6667823..716db963fa6 100644 --- a/src/Analyser/ExprHandler/BitwiseNotHandler.php +++ b/src/Analyser/ExprHandler/BitwiseNotHandler.php @@ -39,9 +39,9 @@ public function supports(Expr $expr): bool return $expr instanceof BitwiseNot; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); return $this->expressionResultFactory->create( $exprResult->getScope(), diff --git a/src/Analyser/ExprHandler/BooleanAndHandler.php b/src/Analyser/ExprHandler/BooleanAndHandler.php index cad60960e80..e3f7af1e738 100644 --- a/src/Analyser/ExprHandler/BooleanAndHandler.php +++ b/src/Analyser/ExprHandler/BooleanAndHandler.php @@ -63,7 +63,7 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type } if (self::getBooleanExpressionDepth($expr->left) <= self::BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH) { - $leftResult = $this->nodeScopeResolver->processExprNode(new Stmt\Expression($expr->left), $expr->left, $scope, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep()); + $leftResult = $this->nodeScopeResolver->processExprNode(new Stmt\Expression($expr->left), $expr->left, $scope, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep(), null); $rightBooleanType = $leftResult->getTruthyScope()->getType($expr->right)->toBoolean(); } else { $rightBooleanType = $scope->filterByTruthyValue($expr->left)->getType($expr->right)->toBoolean(); @@ -260,11 +260,11 @@ private function isTrackableExpression(Expr $expr): bool || $expr instanceof Expr\StaticPropertyFetch; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep()); + $leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $leftTruthyScope = $leftResult->getTruthyScope(); - $rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $leftTruthyScope, $storage, $nodeCallback, $context); + $rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $leftTruthyScope, $storage, $nodeCallback, $context, null); $rightExprType = $rightResult->getType(); if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) { $leftMergedWithRightScope = $leftResult->getFalseyScope(); diff --git a/src/Analyser/ExprHandler/BooleanNotHandler.php b/src/Analyser/ExprHandler/BooleanNotHandler.php index 59cb5a986c1..d5d83ca5e05 100644 --- a/src/Analyser/ExprHandler/BooleanNotHandler.php +++ b/src/Analyser/ExprHandler/BooleanNotHandler.php @@ -37,10 +37,10 @@ public function supports(Expr $expr): bool return $expr instanceof BooleanNot; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $exprResult->getScope(); return $this->expressionResultFactory->create( diff --git a/src/Analyser/ExprHandler/BooleanOrHandler.php b/src/Analyser/ExprHandler/BooleanOrHandler.php index fb9914e56cf..963453a9fc3 100644 --- a/src/Analyser/ExprHandler/BooleanOrHandler.php +++ b/src/Analyser/ExprHandler/BooleanOrHandler.php @@ -70,7 +70,7 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type } if (BooleanAndHandler::getBooleanExpressionDepth($expr->left) <= self::BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH) { - $leftResult = $this->nodeScopeResolver->processExprNode(new Stmt\Expression($expr->left), $expr->left, $scope, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep()); + $leftResult = $this->nodeScopeResolver->processExprNode(new Stmt\Expression($expr->left), $expr->left, $scope, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep(), null); $rightBooleanType = $leftResult->getFalseyScope()->getType($expr->right)->toBoolean(); } else { $rightBooleanType = $scope->filterByFalseyValue($expr->left)->getType($expr->right)->toBoolean(); @@ -343,11 +343,11 @@ private function augmentBooleanOrTruthyWithConditionalHolders(TypeSpecifier $typ return $types; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep()); + $leftResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $leftFalseyScope = $leftResult->getFalseyScope(); - $rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $leftFalseyScope, $storage, $nodeCallback, $context); + $rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $leftFalseyScope, $storage, $nodeCallback, $context, null); $rightExprType = $rightResult->getType(); if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) { $leftMergedWithRightScope = $leftResult->getTruthyScope(); diff --git a/src/Analyser/ExprHandler/CastHandler.php b/src/Analyser/ExprHandler/CastHandler.php index 6877fdd5b3e..f446f81ecbd 100644 --- a/src/Analyser/ExprHandler/CastHandler.php +++ b/src/Analyser/ExprHandler/CastHandler.php @@ -46,10 +46,10 @@ public function supports(Expr $expr): bool return $expr instanceof Cast && !$expr instanceof Cast\String_; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $exprResult->getScope(); return $this->expressionResultFactory->create( diff --git a/src/Analyser/ExprHandler/CastStringHandler.php b/src/Analyser/ExprHandler/CastStringHandler.php index bb13a0e2817..c5f4ecb5f64 100644 --- a/src/Analyser/ExprHandler/CastStringHandler.php +++ b/src/Analyser/ExprHandler/CastStringHandler.php @@ -44,10 +44,10 @@ public function supports(Expr $expr): bool return $expr instanceof Cast\String_; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $impurePoints = $exprResult->getImpurePoints(); $throwPoints = $exprResult->getThrowPoints(); diff --git a/src/Analyser/ExprHandler/ClassConstFetchHandler.php b/src/Analyser/ExprHandler/ClassConstFetchHandler.php index fe989f332c9..e8f9ec80bc6 100644 --- a/src/Analyser/ExprHandler/ClassConstFetchHandler.php +++ b/src/Analyser/ExprHandler/ClassConstFetchHandler.php @@ -56,7 +56,7 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type ); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $hasYield = false; @@ -65,7 +65,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $isAlwaysTerminating = false; if ($expr->class instanceof Expr) { - $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep()); + $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $classResult->getScope(); $hasYield = $classResult->hasYield(); $throwPoints = $classResult->getThrowPoints(); @@ -78,7 +78,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex if ($expr->name instanceof Identifier) { $nodeScopeResolver->callNodeCallback($nodeCallback, $expr->name, $scope, $storage); } else { - $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep()); + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $nameResult->getScope(); $hasYield = $hasYield || $nameResult->hasYield(); $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); diff --git a/src/Analyser/ExprHandler/CloneHandler.php b/src/Analyser/ExprHandler/CloneHandler.php index bc707347411..dc5ebb7c6ab 100644 --- a/src/Analyser/ExprHandler/CloneHandler.php +++ b/src/Analyser/ExprHandler/CloneHandler.php @@ -39,9 +39,9 @@ public function supports(Expr $expr): bool return $expr instanceof Clone_; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); return $this->expressionResultFactory->create( $exprResult->getScope(), diff --git a/src/Analyser/ExprHandler/ClosureHandler.php b/src/Analyser/ExprHandler/ClosureHandler.php index b107c5843c6..24ff8582657 100644 --- a/src/Analyser/ExprHandler/ClosureHandler.php +++ b/src/Analyser/ExprHandler/ClosureHandler.php @@ -39,9 +39,9 @@ public function supports(Expr $expr): bool return $expr instanceof Closure; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $processClosureResult = $nodeScopeResolver->processClosureNode($stmt, $expr, $scope, $storage, $nodeCallback, $context, null); + $processClosureResult = $nodeScopeResolver->processClosureNode($stmt, $expr, $scope, $storage, $nodeCallback, $context, $overriddenType); return $this->expressionResultFactory->create( $processClosureResult->applyByRefUseScope($processClosureResult->getScope()), diff --git a/src/Analyser/ExprHandler/CoalesceHandler.php b/src/Analyser/ExprHandler/CoalesceHandler.php index d7b0fa53571..adb9d5d28b0 100644 --- a/src/Analyser/ExprHandler/CoalesceHandler.php +++ b/src/Analyser/ExprHandler/CoalesceHandler.php @@ -117,17 +117,17 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e return (new SpecifiedTypes([], []))->setRootExpr($expr); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $nonNullabilityResult = $this->nonNullabilityHelper->ensureNonNullability($scope, $expr->left); $condScope = $nodeScopeResolver->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $expr->left); - $condResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $condScope, $storage, $nodeCallback, $context->enterDeep()); + $condResult = $nodeScopeResolver->processExprNode($stmt, $expr->left, $condScope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $this->nonNullabilityHelper->revertNonNullability($condResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions()); $scope = $nodeScopeResolver->lookForUnsetAllowedUndefinedExpressions($scope, $expr->left); $rightScope = $scope->filterByFalseyValue($expr); - $rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $rightScope, $storage, $nodeCallback, $context->enterDeep()); + $rightResult = $nodeScopeResolver->processExprNode($stmt, $expr->right, $rightScope, $storage, $nodeCallback, $context->enterDeep(), null); $rightExprType = $rightResult->getType(); if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) { $scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left])); diff --git a/src/Analyser/ExprHandler/ConstFetchHandler.php b/src/Analyser/ExprHandler/ConstFetchHandler.php index 17f429322e0..ec4a9a9dc22 100644 --- a/src/Analyser/ExprHandler/ConstFetchHandler.php +++ b/src/Analyser/ExprHandler/ConstFetchHandler.php @@ -44,7 +44,7 @@ public function supports(Expr $expr): bool return $expr instanceof ConstFetch; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $nodeScopeResolver->callNodeCallback($nodeCallback, $expr->name, $scope, $storage); diff --git a/src/Analyser/ExprHandler/EmptyHandler.php b/src/Analyser/ExprHandler/EmptyHandler.php index 54e9c397fa1..e267526e0bc 100644 --- a/src/Analyser/ExprHandler/EmptyHandler.php +++ b/src/Analyser/ExprHandler/EmptyHandler.php @@ -85,12 +85,12 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e ), $context)->setRootExpr($expr); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $nonNullabilityResult = $this->nonNullabilityHelper->ensureNonNullability($scope, $expr->expr); $scope = $nodeScopeResolver->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $expr->expr); - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $exprResult->getScope(); $scope = $this->nonNullabilityHelper->revertNonNullability($scope, $nonNullabilityResult->getSpecifiedExpressions()); $scope = $nodeScopeResolver->lookForUnsetAllowedUndefinedExpressions($scope, $expr->expr); diff --git a/src/Analyser/ExprHandler/ErrorSuppressHandler.php b/src/Analyser/ExprHandler/ErrorSuppressHandler.php index ca006ebcedb..8e2fca6ed2d 100644 --- a/src/Analyser/ExprHandler/ErrorSuppressHandler.php +++ b/src/Analyser/ExprHandler/ErrorSuppressHandler.php @@ -35,9 +35,9 @@ public function supports(Expr $expr): bool return $expr instanceof ErrorSuppress; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context, null); return $this->expressionResultFactory->create( $exprResult->getScope(), diff --git a/src/Analyser/ExprHandler/EvalHandler.php b/src/Analyser/ExprHandler/EvalHandler.php index 93cf1adc508..9e52d9381c9 100644 --- a/src/Analyser/ExprHandler/EvalHandler.php +++ b/src/Analyser/ExprHandler/EvalHandler.php @@ -44,10 +44,10 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type return new MixedType(); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $exprResult->getScope()->invalidateVolatileExpressions(); return $this->expressionResultFactory->create( diff --git a/src/Analyser/ExprHandler/ExitHandler.php b/src/Analyser/ExprHandler/ExitHandler.php index 7c1029c14e2..83c7eca56ef 100644 --- a/src/Analyser/ExprHandler/ExitHandler.php +++ b/src/Analyser/ExprHandler/ExitHandler.php @@ -38,7 +38,7 @@ public function supports(Expr $expr): bool return $expr instanceof Exit_; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $kind = $expr->getAttribute('kind', Exit_::KIND_EXIT); @@ -50,7 +50,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $hasYield = false; $throwPoints = []; if ($expr->expr !== null) { - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $exprResult->hasYield(); $throwPoints = $exprResult->getThrowPoints(); $impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints()); diff --git a/src/Analyser/ExprHandler/FirstClassCallableFuncCallHandler.php b/src/Analyser/ExprHandler/FirstClassCallableFuncCallHandler.php index 266996eaeb2..eb9fed81d12 100644 --- a/src/Analyser/ExprHandler/FirstClassCallableFuncCallHandler.php +++ b/src/Analyser/ExprHandler/FirstClassCallableFuncCallHandler.php @@ -49,6 +49,7 @@ public function processExpr( ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, + ?Type $overriddenType, ): ExpressionResult { // handled in NodeScopeResolver before ExprHandlers are called diff --git a/src/Analyser/ExprHandler/FirstClassCallableMethodCallHandler.php b/src/Analyser/ExprHandler/FirstClassCallableMethodCallHandler.php index 1cafdd5b120..4cef4f47935 100644 --- a/src/Analyser/ExprHandler/FirstClassCallableMethodCallHandler.php +++ b/src/Analyser/ExprHandler/FirstClassCallableMethodCallHandler.php @@ -49,6 +49,7 @@ public function processExpr( ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, + ?Type $overriddenType, ): ExpressionResult { // handled in NodeScopeResolver before ExprHandlers are called diff --git a/src/Analyser/ExprHandler/FirstClassCallableNewHandler.php b/src/Analyser/ExprHandler/FirstClassCallableNewHandler.php index e158a8cc7b8..9ade7f94c3a 100644 --- a/src/Analyser/ExprHandler/FirstClassCallableNewHandler.php +++ b/src/Analyser/ExprHandler/FirstClassCallableNewHandler.php @@ -48,6 +48,7 @@ public function processExpr( ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, + ?Type $overriddenType, ): ExpressionResult { // handled in NodeScopeResolver before ExprHandlers are called diff --git a/src/Analyser/ExprHandler/FirstClassCallableStaticCallHandler.php b/src/Analyser/ExprHandler/FirstClassCallableStaticCallHandler.php index 4d3519cf944..fd7c4c042a8 100644 --- a/src/Analyser/ExprHandler/FirstClassCallableStaticCallHandler.php +++ b/src/Analyser/ExprHandler/FirstClassCallableStaticCallHandler.php @@ -47,6 +47,7 @@ public function processExpr( ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, + ?Type $overriddenType, ): ExpressionResult { // handled in NodeScopeResolver before ExprHandlers are called diff --git a/src/Analyser/ExprHandler/FuncCallHandler.php b/src/Analyser/ExprHandler/FuncCallHandler.php index c7d1c46c178..fcf698f1b7c 100644 --- a/src/Analyser/ExprHandler/FuncCallHandler.php +++ b/src/Analyser/ExprHandler/FuncCallHandler.php @@ -116,7 +116,7 @@ public function supports(Expr $expr): bool return $expr instanceof FuncCall && !$expr->isFirstClassCallable(); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $parametersAcceptor = null; @@ -127,7 +127,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex if ($expr->name instanceof Expr) { // process the dynamic callee name first, then consume its type rather // than reading it before processExprNode() stores its result - $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep()); + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $nameType = $nameResult->getType(); if (!$nameType->isCallable()->no()) { $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( @@ -328,6 +328,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $storage, new NoopNodeCallback(), $context->enterDeep(), + null, ); $throwPoints = array_merge($throwPoints, $invokeResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $invokeResult->getImpurePoints()); diff --git a/src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php b/src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php index ff165386f75..dba51b80bf2 100644 --- a/src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php +++ b/src/Analyser/ExprHandler/Helper/ClosureTypeResolver.php @@ -194,6 +194,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu $invalidateExpressions[] = new InvalidateExprNode($node->getPropertyFetch()); }, ExpressionContext::createDeep(), + null, ); $throwPoints = array_map(static fn ($throwPoint) => $throwPoint->toPublic(), $arrowFunctionExprResult->getThrowPoints()); $impurePoints = array_merge($arrowFunctionImpurePoints, $arrowFunctionExprResult->getImpurePoints()); diff --git a/src/Analyser/ExprHandler/IncludeHandler.php b/src/Analyser/ExprHandler/IncludeHandler.php index e251cd84f44..776459478aa 100644 --- a/src/Analyser/ExprHandler/IncludeHandler.php +++ b/src/Analyser/ExprHandler/IncludeHandler.php @@ -45,10 +45,10 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type return new MixedType(); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $identifier = in_array($expr->type, [Include_::TYPE_INCLUDE, Include_::TYPE_INCLUDE_ONCE], true) ? 'include' : 'require'; $scope = $exprResult->getScope()->afterExtractCall()->invalidateVolatileExpressions(); diff --git a/src/Analyser/ExprHandler/InstanceofHandler.php b/src/Analyser/ExprHandler/InstanceofHandler.php index b5288696912..e33ffd1e25a 100644 --- a/src/Analyser/ExprHandler/InstanceofHandler.php +++ b/src/Analyser/ExprHandler/InstanceofHandler.php @@ -48,17 +48,17 @@ public function supports(Expr $expr): bool return $expr instanceof Instanceof_; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $exprResult->hasYield(); $throwPoints = $exprResult->getThrowPoints(); $impurePoints = $exprResult->getImpurePoints(); $isAlwaysTerminating = $exprResult->isAlwaysTerminating(); $scope = $exprResult->getScope(); if (!$expr->class instanceof Name) { - $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep()); + $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $classResult->getScope(); $hasYield = $hasYield || $classResult->hasYield(); $throwPoints = array_merge($throwPoints, $classResult->getThrowPoints()); diff --git a/src/Analyser/ExprHandler/InterpolatedStringHandler.php b/src/Analyser/ExprHandler/InterpolatedStringHandler.php index cdf2bdba301..64a8e60b321 100644 --- a/src/Analyser/ExprHandler/InterpolatedStringHandler.php +++ b/src/Analyser/ExprHandler/InterpolatedStringHandler.php @@ -44,7 +44,7 @@ public function supports(Expr $expr): bool return $expr instanceof InterpolatedString; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $hasYield = false; @@ -55,7 +55,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex if (!$part instanceof Expr) { continue; } - $partResult = $nodeScopeResolver->processExprNode($stmt, $part, $scope, $storage, $nodeCallback, $context->enterDeep()); + $partResult = $nodeScopeResolver->processExprNode($stmt, $part, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $partResult->hasYield(); $throwPoints = array_merge($throwPoints, $partResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $partResult->getImpurePoints()); diff --git a/src/Analyser/ExprHandler/IssetHandler.php b/src/Analyser/ExprHandler/IssetHandler.php index 9fc6c135a1c..02478799eb7 100644 --- a/src/Analyser/ExprHandler/IssetHandler.php +++ b/src/Analyser/ExprHandler/IssetHandler.php @@ -344,7 +344,7 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e return $types; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $hasYield = false; @@ -355,7 +355,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex foreach ($expr->vars as $var) { $nonNullabilityResult = $this->nonNullabilityHelper->ensureNonNullability($scope, $var); $scope = $nodeScopeResolver->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $var); - $varResult = $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, $context->enterDeep()); + $varResult = $nodeScopeResolver->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $varResult->getScope(); $hasYield = $hasYield || $varResult->hasYield(); $throwPoints = array_merge($throwPoints, $varResult->getThrowPoints()); @@ -379,6 +379,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $storage, new NoopNodeCallback(), $context, + null, )->getThrowPoints()); } foreach (array_reverse($expr->vars) as $var) { diff --git a/src/Analyser/ExprHandler/MatchHandler.php b/src/Analyser/ExprHandler/MatchHandler.php index a0d69d5d70d..5e1405b9d70 100644 --- a/src/Analyser/ExprHandler/MatchHandler.php +++ b/src/Analyser/ExprHandler/MatchHandler.php @@ -207,11 +207,11 @@ public function getArmScopesAndTypes(MutatingScope $scope, Match_ $expr): array return $armScopesAndTypes; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $deepContext = $context->enterDeep(); - $condResult = $nodeScopeResolver->processExprNode($stmt, $expr->cond, $scope, $storage, $nodeCallback, $deepContext); + $condResult = $nodeScopeResolver->processExprNode($stmt, $expr->cond, $scope, $storage, $nodeCallback, $deepContext, null); $condType = $condResult->getType(); $condNativeType = $condResult->getNativeType(); $scope = $condResult->getScope(); @@ -322,7 +322,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex } } - $nodeScopeResolver->processExprNode($stmt, $cond, $armConditionScope, $storage, $nodeCallback, $deepContext); + $nodeScopeResolver->processExprNode($stmt, $cond, $armConditionScope, $storage, $nodeCallback, $deepContext, null); $condNodes[] = new MatchExpressionArmCondition( $cond, @@ -359,6 +359,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $storage, $nodeCallback, ExpressionContext::createTopLevel(), + null, ); $armScope = $armResult->getScope(); if (!$armResult->isAlwaysTerminating()) { @@ -395,7 +396,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $hasDefaultCond = true; $matchArmBody = new MatchExpressionArmBody($matchScope, $arm->body); $armNodes[$i] = new MatchExpressionArm($matchArmBody, [], $arm->getStartLine()); - $armResult = $nodeScopeResolver->processExprNode($stmt, $arm->body, $matchScope, $storage, $nodeCallback, ExpressionContext::createTopLevel()); + $armResult = $nodeScopeResolver->processExprNode($stmt, $arm->body, $matchScope, $storage, $nodeCallback, ExpressionContext::createTopLevel(), null); $matchScope = $armResult->getScope(); $hasYield = $hasYield || $armResult->hasYield(); $throwPoints = array_merge($throwPoints, $armResult->getThrowPoints()); @@ -420,7 +421,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex continue; } $condNodes[] = new MatchExpressionArmCondition($armCond, $armCondScope, $armCond->getStartLine()); - $armCondResult = $nodeScopeResolver->processExprNode($stmt, $armCond, $armCondScope, $storage, $nodeCallback, $deepContext); + $armCondResult = $nodeScopeResolver->processExprNode($stmt, $armCond, $armCondScope, $storage, $nodeCallback, $deepContext, null); $hasYield = $hasYield || $armCondResult->hasYield(); $throwPoints = array_merge($throwPoints, $armCondResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $armCondResult->getImpurePoints()); @@ -451,6 +452,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $storage, $nodeCallback, ExpressionContext::createTopLevel(), + null, ); $armScope = $armResult->getScope(); if (!$armResult->isAlwaysTerminating()) { diff --git a/src/Analyser/ExprHandler/MethodCallHandler.php b/src/Analyser/ExprHandler/MethodCallHandler.php index dba59689474..82e398a0431 100644 --- a/src/Analyser/ExprHandler/MethodCallHandler.php +++ b/src/Analyser/ExprHandler/MethodCallHandler.php @@ -73,7 +73,7 @@ public function supports(Expr $expr): bool return $expr instanceof MethodCall && !$expr->isFirstClassCallable(); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $originalScope = $scope; @@ -89,7 +89,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex ); } - $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $closureCallScope ?? $scope, $storage, $nodeCallback, $context->enterDeep()); + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $closureCallScope ?? $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $varResult->hasYield(); $throwPoints = $varResult->getThrowPoints(); $impurePoints = $varResult->getImpurePoints(); @@ -114,7 +114,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex } } else { - $methodNameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep()); + $methodNameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $throwPoints = array_merge($throwPoints, $methodNameResult->getThrowPoints()); $scope = $methodNameResult->getScope(); } diff --git a/src/Analyser/ExprHandler/NewHandler.php b/src/Analyser/ExprHandler/NewHandler.php index 6f1f87ba285..fdf3679cb8a 100644 --- a/src/Analyser/ExprHandler/NewHandler.php +++ b/src/Analyser/ExprHandler/NewHandler.php @@ -96,7 +96,7 @@ public function supports(Expr $expr): bool return $expr instanceof New_ && !$expr->isFirstClassCallable(); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $parametersAcceptor = null; @@ -174,7 +174,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $isDynamic = true; $objectClasses = $scope->getType($expr)->getObjectClassNames(); if (count($objectClasses) === 1) { - $objectExprResult = $nodeScopeResolver->processExprNode($stmt, new New_(new Name($objectClasses[0])), $scope, $storage, new NoopNodeCallback(), $context->enterDeep()); + $objectExprResult = $nodeScopeResolver->processExprNode($stmt, new New_(new Name($objectClasses[0])), $scope, $storage, new NoopNodeCallback(), $context->enterDeep(), null); $className = $objectClasses[0]; $additionalThrowPoints = $objectExprResult->getThrowPoints(); } else { @@ -182,7 +182,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $additionalThrowPoints = [InternalThrowPoint::createImplicit($scope, $expr)]; } - $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep()); + $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $classResult->getScope(); $hasYield = $classResult->hasYield(); $throwPoints = $classResult->getThrowPoints(); diff --git a/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php b/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php index d7dc61b42e4..da74ea5bddf 100644 --- a/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php +++ b/src/Analyser/ExprHandler/NullsafeMethodCallHandler.php @@ -84,7 +84,7 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e return $context->true() ? $types->unionWith($nullSafeTypes) : $types->intersectWith($nullSafeTypes); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $scopeBeforeNullsafe = $scope; @@ -105,6 +105,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $storage, $nodeCallback, $context, + null, ); $scope = $this->nonNullabilityHelper->revertNonNullability($exprResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions()); diff --git a/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php b/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php index 691c19d3910..e4831a3858f 100644 --- a/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php +++ b/src/Analyser/ExprHandler/NullsafePropertyFetchHandler.php @@ -84,7 +84,7 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e return $context->true() ? $types->unionWith($nullSafeTypes) : $types->intersectWith($nullSafeTypes); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $nonNullabilityResult = $this->nonNullabilityHelper->ensureShallowNonNullability($scope, $scope, $expr->var); @@ -94,7 +94,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $expr->var, $expr->name, $attributes, - ), $nonNullabilityResult->getScope(), $storage, $nodeCallback, $context); + ), $nonNullabilityResult->getScope(), $storage, $nodeCallback, $context, null); $scope = $this->nonNullabilityHelper->revertNonNullability($exprResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions()); return $this->expressionResultFactory->create( diff --git a/src/Analyser/ExprHandler/PipeHandler.php b/src/Analyser/ExprHandler/PipeHandler.php index a10009c2d56..4a8ae31238f 100644 --- a/src/Analyser/ExprHandler/PipeHandler.php +++ b/src/Analyser/ExprHandler/PipeHandler.php @@ -63,7 +63,7 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type ])); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $rightAttributes = array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true]); unset($rightAttributes[ExprPrinter::ATTRIBUTE_CACHE_KEY]); @@ -106,7 +106,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex )); } - $callResult = $nodeScopeResolver->processExprNode($stmt, $callExpr, $scope, $storage, $nodeCallback, $context); + $callResult = $nodeScopeResolver->processExprNode($stmt, $callExpr, $scope, $storage, $nodeCallback, $context, null); return $this->expressionResultFactory->create( $callResult->getScope(), diff --git a/src/Analyser/ExprHandler/PostDecHandler.php b/src/Analyser/ExprHandler/PostDecHandler.php index ecdf3bd84d8..2edfabf71b0 100644 --- a/src/Analyser/ExprHandler/PostDecHandler.php +++ b/src/Analyser/ExprHandler/PostDecHandler.php @@ -36,9 +36,9 @@ public function supports(Expr $expr): bool return $expr instanceof PostDec; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep()); + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep(), null); return $this->expressionResultFactory->create( $nodeScopeResolver->processVirtualAssign( diff --git a/src/Analyser/ExprHandler/PostIncHandler.php b/src/Analyser/ExprHandler/PostIncHandler.php index 9a68af90336..e5abc2ed0c9 100644 --- a/src/Analyser/ExprHandler/PostIncHandler.php +++ b/src/Analyser/ExprHandler/PostIncHandler.php @@ -36,9 +36,9 @@ public function supports(Expr $expr): bool return $expr instanceof PostInc; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep()); + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep(), null); return $this->expressionResultFactory->create( $nodeScopeResolver->processVirtualAssign( diff --git a/src/Analyser/ExprHandler/PreDecHandler.php b/src/Analyser/ExprHandler/PreDecHandler.php index 6569fde8c10..59f5a6434f1 100644 --- a/src/Analyser/ExprHandler/PreDecHandler.php +++ b/src/Analyser/ExprHandler/PreDecHandler.php @@ -99,9 +99,9 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type return $scope->getType(new Minus($expr->var, new Int_(1))); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep()); + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep(), null); return $this->expressionResultFactory->create( $nodeScopeResolver->processVirtualAssign( diff --git a/src/Analyser/ExprHandler/PreIncHandler.php b/src/Analyser/ExprHandler/PreIncHandler.php index 7d4be597076..c0f83330af5 100644 --- a/src/Analyser/ExprHandler/PreIncHandler.php +++ b/src/Analyser/ExprHandler/PreIncHandler.php @@ -100,9 +100,9 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type return $scope->getType(new Plus($expr->var, new Int_(1))); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep()); + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep(), null); return $this->expressionResultFactory->create( $nodeScopeResolver->processVirtualAssign( diff --git a/src/Analyser/ExprHandler/PrintHandler.php b/src/Analyser/ExprHandler/PrintHandler.php index cd6a90aee17..cfff0e3f0a6 100644 --- a/src/Analyser/ExprHandler/PrintHandler.php +++ b/src/Analyser/ExprHandler/PrintHandler.php @@ -47,10 +47,10 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type return new ConstantIntegerType(1); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $throwPoints = $exprResult->getThrowPoints(); $impurePoints = $exprResult->getImpurePoints(); diff --git a/src/Analyser/ExprHandler/PropertyFetchHandler.php b/src/Analyser/ExprHandler/PropertyFetchHandler.php index 805b2b190d5..f11320dccc2 100644 --- a/src/Analyser/ExprHandler/PropertyFetchHandler.php +++ b/src/Analyser/ExprHandler/PropertyFetchHandler.php @@ -51,11 +51,11 @@ public function supports(Expr $expr): bool return $expr instanceof PropertyFetch; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $scopeBeforeVar = $scope; - $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep()); + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->var, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $varResult->hasYield(); $throwPoints = $varResult->getThrowPoints(); $impurePoints = $varResult->getImpurePoints(); @@ -75,7 +75,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex } } } else { - $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep()); + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $nameResult->hasYield(); $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $nameResult->getImpurePoints()); diff --git a/src/Analyser/ExprHandler/ScalarHandler.php b/src/Analyser/ExprHandler/ScalarHandler.php index 9b4de986801..b2a79e8cba6 100644 --- a/src/Analyser/ExprHandler/ScalarHandler.php +++ b/src/Analyser/ExprHandler/ScalarHandler.php @@ -41,7 +41,7 @@ public function supports(Expr $expr): bool return $expr instanceof Scalar && !$expr instanceof InterpolatedString; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { return $this->expressionResultFactory->create( $scope, diff --git a/src/Analyser/ExprHandler/StaticCallHandler.php b/src/Analyser/ExprHandler/StaticCallHandler.php index b92881a1863..33d65427373 100644 --- a/src/Analyser/ExprHandler/StaticCallHandler.php +++ b/src/Analyser/ExprHandler/StaticCallHandler.php @@ -81,7 +81,7 @@ public function supports(Expr $expr): bool return $expr instanceof StaticCall && !$expr->isFirstClassCallable(); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $hasYield = false; @@ -90,7 +90,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $isAlwaysTerminating = false; $containsNullsafe = false; if ($expr->class instanceof Expr) { - $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep()); + $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $classResult->hasYield(); $throwPoints = array_merge($throwPoints, $classResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $classResult->getImpurePoints()); @@ -175,7 +175,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex } } } else { - $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep()); + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $nameResult->hasYield(); $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $nameResult->getImpurePoints()); @@ -188,7 +188,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $objectClasses = $scope->getType(new New_($expr->class))->getObjectClassNames(); } if (count($objectClasses) === 1) { - $objectExprResult = $nodeScopeResolver->processExprNode($stmt, new StaticCall(new Name($objectClasses[0]), $expr->name, []), $scope, $storage, new NoopNodeCallback(), $context->enterDeep()); + $objectExprResult = $nodeScopeResolver->processExprNode($stmt, new StaticCall(new Name($objectClasses[0]), $expr->name, []), $scope, $storage, new NoopNodeCallback(), $context->enterDeep(), null); $additionalThrowPoints = $objectExprResult->getThrowPoints(); } else { $additionalThrowPoints = [InternalThrowPoint::createImplicit($scope, $expr)]; diff --git a/src/Analyser/ExprHandler/StaticPropertyFetchHandler.php b/src/Analyser/ExprHandler/StaticPropertyFetchHandler.php index dd15b5a6eb4..9f69b8d46c8 100644 --- a/src/Analyser/ExprHandler/StaticPropertyFetchHandler.php +++ b/src/Analyser/ExprHandler/StaticPropertyFetchHandler.php @@ -51,7 +51,7 @@ public function supports(Expr $expr): bool return $expr instanceof StaticPropertyFetch; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $hasYield = false; @@ -68,7 +68,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $isAlwaysTerminating = false; $containsNullsafe = false; if ($expr->class instanceof Expr) { - $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep()); + $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->class, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $classResult->hasYield(); $throwPoints = $classResult->getThrowPoints(); $impurePoints = $classResult->getImpurePoints(); @@ -77,7 +77,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $containsNullsafe = $classResult->containsNullsafe(); } if (!$expr->name instanceof VarLikeIdentifier) { - $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep()); + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $nameResult->hasYield(); $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $nameResult->getImpurePoints()); diff --git a/src/Analyser/ExprHandler/TernaryHandler.php b/src/Analyser/ExprHandler/TernaryHandler.php index 16350c525c1..a8d48444356 100644 --- a/src/Analyser/ExprHandler/TernaryHandler.php +++ b/src/Analyser/ExprHandler/TernaryHandler.php @@ -46,7 +46,7 @@ public function supports(Expr $expr): bool public function resolveType(MutatingScope $scope, Expr $expr): Type { - $condResult = $this->nodeScopeResolver->processExprNode(new Stmt\Expression($expr->cond), $expr->cond, $scope, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep()); + $condResult = $this->nodeScopeResolver->processExprNode(new Stmt\Expression($expr->cond), $expr->cond, $scope, new ExpressionResultStorage(), new NoopNodeCallback(), ExpressionContext::createDeep(), null); if ($expr->if === null) { $conditionType = $scope->getType($expr->cond); $booleanConditionType = $conditionType->toBoolean(); @@ -100,9 +100,9 @@ public function specifyTypes(TypeSpecifier $typeSpecifier, Scope $scope, Expr $e return $typeSpecifier->specifyTypesInCondition($scope, $conditionExpr, $context)->setRootExpr($expr); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $ternaryCondResult = $nodeScopeResolver->processExprNode($stmt, $expr->cond, $scope, $storage, $nodeCallback, $context->enterDeep()); + $ternaryCondResult = $nodeScopeResolver->processExprNode($stmt, $expr->cond, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $throwPoints = $ternaryCondResult->getThrowPoints(); $impurePoints = $ternaryCondResult->getImpurePoints(); $hasYield = $ternaryCondResult->hasYield(); @@ -111,20 +111,20 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $ifTrueType = null; if ($expr->if === null) { - $elseResult = $nodeScopeResolver->processExprNode($stmt, $expr->else, $ifFalseScope, $storage, $nodeCallback, $context); + $elseResult = $nodeScopeResolver->processExprNode($stmt, $expr->else, $ifFalseScope, $storage, $nodeCallback, $context, null); $throwPoints = array_merge($throwPoints, $elseResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $elseResult->getImpurePoints()); $hasYield = $hasYield || $elseResult->hasYield(); $ifFalseScope = $elseResult->getScope(); } else { - $ifResult = $nodeScopeResolver->processExprNode($stmt, $expr->if, $ifTrueScope, $storage, $nodeCallback, $context); + $ifResult = $nodeScopeResolver->processExprNode($stmt, $expr->if, $ifTrueScope, $storage, $nodeCallback, $context, null); $throwPoints = array_merge($throwPoints, $ifResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $ifResult->getImpurePoints()); $hasYield = $hasYield || $ifResult->hasYield(); $ifTrueScope = $ifResult->getScope(); $ifTrueType = $ifResult->getType(); - $elseResult = $nodeScopeResolver->processExprNode($stmt, $expr->else, $ifFalseScope, $storage, $nodeCallback, $context); + $elseResult = $nodeScopeResolver->processExprNode($stmt, $expr->else, $ifFalseScope, $storage, $nodeCallback, $context, null); $throwPoints = array_merge($throwPoints, $elseResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $elseResult->getImpurePoints()); $hasYield = $hasYield || $elseResult->hasYield(); diff --git a/src/Analyser/ExprHandler/ThrowHandler.php b/src/Analyser/ExprHandler/ThrowHandler.php index e9b1ce7d37a..45bc3a2bdb3 100644 --- a/src/Analyser/ExprHandler/ThrowHandler.php +++ b/src/Analyser/ExprHandler/ThrowHandler.php @@ -38,9 +38,9 @@ public function supports(Expr $expr): bool return $expr instanceof Throw_; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()->enterThrow()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()->enterThrow(), null); return $this->expressionResultFactory->create( $scope, diff --git a/src/Analyser/ExprHandler/UnaryMinusHandler.php b/src/Analyser/ExprHandler/UnaryMinusHandler.php index ea67e7dabc4..3304dbc75e3 100644 --- a/src/Analyser/ExprHandler/UnaryMinusHandler.php +++ b/src/Analyser/ExprHandler/UnaryMinusHandler.php @@ -39,9 +39,9 @@ public function supports(Expr $expr): bool return $expr instanceof UnaryMinus; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); return $this->expressionResultFactory->create( $exprResult->getScope(), diff --git a/src/Analyser/ExprHandler/UnaryPlusHandler.php b/src/Analyser/ExprHandler/UnaryPlusHandler.php index 6ec1abe38fc..f5805cdf310 100644 --- a/src/Analyser/ExprHandler/UnaryPlusHandler.php +++ b/src/Analyser/ExprHandler/UnaryPlusHandler.php @@ -39,9 +39,9 @@ public function supports(Expr $expr): bool return $expr instanceof UnaryPlus; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); return $this->expressionResultFactory->create( $exprResult->getScope(), diff --git a/src/Analyser/ExprHandler/VariableHandler.php b/src/Analyser/ExprHandler/VariableHandler.php index eff0cee6c9a..1a9078c1243 100644 --- a/src/Analyser/ExprHandler/VariableHandler.php +++ b/src/Analyser/ExprHandler/VariableHandler.php @@ -76,7 +76,7 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type return new MixedType(); } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $hasYield = false; @@ -88,7 +88,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $impurePoints[] = new ImpurePoint($scope, $expr, 'superglobal', 'access to superglobal variable', true); } } else { - $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep()); + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->name, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $hasYield = $nameResult->hasYield(); $throwPoints = $nameResult->getThrowPoints(); $impurePoints = $nameResult->getImpurePoints(); diff --git a/src/Analyser/ExprHandler/Virtual/AlwaysRememberedExprHandler.php b/src/Analyser/ExprHandler/Virtual/AlwaysRememberedExprHandler.php index 99d6d9925e8..2840e56163d 100644 --- a/src/Analyser/ExprHandler/Virtual/AlwaysRememberedExprHandler.php +++ b/src/Analyser/ExprHandler/Virtual/AlwaysRememberedExprHandler.php @@ -43,11 +43,12 @@ public function processExpr( ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, + ?Type $overriddenType, ): ExpressionResult { $beforeScope = $scope; $innerExpr = $expr->getExpr(); - $innerResult = $nodeScopeResolver->processExprNode($stmt, $innerExpr, $scope, $storage, $nodeCallback, $context); + $innerResult = $nodeScopeResolver->processExprNode($stmt, $innerExpr, $scope, $storage, $nodeCallback, $context, null); $scope = $innerResult->getScope(); return $this->expressionResultFactory->create( diff --git a/src/Analyser/ExprHandler/Virtual/ExistingArrayDimFetchHandler.php b/src/Analyser/ExprHandler/Virtual/ExistingArrayDimFetchHandler.php index 411d2ee8d65..2e832d641d5 100644 --- a/src/Analyser/ExprHandler/Virtual/ExistingArrayDimFetchHandler.php +++ b/src/Analyser/ExprHandler/Virtual/ExistingArrayDimFetchHandler.php @@ -35,7 +35,7 @@ public function supports(Expr $expr): bool return $expr instanceof ExistingArrayDimFetch; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { // because this is a virtual node handler, the caller will only be interested in the type // we don't need to process the inner expr diff --git a/src/Analyser/ExprHandler/Virtual/FunctionCallableNodeHandler.php b/src/Analyser/ExprHandler/Virtual/FunctionCallableNodeHandler.php index e56509e627e..bb40f102f36 100644 --- a/src/Analyser/ExprHandler/Virtual/FunctionCallableNodeHandler.php +++ b/src/Analyser/ExprHandler/Virtual/FunctionCallableNodeHandler.php @@ -36,7 +36,7 @@ public function supports(Expr $expr): bool return $expr instanceof FunctionCallableNode; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $throwPoints = []; @@ -44,7 +44,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $hasYield = false; $isAlwaysTerminating = false; if ($expr->getName() instanceof Expr) { - $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->getName(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->getName(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $nameResult->getScope(); $hasYield = $nameResult->hasYield(); $throwPoints = $nameResult->getThrowPoints(); diff --git a/src/Analyser/ExprHandler/Virtual/InstantiationCallableNodeHandler.php b/src/Analyser/ExprHandler/Virtual/InstantiationCallableNodeHandler.php index 937b6618d85..129391d4194 100644 --- a/src/Analyser/ExprHandler/Virtual/InstantiationCallableNodeHandler.php +++ b/src/Analyser/ExprHandler/Virtual/InstantiationCallableNodeHandler.php @@ -36,7 +36,7 @@ public function supports(Expr $expr): bool return $expr instanceof InstantiationCallableNode; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $throwPoints = []; @@ -44,7 +44,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $hasYield = false; $isAlwaysTerminating = false; if ($expr->getClass() instanceof Expr) { - $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->getClass(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->getClass(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $classResult->getScope(); $hasYield = $classResult->hasYield(); $throwPoints = $classResult->getThrowPoints(); diff --git a/src/Analyser/ExprHandler/Virtual/MethodCallableNodeHandler.php b/src/Analyser/ExprHandler/Virtual/MethodCallableNodeHandler.php index 28492541bce..502d309fd5e 100644 --- a/src/Analyser/ExprHandler/Virtual/MethodCallableNodeHandler.php +++ b/src/Analyser/ExprHandler/Virtual/MethodCallableNodeHandler.php @@ -37,17 +37,17 @@ public function supports(Expr $expr): bool return $expr instanceof MethodCallableNode; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->getVar(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $varResult = $nodeScopeResolver->processExprNode($stmt, $expr->getVar(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $varResult->getScope(); $hasYield = $varResult->hasYield(); $throwPoints = $varResult->getThrowPoints(); $impurePoints = $varResult->getImpurePoints(); $isAlwaysTerminating = false; if ($expr->getName() instanceof Expr) { - $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->getName(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->getName(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $nameResult->getScope(); $hasYield = $hasYield || $nameResult->hasYield(); $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); diff --git a/src/Analyser/ExprHandler/Virtual/NativeTypeExprHandler.php b/src/Analyser/ExprHandler/Virtual/NativeTypeExprHandler.php index 852d00b14cd..be509c911e2 100644 --- a/src/Analyser/ExprHandler/Virtual/NativeTypeExprHandler.php +++ b/src/Analyser/ExprHandler/Virtual/NativeTypeExprHandler.php @@ -35,7 +35,7 @@ public function supports(Expr $expr): bool return $expr instanceof NativeTypeExpr; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { // because this is a virtual node handler, the caller will only be interested in the type // we don't need to process the inner expr diff --git a/src/Analyser/ExprHandler/Virtual/SetExistingOffsetValueTypeExprHandler.php b/src/Analyser/ExprHandler/Virtual/SetExistingOffsetValueTypeExprHandler.php index 31f221a9e32..5b80d6b3721 100644 --- a/src/Analyser/ExprHandler/Virtual/SetExistingOffsetValueTypeExprHandler.php +++ b/src/Analyser/ExprHandler/Virtual/SetExistingOffsetValueTypeExprHandler.php @@ -35,7 +35,7 @@ public function supports(Expr $expr): bool return $expr instanceof SetExistingOffsetValueTypeExpr; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { // because this is a virtual node handler, the caller will only be interested in the type // we don't need to process the inner expr diff --git a/src/Analyser/ExprHandler/Virtual/SetOffsetValueTypeExprHandler.php b/src/Analyser/ExprHandler/Virtual/SetOffsetValueTypeExprHandler.php index ee9201224ee..abbdf58ce6b 100644 --- a/src/Analyser/ExprHandler/Virtual/SetOffsetValueTypeExprHandler.php +++ b/src/Analyser/ExprHandler/Virtual/SetOffsetValueTypeExprHandler.php @@ -35,7 +35,7 @@ public function supports(Expr $expr): bool return $expr instanceof SetOffsetValueTypeExpr; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { // because this is a virtual node handler, the caller will only be interested in the type // we don't need to process the inner expr diff --git a/src/Analyser/ExprHandler/Virtual/StaticMethodCallableNodeHandler.php b/src/Analyser/ExprHandler/Virtual/StaticMethodCallableNodeHandler.php index b12d7e120e5..69b82bcd7a9 100644 --- a/src/Analyser/ExprHandler/Virtual/StaticMethodCallableNodeHandler.php +++ b/src/Analyser/ExprHandler/Virtual/StaticMethodCallableNodeHandler.php @@ -37,7 +37,7 @@ public function supports(Expr $expr): bool return $expr instanceof StaticMethodCallableNode; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $throwPoints = []; @@ -45,7 +45,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $hasYield = false; $isAlwaysTerminating = false; if ($expr->getClass() instanceof Expr) { - $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->getClass(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $classResult = $nodeScopeResolver->processExprNode($stmt, $expr->getClass(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $classResult->getScope(); $hasYield = $classResult->hasYield(); $throwPoints = $classResult->getThrowPoints(); @@ -53,7 +53,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex $isAlwaysTerminating = $classResult->isAlwaysTerminating(); } if ($expr->getName() instanceof Expr) { - $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->getName(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $nameResult = $nodeScopeResolver->processExprNode($stmt, $expr->getName(), $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $nameResult->getScope(); $hasYield = $hasYield || $nameResult->hasYield(); $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); diff --git a/src/Analyser/ExprHandler/Virtual/TypeExprHandler.php b/src/Analyser/ExprHandler/Virtual/TypeExprHandler.php index 6ca636fe081..c1fe6be22a2 100644 --- a/src/Analyser/ExprHandler/Virtual/TypeExprHandler.php +++ b/src/Analyser/ExprHandler/Virtual/TypeExprHandler.php @@ -35,7 +35,7 @@ public function supports(Expr $expr): bool return $expr instanceof TypeExpr; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { // because this is a virtual node handler, the caller will only be interested in the type // we don't need to process the inner expr diff --git a/src/Analyser/ExprHandler/Virtual/UnsetOffsetExprHandler.php b/src/Analyser/ExprHandler/Virtual/UnsetOffsetExprHandler.php index d414e648fd2..0556490cf68 100644 --- a/src/Analyser/ExprHandler/Virtual/UnsetOffsetExprHandler.php +++ b/src/Analyser/ExprHandler/Virtual/UnsetOffsetExprHandler.php @@ -35,7 +35,7 @@ public function supports(Expr $expr): bool return $expr instanceof UnsetOffsetExpr; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { // because this is a virtual node handler, the caller will only be interested in the type // we don't need to process the inner expr diff --git a/src/Analyser/ExprHandler/YieldFromHandler.php b/src/Analyser/ExprHandler/YieldFromHandler.php index 1aac8244af7..b36e6d9cb1a 100644 --- a/src/Analyser/ExprHandler/YieldFromHandler.php +++ b/src/Analyser/ExprHandler/YieldFromHandler.php @@ -52,10 +52,10 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type return $generatorReturnType; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; - $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $nodeScopeResolver->processExprNode($stmt, $expr->expr, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $exprResult->getScope(); return $this->expressionResultFactory->create( diff --git a/src/Analyser/ExprHandler/YieldHandler.php b/src/Analyser/ExprHandler/YieldHandler.php index 07abbc7e6ee..b602bcab54e 100644 --- a/src/Analyser/ExprHandler/YieldHandler.php +++ b/src/Analyser/ExprHandler/YieldHandler.php @@ -57,7 +57,7 @@ public function resolveType(MutatingScope $scope, Expr $expr): Type return $generatorSendType; } - public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Expr $expr, MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { $beforeScope = $scope; $throwPoints = [ @@ -74,14 +74,14 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex ]; $isAlwaysTerminating = false; if ($expr->key !== null) { - $keyResult = $nodeScopeResolver->processExprNode($stmt, $expr->key, $scope, $storage, $nodeCallback, $context->enterDeep()); + $keyResult = $nodeScopeResolver->processExprNode($stmt, $expr->key, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $keyResult->getScope(); $throwPoints = $keyResult->getThrowPoints(); $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); $isAlwaysTerminating = $keyResult->isAlwaysTerminating(); } if ($expr->value !== null) { - $valueResult = $nodeScopeResolver->processExprNode($stmt, $expr->value, $scope, $storage, $nodeCallback, $context->enterDeep()); + $valueResult = $nodeScopeResolver->processExprNode($stmt, $expr->value, $scope, $storage, $nodeCallback, $context->enterDeep(), null); $scope = $valueResult->getScope(); $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints()); diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 9df08b89aeb..74c3255ea0b 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -62,6 +62,7 @@ use PHPStan\DependencyInjection\AutowiredService; use PHPStan\DependencyInjection\Container; use PHPStan\DependencyInjection\ExtensionsCollection; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\File\FileHelper; use PHPStan\File\FileReader; use PHPStan\Node\BreaklessWhileLoopNode; @@ -249,6 +250,7 @@ public function __construct( private readonly ExtensionsCollection $methodParameterClosureThisExtensions, #[AutowiredExtensions(of: StaticMethodParameterClosureThisExtension::class)] private readonly ExtensionsCollection $staticMethodParameterClosureThisExtensions, + private readonly DynamicParameterTypeExtensionProvider $dynamicParameterTypeExtensionProvider, #[AutowiredExtensions(of: FunctionParameterClosureTypeExtension::class)] private readonly ExtensionsCollection $functionParameterClosureTypeExtensions, #[AutowiredExtensions(of: MethodParameterClosureTypeExtension::class)] @@ -1078,7 +1080,7 @@ public function processStmtNode( $impurePoints = []; $isAlwaysTerminating = false; foreach ($stmt->exprs as $echoExpr) { - $result = $this->processExprNode($stmt, $echoExpr, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $result = $this->processExprNode($stmt, $echoExpr, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); $toStringResult = $this->implicitToStringCallHelper->processImplicitToStringCall($echoExpr, $scope); @@ -1094,7 +1096,7 @@ public function processStmtNode( return new InternalStatementResult($scope, $hasYield, $isAlwaysTerminating, [], $throwPoints, $impurePoints); } elseif ($stmt instanceof Return_) { if ($stmt->expr !== null) { - $result = $this->processExprNode($stmt, $stmt->expr, $stmtScope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $result = $this->processExprNode($stmt, $stmt->expr, $stmtScope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $scope = $result->getScope(); @@ -1110,7 +1112,7 @@ public function processStmtNode( ], $overridingThrowPoints ?? $throwPoints, $impurePoints); } elseif ($stmt instanceof Continue_ || $stmt instanceof Break_) { if ($stmt->num !== null) { - $result = $this->processExprNode($stmt, $stmt->num, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $result = $this->processExprNode($stmt, $stmt->num, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); @@ -1153,7 +1155,7 @@ public function processStmtNode( } $hasAssign = true; - }, $nodeCallback), ExpressionContext::createTopLevel()); + }, $nodeCallback), ExpressionContext::createTopLevel(), null); $throwPoints = array_filter($result->getThrowPoints(), static fn ($throwPoint) => $throwPoint->isExplicit()); if ( count($result->getImpurePoints()) === 0 @@ -1281,7 +1283,7 @@ public function processStmtNode( foreach ($stmt->props as $prop) { $this->callNodeCallback($nodeCallback, $prop, $scope, $storage); if ($prop->default !== null) { - $this->processExprNode($stmt, $prop->default, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $this->processExprNode($stmt, $prop->default, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); } if (!$scope->isInClass()) { @@ -1341,7 +1343,7 @@ public function processStmtNode( $this->callNodeCallback($nodeCallback, $stmt->type, $scope, $storage); } } elseif ($stmt instanceof If_) { - $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $conditionType = ($this->treatPhpDocTypesAsCertain ? $condResult->getType() : $condResult->getNativeType())->toBoolean(); $ifAlwaysTrue = $conditionType->isTrue()->yes(); $exitPoints = []; @@ -1377,7 +1379,7 @@ public function processStmtNode( $condScope = $scope; foreach ($stmt->elseifs as $elseif) { $this->callNodeCallback($nodeCallback, $elseif, $scope, $storage); - $condResult = $this->processExprNode($stmt, $elseif->cond, $condScope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $elseif->cond, $condScope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $elseIfConditionType = ($this->treatPhpDocTypesAsCertain ? $condResult->getType() : $condResult->getNativeType())->toBoolean(); $throwPoints = array_merge($throwPoints, $condResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $condResult->getImpurePoints()); @@ -1463,7 +1465,7 @@ public function processStmtNode( if ($stmt->expr instanceof Variable && is_string($stmt->expr->name)) { $scope = $this->processVarAnnotation($scope, [$stmt->expr->name], $stmt); } - $condResult = $this->processExprNode($stmt, $stmt->expr, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $stmt->expr, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $throwPoints = $overridingThrowPoints ?? $condResult->getThrowPoints(); $impurePoints = $condResult->getImpurePoints(); $scope = $condResult->getScope(); @@ -1727,7 +1729,7 @@ public function processStmtNode( } elseif ($stmt instanceof While_) { $originalStorage = $storage; $storage = $originalStorage->duplicate(); - $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep(), null); $beforeCondBooleanType = ($this->treatPhpDocTypesAsCertain ? $condResult->getType() : $condResult->getNativeType())->toBoolean(); $condScope = $condResult->getFalseyScope(); if (!$context->isTopLevel() && $beforeCondBooleanType->isFalse()->yes()) { @@ -1752,7 +1754,7 @@ public function processStmtNode( $prevScope = $bodyScope; $bodyScope = $bodyScope->mergeWith($scope); $storage = $originalStorage->duplicate(); - $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep())->getTruthyScope(); + $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep(), null)->getTruthyScope(); $bodyScopeResult = $this->processStmtNodesInternal($stmt, $stmt->stmts, $bodyScope, $storage, new NoopNodeCallback(), $context->enterDeep())->filterOutLoopExitPoints(); $bodyScope = $bodyScopeResult->getScope(); foreach ($bodyScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) { @@ -1772,7 +1774,7 @@ public function processStmtNode( $bodyScope = $bodyScope->mergeWith($scope); $bodyScopeMaybeRan = $bodyScope; $storage = $originalStorage; - $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope(); + $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep(), null)->getTruthyScope(); $finalScopeResult = $this->processStmtNodesInternal($stmt, $stmt->stmts, $bodyScope, $storage, $nodeCallback, $context)->filterOutLoopExitPoints(); $finalScope = $finalScopeResult->getScope()->filterByFalseyValue($stmt->cond); @@ -1854,7 +1856,7 @@ public function processStmtNode( foreach ($bodyScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) { $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); } - $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep())->getTruthyScope(); + $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep(), null)->getTruthyScope(); if ($bodyScope->equals($prevScope)) { break; } @@ -1893,13 +1895,13 @@ public function processStmtNode( $finalScope = $scope; } if (!$alwaysTerminating) { - $condResult = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $hasYield = $condResult->hasYield(); $throwPoints = $condResult->getThrowPoints(); $impurePoints = $condResult->getImpurePoints(); $finalScope = $condResult->getFalseyScope(); } else { - $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $this->processExprNode($stmt, $stmt->cond, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); } $breakExitPoints = $bodyScopeResult->getExitPointsByType(Break_::class); @@ -1925,7 +1927,7 @@ public function processStmtNode( $throwPoints = []; $impurePoints = []; foreach ($stmt->init as $initExpr) { - $initResult = $this->processExprNode($stmt, $initExpr, $initScope, $storage, $nodeCallback, ExpressionContext::createTopLevel()); + $initResult = $this->processExprNode($stmt, $initExpr, $initScope, $storage, $nodeCallback, ExpressionContext::createTopLevel(), null); $initScope = $initResult->getScope(); $hasYield = $hasYield || $initResult->hasYield(); $throwPoints = array_merge($throwPoints, $initResult->getThrowPoints()); @@ -1941,7 +1943,7 @@ public function processStmtNode( $storage = $originalStorage->duplicate(); foreach ($stmt->cond as $condExpr) { - $condResult = $this->processExprNode($stmt, $condExpr, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $condExpr, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep(), null); $initScope = $condResult->getScope(); // only the last condition expression is relevant whether the loop continues @@ -1965,7 +1967,7 @@ public function processStmtNode( $storage = $originalStorage->duplicate(); $bodyScope = $bodyScope->mergeWith($initScope); if ($lastCondExpr !== null) { - $bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep())->getTruthyScope(); + $bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createDeep(), null)->getTruthyScope(); } $bodyScopeResult = $this->processStmtNodesInternal($stmt, $stmt->stmts, $bodyScope, $storage, new NoopNodeCallback(), $context->enterDeep())->filterOutLoopExitPoints(); $bodyScope = $bodyScopeResult->getScope(); @@ -1974,7 +1976,7 @@ public function processStmtNode( } foreach ($stmt->loop as $loopExpr) { - $exprResult = $this->processExprNode($stmt, $loopExpr, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createTopLevel()); + $exprResult = $this->processExprNode($stmt, $loopExpr, $bodyScope, $storage, new NoopNodeCallback(), ExpressionContext::createTopLevel(), null); $bodyScope = $exprResult->getScope(); $hasYield = $hasYield || $exprResult->hasYield(); $throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints()); @@ -1998,7 +2000,7 @@ public function processStmtNode( $alwaysIterates = TrinaryLogic::createFromBoolean($context->isTopLevel()); if ($lastCondExpr !== null) { $alwaysIterates = $alwaysIterates->and($bodyScope->getType($lastCondExpr)->toBoolean()->isTrue()); - $bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope(); + $bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $storage, $nodeCallback, ExpressionContext::createDeep(), null)->getTruthyScope(); $bodyScope = $this->inferForLoopExpressions($stmt, $lastCondExpr, $bodyScope); } @@ -2010,7 +2012,7 @@ public function processStmtNode( $loopScope = $finalScope; foreach ($stmt->loop as $loopExpr) { - $loopScope = $this->processExprNode($stmt, $loopExpr, $loopScope, $storage, $nodeCallback, ExpressionContext::createTopLevel())->getScope(); + $loopScope = $this->processExprNode($stmt, $loopExpr, $loopScope, $storage, $nodeCallback, ExpressionContext::createTopLevel(), null)->getScope(); } $finalScope = $finalScope->generalizeWith($loopScope); @@ -2063,7 +2065,7 @@ public function processStmtNode( array_merge($impurePoints, $finalScopeResult->getImpurePoints()), ); } elseif ($stmt instanceof Switch_) { - $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $condResult->getScope(); $scopeForBranches = $scope; $finalScope = null; @@ -2079,7 +2081,7 @@ public function processStmtNode( if ($caseNode->cond !== null) { $condExpr = new BinaryOp\Equal($stmt->cond, $caseNode->cond); $fullCondExpr = $fullCondExpr === null ? $condExpr : new BooleanOr($fullCondExpr, $condExpr); - $caseResult = $this->processExprNode($stmt, $caseNode->cond, $scopeForBranches, $storage, $nodeCallback, ExpressionContext::createDeep()); + $caseResult = $this->processExprNode($stmt, $caseNode->cond, $scopeForBranches, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scopeForBranches = $caseResult->getScope(); $hasYield = $hasYield || $caseResult->hasYield(); $throwPoints = array_merge($throwPoints, $caseResult->getThrowPoints()); @@ -2367,7 +2369,7 @@ public function processStmtNode( $impurePoints = []; foreach ($stmt->vars as $var) { $scope = $this->lookForSetAllowedUndefinedExpressions($scope, $var); - $exprResult = $this->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $exprResult = $this->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $exprResult->getScope(); $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $var); $hasYield = $hasYield || $exprResult->hasYield(); @@ -2383,6 +2385,7 @@ public function processStmtNode( $storage, new NoopNodeCallback(), ExpressionContext::createDeep(), + null, )->getThrowPoints()); } @@ -2445,7 +2448,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch throw new ShouldNotHappenException(); } $scope = $this->lookForSetAllowedUndefinedExpressions($scope, $var); - $varResult = $this->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $varResult = $this->processExprNode($stmt, $var, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $varResult->getImpurePoints()); $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $var); @@ -2478,12 +2481,12 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch } if ($var->default !== null) { - $defaultExprResult = $this->processExprNode($stmt, $var->default, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $defaultExprResult = $this->processExprNode($stmt, $var->default, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $defaultExprResult->getImpurePoints()); } $scope = $scope->enterExpressionAssign($var->var); - $varResult = $this->processExprNode($stmt, $var->var, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $varResult = $this->processExprNode($stmt, $var->var, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $varResult->getImpurePoints()); $scope = $scope->exitExpressionAssign($var->var); @@ -2498,7 +2501,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch $impurePoints = []; foreach ($stmt->consts as $const) { $this->callNodeCallback($nodeCallback, $const, $scope, $storage); - $constResult = $this->processExprNode($stmt, $const->value, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $constResult = $this->processExprNode($stmt, $const->value, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $constResult->getImpurePoints()); if ($const->namespacedName !== null) { $constantName = new Name\FullyQualified($const->namespacedName->toString()); @@ -2514,7 +2517,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch $this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $storage, $nodeCallback); foreach ($stmt->consts as $const) { $this->callNodeCallback($nodeCallback, $const, $scope, $storage); - $constResult = $this->processExprNode($stmt, $const->value, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $constResult = $this->processExprNode($stmt, $const->value, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $constResult->getImpurePoints()); if ($scope->getClassReflection() === null) { throw new ShouldNotHappenException(); @@ -2531,7 +2534,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch $this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $storage, $nodeCallback); $impurePoints = []; if ($stmt->expr !== null) { - $exprResult = $this->processExprNode($stmt, $stmt->expr, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $exprResult = $this->processExprNode($stmt, $stmt->expr, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = $exprResult->getImpurePoints(); } } elseif ($stmt instanceof InlineHTML) { @@ -2765,6 +2768,7 @@ public function processExprNode( ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, + ?Type $overriddenType, ): ExpressionResult { if ($expr instanceof Expr\CallLike && $expr->isFirstClassCallable()) { @@ -2780,7 +2784,7 @@ public function processExprNode( throw new ShouldNotHappenException(); } - $newExprResult = $this->processExprNode($stmt, $newExpr, $scope, $storage, $nodeCallback, $context); + $newExprResult = $this->processExprNode($stmt, $newExpr, $scope, $storage, $nodeCallback, $context, null); $expressionResult = $this->expressionResultFactory->create( $newExprResult->getScope(), beforeScope: $scope, @@ -2798,7 +2802,7 @@ public function processExprNode( $exprHandler = ExprHandlerRegistry::resolve($expr, $this->container); if ($exprHandler !== null) { - $expressionResult = $exprHandler->processExpr($this, $stmt, $expr, $scope, $storage, $nodeCallback, $context); + $expressionResult = $exprHandler->processExpr($this, $stmt, $expr, $scope, $storage, $nodeCallback, $context, $overriddenType); $this->storeExpressionResult($storage, $expr, $expressionResult); return $expressionResult; } @@ -2943,8 +2947,8 @@ public function processClosureNode( ExpressionResultStorage $storage, callable $nodeCallback, ExpressionContext $context, - ?Type $passedToType, - ?Type $nativePassedToType = null, + ?Type $overriddenType, + ?Type $nativeOverriddenType = null, ): ProcessClosureResult { foreach ($expr->params as $param) { @@ -2954,8 +2958,8 @@ public function processClosureNode( $byRefUses = []; $closureCallArgs = $expr->getAttribute(ClosureArgVisitor::ATTRIBUTE_NAME); - $callableParameters = $this->createCallableParameters($scope, $expr, $closureCallArgs, $passedToType); - $nativeCallableParameters = $this->createNativeCallableParameters($scope, $expr, $closureCallArgs, $nativePassedToType); + $callableParameters = $this->createCallableParameters($scope, $expr, $closureCallArgs, $overriddenType); + $nativeCallableParameters = $this->createNativeCallableParameters($scope, $expr, $closureCallArgs, $nativeOverriddenType); $useScope = $scope; foreach ($expr->uses as $use) { @@ -2994,7 +2998,7 @@ public function processClosureNode( $scope = $scope->assignVariable($inAssignRightSideVariableName, $variableType, $variableNativeType, TrinaryLogic::createYes()); } } - $this->processExprNode($stmt, $use->var, $useScope, $storage, $nodeCallback, $context); + $this->processExprNode($stmt, $use->var, $useScope, $storage, $nodeCallback, $context, null); if (!$use->byRef) { continue; } @@ -3063,6 +3067,7 @@ public function processClosureNode( $publicStatementResult, $executionEnds, array_merge($publicStatementResult->getImpurePoints(), $closureImpurePoints), + $overriddenType, ), $closureScope, $storage); return new ProcessClosureResult($scope, $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions); @@ -3113,6 +3118,7 @@ public function processClosureNode( $publicStatementResult, $executionEnds, array_merge($publicStatementResult->getImpurePoints(), $closureImpurePoints), + $overriddenType, ), $closureScope, $storage); return new ProcessClosureResult($scope, $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions, $closureResultScope, $byRefUses); @@ -3151,8 +3157,8 @@ public function processArrowFunctionNode( MutatingScope $scope, ExpressionResultStorage $storage, callable $nodeCallback, - ?Type $passedToType, - ?Type $nativePassedToType = null, + ?Type $overriddenType, + ?Type $nativeOverriddenType = null, ): ExpressionResult { foreach ($expr->params as $param) { @@ -3163,15 +3169,15 @@ public function processArrowFunctionNode( } $arrowFunctionCallArgs = $expr->getAttribute(ArrowFunctionArgVisitor::ATTRIBUTE_NAME); - $callableParameters = $this->createCallableParameters($scope, $expr, $arrowFunctionCallArgs, $passedToType); - $nativeCallableParameters = $this->createNativeCallableParameters($scope, $expr, $arrowFunctionCallArgs, $nativePassedToType); + $callableParameters = $this->createCallableParameters($scope, $expr, $arrowFunctionCallArgs, $overriddenType); + $nativeCallableParameters = $this->createNativeCallableParameters($scope, $expr, $arrowFunctionCallArgs, $nativeOverriddenType); $arrowFunctionScope = $scope->enterArrowFunction($expr, $callableParameters, $nativeCallableParameters); $arrowFunctionType = $arrowFunctionScope->getAnonymousFunctionReflection(); if ($arrowFunctionType === null) { throw new ShouldNotHappenException(); } - $this->callNodeCallback($nodeCallback, new InArrowFunctionNode($arrowFunctionType, $expr), $arrowFunctionScope, $storage); - $exprResult = $this->processExprNode($stmt, $expr->expr, $arrowFunctionScope, $storage, $nodeCallback, ExpressionContext::createTopLevel()); + $this->callNodeCallback($nodeCallback, new InArrowFunctionNode($arrowFunctionType, $expr, $overriddenType), $arrowFunctionScope, $storage); + $exprResult = $this->processExprNode($stmt, $expr->expr, $arrowFunctionScope, $storage, $nodeCallback, ExpressionContext::createTopLevel(), null); return $this->expressionResultFactory->create($scope, beforeScope: $scope, expr: $expr, hasYield: false, isAlwaysTerminating: $exprResult->isAlwaysTerminating(), throwPoints: $exprResult->getThrowPoints(), impurePoints: $exprResult->getImpurePoints()); } @@ -3180,18 +3186,18 @@ public function processArrowFunctionNode( * @param Node\Arg[]|null $args * @return ParameterReflection[]|null */ - public function createCallableParameters(Scope $scope, Expr $closureExpr, ?array $args, ?Type $passedToType): ?array + public function createCallableParameters(Scope $scope, Expr $closureExpr, ?array $args, ?Type $overriddenType): ?array { - return $this->doCreateCallableParameters($scope, $closureExpr, $args, $passedToType, static fn (Scope $s, Expr $e) => $s->getType($e)); + return $this->doCreateCallableParameters($scope, $closureExpr, $args, $overriddenType, static fn (Scope $s, Expr $e) => $s->getType($e)); } /** * @param Node\Arg[]|null $args * @return ParameterReflection[]|null */ - public function createNativeCallableParameters(Scope $scope, Expr $closureExpr, ?array $args, ?Type $nativePassedToType): ?array + public function createNativeCallableParameters(Scope $scope, Expr $closureExpr, ?array $args, ?Type $nativeOverriddenType): ?array { - return $this->doCreateCallableParameters($scope, $closureExpr, $args, $nativePassedToType, static fn (Scope $s, Expr $e) => $s->getNativeType($e)); + return $this->doCreateCallableParameters($scope, $closureExpr, $args, $nativeOverriddenType, static fn (Scope $s, Expr $e) => $s->getNativeType($e)); } /** @@ -3199,7 +3205,7 @@ public function createNativeCallableParameters(Scope $scope, Expr $closureExpr, * @param Closure(Scope, Expr): Type $typeGetter * @return ParameterReflection[]|null */ - private function doCreateCallableParameters(Scope $scope, Expr $closureExpr, ?array $args, ?Type $passedToType, Closure $typeGetter): ?array + private function doCreateCallableParameters(Scope $scope, Expr $closureExpr, ?array $args, ?Type $overriddenType, Closure $typeGetter): ?array { $callableParameters = null; if ($args !== null) { @@ -3238,16 +3244,16 @@ private function doCreateCallableParameters(Scope $scope, Expr $closureExpr, ?ar ); } } - } elseif ($passedToType !== null && !$passedToType->isCallable()->no()) { - if ($passedToType instanceof UnionType) { - $passedToType = $passedToType->filterTypes(static fn (Type $innerType) => $innerType->isCallable()->yes()); + } elseif ($overriddenType !== null && !$overriddenType->isCallable()->no()) { + if ($overriddenType instanceof UnionType) { + $overriddenType = $overriddenType->filterTypes(static fn (Type $innerType) => $innerType->isCallable()->yes()); - if ($passedToType->isCallable()->no()) { + if ($overriddenType->isCallable()->no()) { return null; } } - $acceptors = $passedToType->getCallableParametersAcceptors($scope); + $acceptors = $overriddenType->getCallableParametersAcceptors($scope); foreach ($acceptors as $acceptor) { $acceptorParameters = array_map(static fn (ParameterReflection $callableParameter) => new NativeParameterReflection( $callableParameter->getName(), @@ -3306,7 +3312,7 @@ private function processParamNode( return; } - $this->processExprNode($stmt, $param->default, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $this->processExprNode($stmt, $param->default, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); } /** @@ -3343,7 +3349,7 @@ private function processAttributeGroups( } foreach ($attr->args as $arg) { - $this->processExprNode($stmt, $arg->value, $scope, $storage, $nodeCallback, ExpressionContext::createDeep()); + $this->processExprNode($stmt, $arg->value, $scope, $storage, $nodeCallback, ExpressionContext::createDeep(), null); $this->callNodeCallback($nodeCallback, $arg, $scope, $storage); } $this->callNodeCallback($nodeCallback, $attr, $scope, $storage); @@ -3585,6 +3591,7 @@ public function processArgs( $assignByReference = false; $parameter = null; $parameterType = null; + $overwritingParameterType = null; $parameterNativeType = null; if ($parameters !== null) { $matchedParameter = null; @@ -3619,6 +3626,13 @@ public function processArgs( } } + if ($parameter !== null && $calleeReflection !== null) { + $overwritingParameterType = $this->getParameterTypeFromDynamicParameterTypeExtension($callLike, $calleeReflection, $parameter, $scope); + if ($overwritingParameterType !== null) { + $parameterType = $overwritingParameterType; + } + } + $lookForUnset = false; if ($assignByReference) { $isBuiltin = false; @@ -3665,6 +3679,7 @@ public function processArgs( } } + // @todo remove once the closure type extensions are removed if ($parameter !== null) { $overwritingParameterType = $this->getParameterTypeFromParameterClosureTypeExtension($callLike, $calleeReflection, $parameter, $scopeToPass); @@ -3740,6 +3755,7 @@ public function processArgs( } } + // @todo remove once the closure type extensions are removed if ($parameter !== null) { $overwritingParameterType = $this->getParameterTypeFromParameterClosureTypeExtension($callLike, $calleeReflection, $parameter, $scopeToPass); @@ -3767,7 +3783,7 @@ public function processArgs( if ($enterExpressionAssignForByRef) { $scopeToPass = $scopeToPass->enterExpressionAssign($arg->value); } - $exprResult = $this->processExprNode($stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $context->enterDeep()); + $exprResult = $this->processExprNode($stmt, $arg->value, $scopeToPass, $storage, $nodeCallback, $context->enterDeep(), $parameterType); $throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints()); $isAlwaysTerminating = $isAlwaysTerminating || $exprResult->isAlwaysTerminating(); @@ -3943,6 +3959,57 @@ private function shouldInvalidateCallbackExpressions(?ParameterReflection $param return true; } + /** + * @param MethodReflection|FunctionReflection|null $calleeReflection + */ + private function getParameterTypeFromDynamicParameterTypeExtension(CallLike $callLike, $calleeReflection, ParameterReflection $parameter, MutatingScope $scope): ?Type + { + if ($callLike instanceof FuncCall && $calleeReflection instanceof FunctionReflection) { + foreach ($this->dynamicParameterTypeExtensionProvider->getDynamicFunctionParameterTypeExtensions() as $dynamicFunctionParameterTypeExtension) { + if (!$dynamicFunctionParameterTypeExtension->isFunctionSupported($calleeReflection, $parameter)) { + continue; + } + $type = $dynamicFunctionParameterTypeExtension->getTypeFromFunctionCall($calleeReflection, $callLike, $parameter, $scope); + if ($type !== null) { + return $type; + } + } + } elseif ($callLike instanceof StaticCall && $calleeReflection instanceof MethodReflection) { + foreach ($this->dynamicParameterTypeExtensionProvider->getDynamicStaticMethodParameterTypeExtensions() as $dynamicStaticMethodParameterTypeExtension) { + if (!$dynamicStaticMethodParameterTypeExtension->isStaticMethodSupported($calleeReflection, $parameter)) { + continue; + } + $type = $dynamicStaticMethodParameterTypeExtension->getTypeFromStaticMethodCall($calleeReflection, $callLike, $parameter, $scope); + if ($type !== null) { + return $type; + } + } + } elseif ($callLike instanceof New_ && $callLike->class instanceof Name && $calleeReflection instanceof MethodReflection) { + $staticCall = new StaticCall($callLike->class, new Identifier('__construct'), $callLike->getArgs()); + foreach ($this->dynamicParameterTypeExtensionProvider->getDynamicStaticMethodParameterTypeExtensions() as $dynamicStaticMethodParameterTypeExtension) { + if (!$dynamicStaticMethodParameterTypeExtension->isStaticMethodSupported($calleeReflection, $parameter)) { + continue; + } + $type = $dynamicStaticMethodParameterTypeExtension->getTypeFromStaticMethodCall($calleeReflection, $staticCall, $parameter, $scope); + if ($type !== null) { + return $type; + } + } + } elseif ($callLike instanceof MethodCall && $calleeReflection instanceof MethodReflection) { + foreach ($this->dynamicParameterTypeExtensionProvider->getDynamicMethodParameterTypeExtensions() as $dynamicMethodParameterTypeExtension) { + if (!$dynamicMethodParameterTypeExtension->isMethodSupported($calleeReflection, $parameter)) { + continue; + } + $type = $dynamicMethodParameterTypeExtension->getTypeFromMethodCall($calleeReflection, $callLike, $parameter, $scope); + if ($type !== null) { + return $type; + } + } + } + + return null; + } + /** * @param MethodReflection|FunctionReflection|null $calleeReflection */ diff --git a/src/DependencyInjection/Type/DynamicParameterTypeExtensionProvider.php b/src/DependencyInjection/Type/DynamicParameterTypeExtensionProvider.php new file mode 100644 index 00000000000..c6bc6fd2a21 --- /dev/null +++ b/src/DependencyInjection/Type/DynamicParameterTypeExtensionProvider.php @@ -0,0 +1,21 @@ +container->getServicesByTag(self::FUNCTION_TAG); + } + + public function getDynamicMethodParameterTypeExtensions(): array + { + return $this->container->getServicesByTag(self::METHOD_TAG); + } + + public function getDynamicStaticMethodParameterTypeExtensions(): array + { + return $this->container->getServicesByTag(self::STATIC_METHOD_TAG); + } + +} diff --git a/src/Node/ClosureReturnStatementsNode.php b/src/Node/ClosureReturnStatementsNode.php index 87b63a60a4d..8af87b65a78 100644 --- a/src/Node/ClosureReturnStatementsNode.php +++ b/src/Node/ClosureReturnStatementsNode.php @@ -10,6 +10,7 @@ use PhpParser\NodeAbstract; use PHPStan\Analyser\ImpurePoint; use PHPStan\Analyser\StatementResult; +use PHPStan\Type\Type; use function count; /** @@ -33,6 +34,7 @@ public function __construct( private StatementResult $statementResult, private array $executionEnds, private array $impurePoints, + private ?Type $overriddenType = null, ) { parent::__construct($closureExpr->getAttributes()); @@ -84,6 +86,11 @@ public function returnsByRef(): bool return $this->closureExpr->byRef; } + public function getOverriddenType(): ?Type + { + return $this->overriddenType; + } + #[Override] public function getType(): string { diff --git a/src/Node/InArrowFunctionNode.php b/src/Node/InArrowFunctionNode.php index 6876978cece..c92fb487d59 100644 --- a/src/Node/InArrowFunctionNode.php +++ b/src/Node/InArrowFunctionNode.php @@ -7,6 +7,7 @@ use PhpParser\Node\Expr\ArrowFunction; use PhpParser\NodeAbstract; use PHPStan\Type\ClosureType; +use PHPStan\Type\Type; /** * @api @@ -16,7 +17,11 @@ final class InArrowFunctionNode extends NodeAbstract implements VirtualNode private Node\Expr\ArrowFunction $originalNode; - public function __construct(private ClosureType $closureType, ArrowFunction $originalNode) + public function __construct( + private ClosureType $closureType, + ArrowFunction $originalNode, + private ?Type $overriddenType = null, + ) { parent::__construct($originalNode->getAttributes()); $this->originalNode = $originalNode; @@ -32,6 +37,11 @@ public function getOriginalNode(): Node\Expr\ArrowFunction return $this->originalNode; } + public function getOverriddenType(): ?Type + { + return $this->overriddenType; + } + #[Override] public function getType(): string { diff --git a/src/Rules/FunctionCallParametersCheck.php b/src/Rules/FunctionCallParametersCheck.php index 0ba5e1a7a57..c31a94a9749 100644 --- a/src/Rules/FunctionCallParametersCheck.php +++ b/src/Rules/FunctionCallParametersCheck.php @@ -10,8 +10,11 @@ use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\AutowiredService; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Reflection\ConstantReflection; use PHPStan\Reflection\ExtendedParameterReflection; +use PHPStan\Reflection\FunctionReflection; +use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\ParameterReflection; use PHPStan\Reflection\ParametersAcceptor; use PHPStan\Reflection\ReflectionProvider; @@ -55,6 +58,7 @@ public function __construct( private UnresolvableTypeHelper $unresolvableTypeHelper, private PropertyReflectionFinder $propertyReflectionFinder, private ReflectionProvider $reflectionProvider, + private DynamicParameterTypeExtensionProvider $dynamicParameterTypeExtensionProvider, #[AutowiredParameter(ref: '%checkFunctionArgumentTypes%')] private bool $checkArgumentTypes, #[AutowiredParameter] @@ -98,6 +102,7 @@ public function check( string $exclusiveConstantsMessage, string $bitmaskNotAllowedMessage, ?array $renamedNamedArgumentParameterData, + MethodReflection|FunctionReflection|null $calleeReflection = null, ): array { if ($funcCall instanceof Node\Expr\MethodCall || $funcCall instanceof Node\Expr\StaticCall || $funcCall instanceof Node\Expr\FuncCall) { @@ -391,6 +396,13 @@ public function check( if ($this->checkArgumentTypes) { $parameterType = TypeUtils::resolveLateResolvableTypes($parameter->getType()); + if (! $funcCall instanceof Node\Expr\New_) { + $overriddenType = $this->getParameterTypeFromDynamicExtension($funcCall, $calleeReflection, $parameter, $scope); + if ($overriddenType !== null) { + $parameterType = $overriddenType; + } + } + if ( !$parameter->passedByReference()->createsNewVariable() || (!$isBuiltin && !$argumentValueType instanceof ErrorType) @@ -895,4 +907,50 @@ private function callReturnsByReference(Expr $expr, Scope $scope): bool return false; } + private function getParameterTypeFromDynamicExtension( + Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall $funcCall, + MethodReflection|FunctionReflection|null $calleeReflection, + ParameterReflection $parameter, + Scope $scope, + ): ?Type + { + if ($calleeReflection === null) { + return null; + } + + if ($funcCall instanceof Node\Expr\FuncCall && $calleeReflection instanceof FunctionReflection) { + foreach ($this->dynamicParameterTypeExtensionProvider->getDynamicFunctionParameterTypeExtensions() as $extension) { + if (!$extension->isFunctionSupported($calleeReflection, $parameter)) { + continue; + } + $type = $extension->getTypeFromFunctionCall($calleeReflection, $funcCall, $parameter, $scope); + if ($type !== null) { + return $type; + } + } + } elseif ($funcCall instanceof Node\Expr\StaticCall && $calleeReflection instanceof MethodReflection) { + foreach ($this->dynamicParameterTypeExtensionProvider->getDynamicStaticMethodParameterTypeExtensions() as $extension) { + if (!$extension->isStaticMethodSupported($calleeReflection, $parameter)) { + continue; + } + $type = $extension->getTypeFromStaticMethodCall($calleeReflection, $funcCall, $parameter, $scope); + if ($type !== null) { + return $type; + } + } + } elseif ($funcCall instanceof Node\Expr\MethodCall && $calleeReflection instanceof MethodReflection) { + foreach ($this->dynamicParameterTypeExtensionProvider->getDynamicMethodParameterTypeExtensions() as $extension) { + if (!$extension->isMethodSupported($calleeReflection, $parameter)) { + continue; + } + $type = $extension->getTypeFromMethodCall($calleeReflection, $funcCall, $parameter, $scope); + if ($type !== null) { + return $type; + } + } + } + + return null; + } + } diff --git a/src/Rules/Functions/ArrowFunctionReturnTypeRule.php b/src/Rules/Functions/ArrowFunctionReturnTypeRule.php index ab1fecfe737..70f2786cd96 100644 --- a/src/Rules/Functions/ArrowFunctionReturnTypeRule.php +++ b/src/Rules/Functions/ArrowFunctionReturnTypeRule.php @@ -12,6 +12,8 @@ use PHPStan\ShouldNotHappenException; use PHPStan\Type\NeverType; use PHPStan\Type\ObjectType; +use PHPStan\Type\TypeCombinator; +use function array_map; /** * @implements Rule @@ -37,6 +39,13 @@ public function processNode(Node $node, Scope $scope): array $returnType = $scope->getAnonymousFunctionReturnType(); $generatorType = new ObjectType(Generator::class); + $overriddenType = $node->getOverriddenType(); + if ($overriddenType !== null && $overriddenType->isCallable()->yes()) { + $returnType = TypeCombinator::union(...array_map( + static fn ($a) => $a->getReturnType(), + $overriddenType->getCallableParametersAcceptors($scope), + )); + } $originalNode = $node->getOriginalNode(); $isVoidSuperType = $returnType->isVoid(); diff --git a/src/Rules/Functions/CallToFunctionParametersRule.php b/src/Rules/Functions/CallToFunctionParametersRule.php index f01a081fdae..be1bdf1d78c 100644 --- a/src/Rules/Functions/CallToFunctionParametersRule.php +++ b/src/Rules/Functions/CallToFunctionParametersRule.php @@ -74,6 +74,7 @@ public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataE 'Constants %s cannot be combined for %s of function ' . $functionName . '.', 'Combining constants with | is not allowed for %s of function ' . $functionName . '.', null, + $function, ); } diff --git a/src/Rules/Functions/ClosureReturnTypeRule.php b/src/Rules/Functions/ClosureReturnTypeRule.php index abeb7c3866d..abfe196e39a 100644 --- a/src/Rules/Functions/ClosureReturnTypeRule.php +++ b/src/Rules/Functions/ClosureReturnTypeRule.php @@ -9,6 +9,7 @@ use PHPStan\Rules\FunctionReturnTypeCheck; use PHPStan\Rules\Rule; use PHPStan\Type\TypeCombinator; +use function array_map; /** * @implements Rule @@ -33,6 +34,13 @@ public function processNode(Node $node, Scope $scope): array } $returnType = $scope->getAnonymousFunctionReturnType(); + $overriddenType = $node->getOverriddenType(); + if ($overriddenType !== null && $overriddenType->isCallable()->yes()) { + $returnType = TypeCombinator::union(...array_map( + static fn ($a) => $a->getReturnType(), + $overriddenType->getCallableParametersAcceptors($scope), + )); + } $containsNull = TypeCombinator::containsNull($returnType); $hasNativeTypehint = $node->getClosureExpr()->returnType !== null; diff --git a/src/Rules/Methods/CallMethodsRule.php b/src/Rules/Methods/CallMethodsRule.php index 51881bfbea2..b593f2c8d38 100644 --- a/src/Rules/Methods/CallMethodsRule.php +++ b/src/Rules/Methods/CallMethodsRule.php @@ -108,6 +108,7 @@ private function processSingleMethodCall(Scope&NodeCallbackInvoker&CollectedData $declaringClass->getName(), $methodReflection->getName(), ] : null, + $methodReflection, )); } diff --git a/src/Rules/Methods/CallStaticMethodsRule.php b/src/Rules/Methods/CallStaticMethodsRule.php index a275d3fb731..009514adcaf 100644 --- a/src/Rules/Methods/CallStaticMethodsRule.php +++ b/src/Rules/Methods/CallStaticMethodsRule.php @@ -114,6 +114,7 @@ private function processSingleMethodCall(Scope&NodeCallbackInvoker&CollectedData 'Constants %s cannot be combined for %s of ' . $lowercasedMethodName . '.', 'Combining constants with | is not allowed for %s of ' . $lowercasedMethodName . '.', null, + $method, )); return $errors; diff --git a/src/Testing/RuleTestCase.php b/src/Testing/RuleTestCase.php index 04f550f51ba..91031f78da3 100644 --- a/src/Testing/RuleTestCase.php +++ b/src/Testing/RuleTestCase.php @@ -21,6 +21,7 @@ use PHPStan\Dependency\DependencyResolver; use PHPStan\Dependency\PackageDependencyResolver; use PHPStan\DependencyInjection\DirectExtensionsCollection; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\File\FileHelper; use PHPStan\File\FileReader; use PHPStan\Fixable\Patcher; @@ -118,6 +119,7 @@ protected function createNodeScopeResolver(): NodeScopeResolver self::getContainer()->getExtensionsCollection(FunctionParameterClosureThisExtension::class), self::getContainer()->getExtensionsCollection(MethodParameterClosureThisExtension::class), self::getContainer()->getExtensionsCollection(StaticMethodParameterClosureThisExtension::class), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), self::getContainer()->getExtensionsCollection(FunctionParameterClosureTypeExtension::class), self::getContainer()->getExtensionsCollection(MethodParameterClosureTypeExtension::class), self::getContainer()->getExtensionsCollection(StaticMethodParameterClosureTypeExtension::class), diff --git a/src/Testing/TypeInferenceTestCase.php b/src/Testing/TypeInferenceTestCase.php index 13e9a907dab..ef6ea7951b3 100644 --- a/src/Testing/TypeInferenceTestCase.php +++ b/src/Testing/TypeInferenceTestCase.php @@ -13,6 +13,7 @@ use PHPStan\Analyser\NodeScopeResolver; use PHPStan\Analyser\Scope; use PHPStan\Analyser\ScopeContext; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\File\FileHelper; use PHPStan\File\SystemAgnosticSimpleRelativePathHelper; use PHPStan\Node\DeepNodeCloner; @@ -93,6 +94,7 @@ protected static function createNodeScopeResolver(): NodeScopeResolver $container->getExtensionsCollection(FunctionParameterClosureThisExtension::class), $container->getExtensionsCollection(MethodParameterClosureThisExtension::class), $container->getExtensionsCollection(StaticMethodParameterClosureThisExtension::class), + $container->getByType(DynamicParameterTypeExtensionProvider::class), $container->getExtensionsCollection(FunctionParameterClosureTypeExtension::class), $container->getExtensionsCollection(MethodParameterClosureTypeExtension::class), $container->getExtensionsCollection(StaticMethodParameterClosureTypeExtension::class), diff --git a/src/Type/DynamicFunctionParameterTypeExtension.php b/src/Type/DynamicFunctionParameterTypeExtension.php new file mode 100644 index 00000000000..1eed7eab09c --- /dev/null +++ b/src/Type/DynamicFunctionParameterTypeExtension.php @@ -0,0 +1,34 @@ +getExtensionsCollection(FunctionParameterClosureThisExtension::class), $container->getExtensionsCollection(MethodParameterClosureThisExtension::class), $container->getExtensionsCollection(StaticMethodParameterClosureThisExtension::class), + $container->getByType(DynamicParameterTypeExtensionProvider::class), $container->getExtensionsCollection(FunctionParameterClosureTypeExtension::class), $container->getExtensionsCollection(MethodParameterClosureTypeExtension::class), $container->getExtensionsCollection(StaticMethodParameterClosureTypeExtension::class), diff --git a/tests/PHPStan/Analyser/Bug9307CallMethodsRuleTest.php b/tests/PHPStan/Analyser/Bug9307CallMethodsRuleTest.php index 3cf21124155..436616eebc7 100644 --- a/tests/PHPStan/Analyser/Bug9307CallMethodsRuleTest.php +++ b/tests/PHPStan/Analyser/Bug9307CallMethodsRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Analyser; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\FunctionCallParametersCheck; use PHPStan\Rules\Methods\CallMethodsRule; use PHPStan\Rules\Methods\MethodCallCheck; @@ -44,6 +45,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Analyser/DynamicParameterTypeExtensionArraysTest.php b/tests/PHPStan/Analyser/DynamicParameterTypeExtensionArraysTest.php new file mode 100644 index 00000000000..756d80acfc1 --- /dev/null +++ b/tests/PHPStan/Analyser/DynamicParameterTypeExtensionArraysTest.php @@ -0,0 +1,30 @@ += 8.0.0')] +class DynamicParameterTypeExtensionArraysTest extends TypeInferenceTestCase +{ + + public static function dataFileAsserts(): iterable + { + yield from self::gatherAssertTypes(__DIR__ . '/data/dynamic-parameter-type-extension-arrays.php'); + } + + /** @param mixed ...$args */ + #[DataProvider('dataFileAsserts')] + public function testFileAsserts(string $assertType, string $file, ...$args): void + { + $this->assertFileAsserts($assertType, $file, ...$args); + } + + public static function getAdditionalConfigFiles(): array + { + return [__DIR__ . '/dynamic-parameter-type-extension-arrays.neon']; + } + +} diff --git a/tests/PHPStan/Analyser/DynamicParameterTypeExtensionClosuresTest.php b/tests/PHPStan/Analyser/DynamicParameterTypeExtensionClosuresTest.php new file mode 100644 index 00000000000..7a4c0890ace --- /dev/null +++ b/tests/PHPStan/Analyser/DynamicParameterTypeExtensionClosuresTest.php @@ -0,0 +1,30 @@ += 8.0.0')] +class DynamicParameterTypeExtensionClosuresTest extends TypeInferenceTestCase +{ + + public static function dataFileAsserts(): iterable + { + yield from self::gatherAssertTypes(__DIR__ . '/data/dynamic-parameter-type-extension-closures.php'); + } + + /** @param mixed ...$args */ + #[DataProvider('dataFileAsserts')] + public function testFileAsserts(string $assertType, string $file, ...$args): void + { + $this->assertFileAsserts($assertType, $file, ...$args); + } + + public static function getAdditionalConfigFiles(): array + { + return [__DIR__ . '/dynamic-parameter-type-extension-closures.neon']; + } + +} diff --git a/tests/PHPStan/Analyser/DynamicParameterTypeExtensionErrorsRuleTest.php b/tests/PHPStan/Analyser/DynamicParameterTypeExtensionErrorsRuleTest.php new file mode 100644 index 00000000000..2ba8fa1dd3c --- /dev/null +++ b/tests/PHPStan/Analyser/DynamicParameterTypeExtensionErrorsRuleTest.php @@ -0,0 +1,72 @@ + + */ +class DynamicParameterTypeExtensionErrorsRuleTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + $reflectionProvider = self::createReflectionProvider(); + $ruleLevelHelper = new RuleLevelHelper( + $reflectionProvider, + checkNullables: true, + checkThisOnly: false, + checkUnionTypes: true, + checkExplicitMixed: true, + checkImplicitMixed: false, + checkBenevolentUnionTypes: false, + discoveringSymbolsTip: true, + ); + return new CallMethodsRule( + new MethodCallCheck( + $reflectionProvider, + $ruleLevelHelper, + checkFunctionNameCase: true, + reportMagicMethods: true, + ), + new FunctionCallParametersCheck( + $ruleLevelHelper, + new NullsafeCheck(), + new UnresolvableTypeHelper(), + new PropertyReflectionFinder(), + $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), + checkArgumentTypes: true, + checkArgumentsPassedByReference: true, + checkExtraArguments: true, + checkMissingTypehints: true, + ), + ); + } + + public static function getAdditionalConfigFiles(): array + { + return [__DIR__ . '/dynamic-parameter-type-extension-closures-errors.neon']; + } + + public function testErrorCases(): void + { + $this->analyse([__DIR__ . '/data/dynamic-parameter-type-extension-closures-errors.php'], [ + [ + 'Call to an undefined method DynamicParameterTypeExtensionClosuresErrors\Generic::nonExistentMethod().', + 84, + ], + ]); + } + +} diff --git a/tests/PHPStan/Analyser/ExpressionResultTest.php b/tests/PHPStan/Analyser/ExpressionResultTest.php index 4da555b362f..11dd4c2dd61 100644 --- a/tests/PHPStan/Analyser/ExpressionResultTest.php +++ b/tests/PHPStan/Analyser/ExpressionResultTest.php @@ -212,6 +212,7 @@ public function testIsAlwaysTerminating( static function (): void { }, ExpressionContext::createTopLevel(), + null, ); $this->assertSame($expectedIsAlwaysTerminating, $result->isAlwaysTerminating()); } diff --git a/tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverRuleTest.php b/tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverRuleTest.php index c9732b873d2..6ef217e691c 100644 --- a/tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverRuleTest.php +++ b/tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverRuleTest.php @@ -8,6 +8,7 @@ use PHPStan\Analyser\NodeScopeResolver; use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\DirectExtensionsCollection; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\File\FileHelper; use PHPStan\Node\DeepNodeCloner; use PHPStan\PhpDoc\PhpDocInheritanceResolver; @@ -138,6 +139,7 @@ protected function createNodeScopeResolver(): NodeScopeResolver self::getContainer()->getExtensionsCollection(FunctionParameterClosureThisExtension::class), self::getContainer()->getExtensionsCollection(MethodParameterClosureThisExtension::class), self::getContainer()->getExtensionsCollection(StaticMethodParameterClosureThisExtension::class), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), self::getContainer()->getExtensionsCollection(FunctionParameterClosureTypeExtension::class), self::getContainer()->getExtensionsCollection(MethodParameterClosureTypeExtension::class), self::getContainer()->getExtensionsCollection(StaticMethodParameterClosureTypeExtension::class), diff --git a/tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverTest.php b/tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverTest.php index 00038bbc18e..d598e66b104 100644 --- a/tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/Fiber/FiberNodeScopeResolverTest.php @@ -5,6 +5,7 @@ use PHPStan\Analyser\ExpressionResultFactory; use PHPStan\Analyser\ExprHandler\Helper\ImplicitToStringCallHelper; use PHPStan\Analyser\NodeScopeResolver; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\File\FileHelper; use PHPStan\Node\DeepNodeCloner; use PHPStan\PhpDoc\PhpDocInheritanceResolver; @@ -71,6 +72,7 @@ protected static function createNodeScopeResolver(): NodeScopeResolver $container->getExtensionsCollection(FunctionParameterClosureThisExtension::class), $container->getExtensionsCollection(MethodParameterClosureThisExtension::class), $container->getExtensionsCollection(StaticMethodParameterClosureThisExtension::class), + $container->getByType(DynamicParameterTypeExtensionProvider::class), $container->getExtensionsCollection(FunctionParameterClosureTypeExtension::class), $container->getExtensionsCollection(MethodParameterClosureTypeExtension::class), $container->getExtensionsCollection(StaticMethodParameterClosureTypeExtension::class), diff --git a/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-arrays.php b/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-arrays.php new file mode 100644 index 00000000000..8267ae3e30c --- /dev/null +++ b/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-arrays.php @@ -0,0 +1,181 @@ += 8.0 + +namespace DynamicParameterTypeExtensionArrays; + +use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\StaticCall; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\FunctionReflection; +use PHPStan\Reflection\MethodReflection; +use PHPStan\Reflection\Native\NativeParameterReflection; +use PHPStan\Reflection\ParameterReflection; +use PHPStan\Reflection\PassedByReference; +use PHPStan\Type\CallableType; +use PHPStan\Type\FloatType; +use PHPStan\Type\Generic\GenericObjectType; +use PHPStan\Type\Constant\ConstantIntegerType; +use PHPStan\Type\Constant\ConstantStringType; +use PHPStan\Type\DynamicFunctionParameterTypeExtension; +use PHPStan\Type\DynamicMethodParameterTypeExtension; +use PHPStan\Type\DynamicStaticMethodParameterTypeExtension; +use PHPStan\Type\IntegerType; +use PHPStan\Type\StringType; +use PHPStan\Type\Type; +use PHPStan\Type\MixedType; +use PHPStan\TrinaryLogic; +use function PHPStan\Testing\assertType; + +final class DynamicParameterTypeExtension implements DynamicFunctionParameterTypeExtension, DynamicMethodParameterTypeExtension, DynamicStaticMethodParameterTypeExtension +{ + + public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool + { + return true; + } + + public function isMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool + { + return true; + } + + public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool + { + return true; + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type + { + return $this->getType($methodReflection, $methodCall, $parameter, $scope); + } + + public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type + { + return $this->getType($methodReflection, $methodCall, $parameter, $scope); + } + + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, ParameterReflection $parameter, Scope $scope): ?Type + { + return $this->getType($functionReflection, $functionCall, $parameter, $scope); + } + + private function getType( + FunctionReflection|MethodReflection $functionReflection, + FuncCall|MethodCall|StaticCall $call, + ParameterReflection $parameter, + Scope $scope, + ): ?Type + { + $arg = $call->getArgs()[0] ?? null; + if (!$arg) { + return null; + } + + $type = $scope->getType($arg->value)->getConstantArrays()[0] ?? null; + if (!$type) { + return null; + } + + $replacements = [ + 'a' => new IntegerType(), + 'b' => new StringType(), + 0 => new IntegerType(), + 1 => new StringType(), + 2 => new FloatType(), + ]; + + foreach ($replacements as $key => $value) { + $keyType = is_int($key) ? new ConstantIntegerType($key) : new ConstantStringType($key); + if ($type->hasOffsetValueType($keyType)->no()) { + continue; + } + + $newType = new CallableType([ + new NativeParameterReflection('test', false, new GenericObjectType(Generic::class, [$value]), PassedByReference::createNo(), false, null), + ], new MixedType(), false); + + $type = $type->setOffsetValueType($keyType, $newType, false); + } + + return $type; + } +} + +class Foo +{ + + /** @param array)> $array */ + public function methodWithArray($array) {} + + public static function staticMethodWithArray(array $array) {} + +} + +/** @template T */ +class Generic +{ + public function __construct( + /** @var T */ + private mixed $value, + ) { + } + + /** @return T */ + public function getValue() + { + return $this->value; + } +} + +/** @param array)> $array */ +function functionWithArray(array $array): void {} + +/** @param list)> $list */ +function functionWithNumericArray(array $list): void {} + +function test(Foo $foo): void +{ + functionWithArray([ + fn ($i) => assertType('int', $i->getValue()), + fn ($i) => assertType('string', $i->getValue()), + fn ($i) => assertType('float', $i->getValue()), + ]); + + functionWithArray([ + 0 => fn ($i) => assertType('int', $i->getValue()), + 1 => fn ($i) => assertType('string', $i->getValue()), + 2 => fn ($i) => assertType('float', $i->getValue()), + ]); + + functionWithArray([ + 'a' => fn ($i) => assertType('int', $i->getValue()), + 'b' => fn ($i) => assertType('string', $i->getValue()), + 'c' => fn (int $i) => assertType('int', $i), + ]); + $foo->methodWithArray([ + 'a' => fn ($i) => assertType('int', $i->getValue()), + 'b' => fn ($i) => assertType('string', $i->getValue()), + 'c' => fn (int $i) => assertType('int', $i), + ]); + Foo::staticMethodWithArray([ + 'a' => fn ($i) => assertType('int', $i->getValue()), + 'b' => fn ($i) => assertType('string', $i->getValue()), + 'c' => fn (int $i) => assertType('int', $i), + ]); + + functionWithArray([ + 'a' => function ($i) { assertType('int', $i->getValue()); }, + 'b' => function ($i) { assertType('string', $i->getValue()); }, + 'c' => function (int $i) { assertType('int', $i); }, + ]); + $foo->methodWithArray([ + 'a' => function ($i) { assertType('int', $i->getValue()); }, + 'b' => function ($i) { assertType('string', $i->getValue()); }, + 'c' => function (int $i) { assertType('int', $i); }, + ]); + Foo::staticMethodWithArray([ + 'a' => function ($i) { assertType('int', $i->getValue()); }, + 'b' => function ($i) { assertType('string', $i->getValue()); }, + 'c' => function (int $i) { assertType('int', $i); }, + ]); +} diff --git a/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-closures-errors.php b/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-closures-errors.php new file mode 100644 index 00000000000..0eee2e608e2 --- /dev/null +++ b/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-closures-errors.php @@ -0,0 +1,96 @@ += 8.0 + +namespace DynamicParameterTypeExtensionClosuresErrors; + +use PhpParser\Node\Expr\MethodCall; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\MethodReflection; +use PHPStan\Reflection\Native\NativeParameterReflection; +use PHPStan\Reflection\ParameterReflection; +use PHPStan\Reflection\PassedByReference; +use PHPStan\Type\CallableType; +use PHPStan\Type\DynamicMethodParameterTypeExtension; +use PHPStan\Type\Generic\GenericObjectType; +use PHPStan\Type\IntegerType; +use PHPStan\Type\MixedType; +use PHPStan\Type\StringType; +use PHPStan\Type\Type; + +/** @template T */ +class Generic +{ + /** @param T $value */ + public function __construct(private mixed $value) {} + + /** @return T */ + public function getValue() { return $this->value; } +} + +class Foo +{ + public function methodWithCallable(int $foo, callable $callback): void {} +} + +final class ErrorTestExtension implements DynamicMethodParameterTypeExtension +{ + + public function isMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool + { + return $methodReflection->getDeclaringClass()->getName() === Foo::class + && $parameter->getName() === 'callback' + && $methodReflection->getName() === 'methodWithCallable'; + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type + { + $args = $methodCall->getArgs(); + if (count($args) < 2) { + return null; + } + + $integer = $scope->getType($args[0]->value)->getConstantScalarValues()[0] ?? null; + + $valueType = $integer === 1 ? new IntegerType() : new StringType(); + + return new CallableType( + [ + new NativeParameterReflection('test', false, new GenericObjectType(Generic::class, [$valueType]), PassedByReference::createNo(), false, null), + ], + new MixedType(), + ); + } + +} + +function acceptInt(int $value): void {} +function acceptString(string $value): void {} + +function testErrorCases(Foo $foo): void +{ + // Extension overrides param to Generic, getValue() returns int + // Passing int where string is expected should be an error + $foo->methodWithCallable(1, function ($i) { + acceptString($i->getValue()); + }); + + // Extension overrides param to Generic, getValue() returns string + // Passing string where int is expected should be an error + $foo->methodWithCallable(2, function ($i) { + acceptInt($i->getValue()); + }); + + // Calling non-existent method on overridden type should be an error + $foo->methodWithCallable(1, function ($i) { + $i->nonExistentMethod(); + }); + + // No error: correct usage matches overridden parameter type + $foo->methodWithCallable(1, function ($i) { + acceptInt($i->getValue()); + }); + + // No error: correct usage for string variant + $foo->methodWithCallable(2, function ($i) { + acceptString($i->getValue()); + }); +} diff --git a/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-closures.php b/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-closures.php new file mode 100644 index 00000000000..7afc522247b --- /dev/null +++ b/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-closures.php @@ -0,0 +1,269 @@ += 8.0 + +namespace DynamicParameterTypeExtensionClosures; + +use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\StaticCall; +use PHPStan\Analyser\Scope; +use PHPStan\Reflection\FunctionReflection; +use PHPStan\Reflection\MethodReflection; +use PHPStan\Reflection\Native\NativeParameterReflection; +use PHPStan\Reflection\ParameterReflection; +use PHPStan\Reflection\PassedByReference; +use PHPStan\Type\CallableType; +use PHPStan\Type\DynamicFunctionParameterTypeExtension; +use PHPStan\Type\DynamicMethodParameterTypeExtension; +use PHPStan\Type\DynamicStaticMethodParameterTypeExtension; +use PHPStan\Type\FloatType; +use PHPStan\Type\Generic\GenericObjectType; +use PHPStan\Type\IntegerType; +use PHPStan\Type\StringType; +use PHPStan\Type\Type; +use PHPStan\Type\MixedType; +use function PHPStan\Testing\assertType; + +final class DynamicParameterTypeExtension implements DynamicFunctionParameterTypeExtension, DynamicMethodParameterTypeExtension, DynamicStaticMethodParameterTypeExtension +{ + + public function isFunctionSupported(FunctionReflection $functionReflection, ParameterReflection $parameter): bool + { + return $functionReflection->getName() === 'DynamicParameterTypeExtensionClosures\functionWithCallable'; + } + + public function isMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool + { + return $methodReflection->getDeclaringClass()->getName() === Foo::class && + $parameter->getName() === 'callback' && + $methodReflection->getName() === 'methodWithCallable'; + } + + public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool + { + if ($methodReflection->getDeclaringClass()->getName() === Foo::class && $methodReflection->getName() === 'staticMethodWithCallable') { + return true; + } + + if ($methodReflection->getDeclaringClass()->getName() === Bar::class && $methodReflection->getName() === '__construct') { + return true; + } + + return false; + } + + public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, ParameterReflection $parameter, Scope $scope): ?Type + { + return $this->getType($functionReflection, $functionCall, $parameter, $scope); + } + + public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type + { + return $this->getType($methodReflection, $methodCall, $parameter, $scope); + } + + public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type + { + if ($methodReflection->getDeclaringClass()->getName() === Bar::class && $methodReflection->getName() === '__construct') { + $args = $methodCall->getArgs(); + + if (count($args) < 2) { + return null; + } + + $integer = $scope->getType($args[0]->value)->getConstantScalarValues()[0]; + + if ($integer === 1) { + return new CallableType( + [ + new NativeParameterReflection('test', false, new IntegerType(), PassedByReference::createNo(), false, null), + ], + new MixedType() + ); + } + + return new CallableType( + [ + new NativeParameterReflection('test', false, new StringType(), PassedByReference::createNo(), false, null), + ], + new MixedType() + ); + } + + return new CallableType( + [ + new NativeParameterReflection('test', false, new FloatType(), PassedByReference::createNo(), false, null), + ], + new MixedType() + ); + } + + private function getType( + FunctionReflection|MethodReflection $methodReflection, + FuncCall|MethodCall $methodCall, + ParameterReflection $parameter, + Scope $scope, + ): ?Type { + $args = $methodCall->getArgs(); + + if (count($args) < 2) { + return null; + } + + $integer = $scope->getType($args[0]->value)->getConstantScalarValues()[0]; + + if ($integer === 1) { + return new CallableType( + [ + new NativeParameterReflection('test', false, new GenericObjectType(Generic::class, [new IntegerType()]), PassedByReference::createNo(), false, null), + ], + new MixedType() + ); + } + + return new CallableType( + [ + new NativeParameterReflection('test', false, new GenericObjectType(Generic::class, [new StringType()]), PassedByReference::createNo(), false, null), + ], + new MixedType() + ); + } +} + +class Foo +{ + + /** + * @param int $foo + * @param callable(Generic) $callback + * + * @return void + */ + public function methodWithCallable(int $foo, callable $callback) {} + + /** @return void */ + public static function staticMethodWithCallable(callable $callback) {} + +} + +/** @template T */ +class Generic +{ + private $value; + + /** @param T $value */ + public function __construct($value) + { + $this->value = $value; + } + + /** @return T */ + public function getValue() + { + return $this->value; + } +} + +class Bar +{ + + /** + * @param int $foo + * @param callable(mixed) $callback + */ + public function __construct(int $foo, callable $callback) + { + + } + +} + +/** + * @param int $foo + * @param callable(Generic) $callback + * + * @return void + */ +function functionWithCallable(int $foo, callable $callback) {} + +function test(Foo $foo): void +{ + + // arrow functions + $foo->methodWithCallable(1, fn ($i) => assertType('int', $i->getValue())); + (new Foo)->methodWithCallable(2, fn (Generic $i) => assertType('string', $i->getValue())); + Foo::staticMethodWithCallable(fn ($i) => assertType('float', $i)); + functionWithCallable(1, fn ($i) => assertType('int', $i->getValue())); + functionWithCallable(2, fn (Generic $i) => assertType('string', $i->getValue())); + + new Bar(1, fn ($i) => assertType('int', $i)); + new Bar(2, fn ($i) => assertType('string', $i)); + + + // closures + $foo->methodWithCallable(1, function ($i) { assertType('int', $i->getValue()); }); + (new Foo)->methodWithCallable(2, function (Generic $i) { assertType('string', $i->getValue()); }); + Foo::staticMethodWithCallable(function ($i) { assertType('float', $i); }); + functionWithCallable(1, function ($i) { assertType('int', $i->getValue()); }); + functionWithCallable(2, function (Generic $i) { assertType('string', $i->getValue()); }); + + new Bar(1, function ($i) { assertType('int', $i); }); + new Bar(2, function ($i) { assertType('string', $i); }); +} + +/** + * @param callable(int): void|null $callback + */ +function functionWithUnionCallable(callable|null $callback): void {} + +/** + * @param callable(int): string $callback + */ +function functionWithCallableReturnType(callable $callback): void {} + +function testUnionCallable(): void +{ + // Test with union type containing callable and non-callable + functionWithUnionCallable(fn ($i) => assertType('int', $i)); + functionWithUnionCallable(function ($i) { assertType('int', $i); }); + + // Test closure return type checking + functionWithCallableReturnType(fn ($i): string => 'test'); + functionWithCallableReturnType(function ($i): string { return 'test'; }); +} + +function testComplexExpressions(Foo $foo): void +{ + // Type narrowing inside overridden closure + $foo->methodWithCallable(1, function ($i) { + $val = $i->getValue(); + assertType('int', $val); + + if ($val > 0) { + assertType('int<1, max>', $val); + } + }); + + // Variable assignment and reuse + functionWithCallable(2, function ($i) { + $val = $i->getValue(); + assertType('string', $val); + + $upper = strtoupper($val); + assertType('uppercase-string', $upper); + }); + + // Nested method calls on overridden type + $foo->methodWithCallable(1, function ($i) { + assertType('DynamicParameterTypeExtensionClosures\Generic', $i); + assertType('int', $i->getValue()); + }); + + // Multiple statements in closure body + functionWithCallable(1, function ($i) { + $a = $i->getValue(); + $b = $i->getValue(); + assertType('int', $a); + assertType('int', $b); + assertType('int', $a + $b); + }); +} diff --git a/tests/PHPStan/Analyser/dynamic-parameter-type-extension-arrays.neon b/tests/PHPStan/Analyser/dynamic-parameter-type-extension-arrays.neon new file mode 100644 index 00000000000..82f6e9cb59d --- /dev/null +++ b/tests/PHPStan/Analyser/dynamic-parameter-type-extension-arrays.neon @@ -0,0 +1,7 @@ +services: + - + class: DynamicParameterTypeExtensionArrays\DynamicParameterTypeExtension + tags: + - phpstan.dynamicFunctionParameterTypeExtension + - phpstan.dynamicMethodParameterTypeExtension + - phpstan.dynamicStaticMethodParameterTypeExtension diff --git a/tests/PHPStan/Analyser/dynamic-parameter-type-extension-closures-errors.neon b/tests/PHPStan/Analyser/dynamic-parameter-type-extension-closures-errors.neon new file mode 100644 index 00000000000..94483d2f9d3 --- /dev/null +++ b/tests/PHPStan/Analyser/dynamic-parameter-type-extension-closures-errors.neon @@ -0,0 +1,5 @@ +services: + - + class: DynamicParameterTypeExtensionClosuresErrors\ErrorTestExtension + tags: + - phpstan.dynamicMethodParameterTypeExtension diff --git a/tests/PHPStan/Analyser/dynamic-parameter-type-extension-closures.neon b/tests/PHPStan/Analyser/dynamic-parameter-type-extension-closures.neon new file mode 100644 index 00000000000..13c6ef79d71 --- /dev/null +++ b/tests/PHPStan/Analyser/dynamic-parameter-type-extension-closures.neon @@ -0,0 +1,7 @@ +services: + - + class: DynamicParameterTypeExtensionClosures\DynamicParameterTypeExtension + tags: + - phpstan.dynamicFunctionParameterTypeExtension + - phpstan.dynamicMethodParameterTypeExtension + - phpstan.dynamicStaticMethodParameterTypeExtension diff --git a/tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php b/tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php index a9e75314f24..291f759e48d 100644 --- a/tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Classes; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -49,6 +50,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php b/tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php index 5e1e49d1c54..bbb539724b6 100644 --- a/tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Classes; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -44,6 +45,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php b/tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php index 0c5c640b794..854c2b20492 100644 --- a/tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php +++ b/tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Classes; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; use PHPStan\Rules\ClassNameCheck; @@ -46,6 +47,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php index f4cc96575bf..cee80881fcb 100644 --- a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php +++ b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Classes; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; use PHPStan\Rules\ClassNameCheck; @@ -48,6 +49,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Constants/ConstantAttributesRuleTest.php b/tests/PHPStan/Rules/Constants/ConstantAttributesRuleTest.php index a9d853a3907..3b013c42df7 100644 --- a/tests/PHPStan/Rules/Constants/ConstantAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Constants/ConstantAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Constants; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Php\PhpVersion; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; @@ -50,6 +51,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php b/tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php index d2d76782cc0..0377452c113 100644 --- a/tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php +++ b/tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\EnumCases; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -45,6 +46,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Functions/ArrowFunctionAttributesRuleTest.php b/tests/PHPStan/Rules/Functions/ArrowFunctionAttributesRuleTest.php index 80c987474cd..05f5ac32506 100644 --- a/tests/PHPStan/Rules/Functions/ArrowFunctionAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ArrowFunctionAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Functions; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -44,6 +45,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Functions/Bug14844Test.php b/tests/PHPStan/Rules/Functions/Bug14844Test.php index 0dc44d2ba63..ee33687670a 100644 --- a/tests/PHPStan/Rules/Functions/Bug14844Test.php +++ b/tests/PHPStan/Rules/Functions/Bug14844Test.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Functions; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\FunctionCallParametersCheck; use PHPStan\Rules\NullsafeCheck; use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; @@ -37,6 +38,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $broker, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php b/tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php index 03448315326..7c026d34932 100644 --- a/tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Functions; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\FunctionCallParametersCheck; use PHPStan\Rules\NullsafeCheck; use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; @@ -41,6 +42,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 44bc0a56d08..2fcd5a4a6b5 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Functions; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\FunctionCallParametersCheck; use PHPStan\Rules\NullsafeCheck; use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; @@ -45,6 +46,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $broker, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, @@ -743,19 +745,19 @@ public function testPregReplaceCallback(): void { $this->analyse([__DIR__ . '/data/preg_replace_callback.php'], [ [ - 'Parameter #2 $callback of function preg_replace_callback expects callable(array): string, Closure(string): string given.', + 'Parameter #2 $callback of function preg_replace_callback expects Closure(array{non-falsy-string}): string, Closure(string): string given.', 6, ], [ - 'Parameter #2 $callback of function preg_replace_callback expects callable(array): string, Closure(string): string given.', + 'Parameter #2 $callback of function preg_replace_callback expects Closure(array{non-falsy-string}): string, Closure(string): string given.', 13, ], [ - 'Parameter #2 $callback of function preg_replace_callback expects callable(array): string, Closure(array): void given.', + 'Parameter #2 $callback of function preg_replace_callback expects Closure(array{non-falsy-string}): string, Closure(array): void given.', 20, ], [ - 'Parameter #2 $callback of function preg_replace_callback expects callable(array): string, Closure(): void given.', + 'Parameter #2 $callback of function preg_replace_callback expects Closure(array{non-falsy-string}): string, Closure(): void given.', 25, ], ]); diff --git a/tests/PHPStan/Rules/Functions/CallUserFuncRuleTest.php b/tests/PHPStan/Rules/Functions/CallUserFuncRuleTest.php index a05d57f9457..626fb4af0c9 100644 --- a/tests/PHPStan/Rules/Functions/CallUserFuncRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallUserFuncRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Functions; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\FunctionCallParametersCheck; use PHPStan\Rules\NullsafeCheck; use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; @@ -37,6 +38,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php b/tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php index ec1d69de635..37fde4bf0ee 100644 --- a/tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Functions; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -44,6 +45,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php b/tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php index 0ad877c2c30..d9823484039 100644 --- a/tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Functions; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -44,6 +45,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php b/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php index 32747cba13e..5ebdeb32c44 100644 --- a/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Functions; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -44,6 +45,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 7bb3906083d..4aea2855b73 100644 --- a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Methods; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\FunctionCallParametersCheck; use PHPStan\Rules\NullsafeCheck; use PHPStan\Rules\PhpDoc\UnresolvableTypeHelper; @@ -55,6 +56,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php index f4d3868b15e..d60d32ed694 100644 --- a/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Methods; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; use PHPStan\Rules\ClassNameCheck; @@ -68,6 +69,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php b/tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php index d9f2516fdb2..ed6522c579a 100644 --- a/tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Methods; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -44,6 +45,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Methods/MethodCallWithPossiblyRenamedNamedArgumentRuleTest.php b/tests/PHPStan/Rules/Methods/MethodCallWithPossiblyRenamedNamedArgumentRuleTest.php index bcece2e61ef..3c107683f82 100644 --- a/tests/PHPStan/Rules/Methods/MethodCallWithPossiblyRenamedNamedArgumentRuleTest.php +++ b/tests/PHPStan/Rules/Methods/MethodCallWithPossiblyRenamedNamedArgumentRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Methods; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Php\PhpVersion; use PHPStan\Reflection\Php\PhpClassReflectionExtension; use PHPStan\Rules\FunctionCallParametersCheck; @@ -32,7 +33,7 @@ protected function getRule(): Rule return new CompositeRule([ new CallMethodsRule( new MethodCallCheck($reflectionProvider, $ruleLevelHelper, true, true), - new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, true, true, true, true), + new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, true), ), new OverridingMethodRule( $phpVersion, diff --git a/tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php b/tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php index a35a951753e..2ea0453fa8f 100644 --- a/tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Properties; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Php\PhpVersion; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; @@ -46,6 +47,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Properties/PropertyHookAttributesRuleTest.php b/tests/PHPStan/Rules/Properties/PropertyHookAttributesRuleTest.php index 385ff733bf3..65ed0b8c52a 100644 --- a/tests/PHPStan/Rules/Properties/PropertyHookAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/PropertyHookAttributesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Properties; use PHPStan\Classes\ForbiddenClassNameExtension; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -44,6 +45,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true, diff --git a/tests/PHPStan/Rules/Traits/TraitAttributesRuleTest.php b/tests/PHPStan/Rules/Traits/TraitAttributesRuleTest.php index 68fea35dcde..bc0da0af29b 100644 --- a/tests/PHPStan/Rules/Traits/TraitAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Traits/TraitAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Traits; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Php\PhpVersion; use PHPStan\Rules\RestrictedUsage\RestrictedClassNameUsageExtension; use PHPStan\Classes\ForbiddenClassNameExtension; @@ -51,6 +52,7 @@ protected function getRule(): Rule new UnresolvableTypeHelper(), new PropertyReflectionFinder(), $reflectionProvider, + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), checkArgumentTypes: true, checkArgumentsPassedByReference: true, checkExtraArguments: true,