diff --git a/.github/workflows/e2e-tests.yml b/.github/workflows/e2e-tests.yml index ec604bb074..90d97d281b 100644 --- a/.github/workflows/e2e-tests.yml +++ b/.github/workflows/e2e-tests.yml @@ -264,6 +264,10 @@ jobs: cd e2e/bug-11857 composer install ../../bin/phpstan + - script: | + cd e2e/bug-12585 + composer install + ../../bin/phpstan - script: | cd e2e/result-cache-meta-extension composer install @@ -405,6 +409,10 @@ jobs: - script: | cd e2e/bug13425 timeout 15 ../bashunit -a exit_code "1" "../../bin/phpstan analyze src/ plugins/" + - script: | + cd e2e/parameter-type-extension + composer install + ../../bin/phpstan analyze steps: - name: "Checkout" diff --git a/e2e/bug-12585/.gitignore b/e2e/bug-12585/.gitignore new file mode 100644 index 0000000000..8b7ef35032 --- /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 0000000000..a072011fe8 --- /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 0000000000..a463d65106 --- /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 0000000000..285e732b1a --- /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 0000000000..57abe4977e --- /dev/null +++ b/e2e/bug-12585/src/test.php @@ -0,0 +1,157 @@ + $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 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')); +} diff --git a/e2e/parameter-type-extension/.gitignore b/e2e/parameter-type-extension/.gitignore new file mode 100644 index 0000000000..de4a392c33 --- /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 0000000000..f8a4e6ebed --- /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 0000000000..b383d88ac5 --- /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 0000000000..399cb98181 --- /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 0000000000..4920ab3a2a --- /dev/null +++ b/e2e/parameter-type-extension/src/ParameterTypeExtension.php @@ -0,0 +1,94 @@ +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([ + /** @phpstan-ignore phpstanApi.constructor */ + new NativeParameterReflection('test', false, $relationType, PassedByReference::createNo(), false, 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 0000000000..51fc3c2b63 --- /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 0000000000..a49f76ab8e --- /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 0000000000..2052022fd8 --- /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 0000000000..f27399a042 --- /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 0000000000..0fb0a2c194 --- /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 0000000000..15a2ff3ad6 --- /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 0000000000..4f0c8d559f --- /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 0000000000..0da183802c --- /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 0000000000..87fda747e6 --- /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 0000000000..99aa7db51c --- /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 5922c9380b..22e8b4a76a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -66,6 +66,60 @@ parameters: count: 1 path: src/Analyser/MutatingScope.php + - + rawMessage: 'Call to method getFunctionParameterClosureTypeExtensions() of deprecated interface PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider.' + identifier: method.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Call to method getMethodParameterClosureTypeExtensions() of deprecated interface PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider.' + identifier: method.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: 'Call to method getStaticMethodParameterClosureTypeExtensions() of deprecated interface PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider.' + identifier: method.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: 1 + 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: 1 + path: src/Analyser/NodeScopeResolver.php + - rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.' identifier: phpstanApi.instanceofType @@ -78,6 +132,18 @@ parameters: count: 1 path: src/Analyser/NodeScopeResolver.php + - + rawMessage: 'Parameter $parameterClosureTypeExtensionProvider of method PHPStan\Analyser\NodeScopeResolver::__construct() has typehint with deprecated interface PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider.' + identifier: parameter.deprecatedInterface + count: 1 + path: src/Analyser/NodeScopeResolver.php + + - + rawMessage: Property $parameterClosureTypeExtensionProvider references deprecated interface PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider 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 @@ -240,6 +306,42 @@ parameters: count: 1 path: src/DependencyInjection/NeonAdapter.php + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/DependencyInjection/ValidateServiceTagsExtension.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\MethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/DependencyInjection/ValidateServiceTagsExtension.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\Type\StaticMethodParameterClosureTypeExtension. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/DependencyInjection/ValidateServiceTagsExtension.php + + - + rawMessage: Fetching class constant FUNCTION_TAG of deprecated class PHPStan\DependencyInjection\Type\LazyParameterClosureTypeExtensionProvider. + identifier: classConstant.deprecatedClass + count: 1 + path: src/DependencyInjection/ValidateServiceTagsExtension.php + + - + rawMessage: Fetching class constant METHOD_TAG of deprecated class PHPStan\DependencyInjection\Type\LazyParameterClosureTypeExtensionProvider. + identifier: classConstant.deprecatedClass + count: 1 + path: src/DependencyInjection/ValidateServiceTagsExtension.php + + - + rawMessage: Fetching class constant STATIC_METHOD_TAG of deprecated class PHPStan\DependencyInjection\Type\LazyParameterClosureTypeExtensionProvider. + identifier: classConstant.deprecatedClass + count: 1 + path: src/DependencyInjection/ValidateServiceTagsExtension.php + - rawMessage: Creating new ReflectionClass is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead. identifier: phpstanApi.runtimeReflection @@ -795,6 +897,18 @@ parameters: count: 1 path: src/Testing/PHPStanTestCase.php + - + rawMessage: Access to constant on deprecated interface PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider. + identifier: classConstant.deprecatedInterface + count: 1 + path: src/Testing/RuleTestCase.php + + - + rawMessage: Access to constant on deprecated interface PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider. + 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 @@ -1653,6 +1767,12 @@ parameters: count: 1 path: src/Type/Php/NumberFormatFunctionDynamicReturnTypeExtension.php + - + rawMessage: Class PHPStan\Type\Php\PregReplaceCallbackClosureTypeExtension implements deprecated interface PHPStan\Type\FunctionParameterClosureTypeExtension. + identifier: class.implementsDeprecatedInterface + count: 1 + path: src/Type/Php/PregReplaceCallbackClosureTypeExtension.php + - rawMessage: 'Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.' identifier: phpstanApi.instanceofType @@ -1881,6 +2001,12 @@ parameters: count: 2 path: src/Type/VoidType.php + - + rawMessage: Access to constant on deprecated interface PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider. + 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 diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index b2c70f9eae..f89c3d0312 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -989,7 +989,7 @@ private function resolveType(string $exprString, Expr $node): Type if ($this->getBooleanExpressionDepth($node->left) <= self::BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH) { $noopCallback = static function (): void { }; - $leftResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->left), $node->left, $this, $noopCallback, ExpressionContext::createDeep()); + $leftResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->left), $node->left, $this, $noopCallback, ExpressionContext::createDeep(), null); $rightBooleanType = $leftResult->getTruthyScope()->getType($node->right)->toBoolean(); } else { $rightBooleanType = $this->filterByTruthyValue($node->left)->getType($node->right)->toBoolean(); @@ -1021,7 +1021,7 @@ private function resolveType(string $exprString, Expr $node): Type if ($this->getBooleanExpressionDepth($node->left) <= self::BOOLEAN_EXPRESSION_MAX_PROCESS_DEPTH) { $noopCallback = static function (): void { }; - $leftResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->left), $node->left, $this, $noopCallback, ExpressionContext::createDeep()); + $leftResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->left), $node->left, $this, $noopCallback, ExpressionContext::createDeep(), null); $rightBooleanType = $leftResult->getFalseyScope()->getType($node->right)->toBoolean(); } else { $rightBooleanType = $this->filterByFalseyValue($node->left)->getType($node->right)->toBoolean(); @@ -1434,6 +1434,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu ); }, ExpressionContext::createDeep(), + null, ); $throwPoints = array_map(static fn ($throwPoint) => $throwPoint->toPublic(), $arrowFunctionExprResult->getThrowPoints()); $impurePoints = array_merge($arrowFunctionImpurePoints, $arrowFunctionExprResult->getImpurePoints()); @@ -2049,7 +2050,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu if ($node instanceof Expr\Ternary) { $noopCallback = static function (): void { }; - $condResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->cond), $node->cond, $this, $noopCallback, ExpressionContext::createDeep()); + $condResult = $this->nodeScopeResolver->processExprNode(new Node\Stmt\Expression($node->cond), $node->cond, $this, $noopCallback, ExpressionContext::createDeep(), null); if ($node->if === null) { $conditionType = $this->getType($node->cond); $booleanConditionType = $conditionType->toBoolean(); diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index e093860bac..97e5e648cf 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -65,6 +65,7 @@ use PHPStan\BetterReflection\SourceLocator\Located\LocatedSource; use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\AutowiredService; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider; use PHPStan\DependencyInjection\Type\ParameterClosureThisExtensionProvider; use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider; @@ -267,6 +268,7 @@ public function __construct( private readonly DynamicThrowTypeExtensionProvider $dynamicThrowTypeExtensionProvider, private readonly ReadWritePropertiesExtensionProvider $readWritePropertiesExtensionProvider, private readonly ParameterClosureThisExtensionProvider $parameterClosureThisExtensionProvider, + private readonly DynamicParameterTypeExtensionProvider $dynamicParameterTypeExtensionProvider, private readonly ParameterClosureTypeExtensionProvider $parameterClosureTypeExtensionProvider, private readonly ScopeFactory $scopeFactory, #[AutowiredParameter] @@ -859,7 +861,7 @@ private function processStmtNode( $throwPoints = []; $isAlwaysTerminating = false; foreach ($stmt->exprs as $echoExpr) { - $result = $this->processExprNode($stmt, $echoExpr, $scope, $nodeCallback, ExpressionContext::createDeep()); + $result = $this->processExprNode($stmt, $echoExpr, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); $scope = $result->getScope(); $hasYield = $hasYield || $result->hasYield(); @@ -873,7 +875,7 @@ private function processStmtNode( return new InternalStatementResult($scope, $hasYield, $isAlwaysTerminating, [], $throwPoints, $impurePoints); } elseif ($stmt instanceof Return_) { if ($stmt->expr !== null) { - $result = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep()); + $result = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $scope = $result->getScope(); @@ -889,7 +891,7 @@ private function processStmtNode( ], $overridingThrowPoints ?? $throwPoints, $impurePoints); } elseif ($stmt instanceof Continue_ || $stmt instanceof Break_) { if ($stmt->num !== null) { - $result = $this->processExprNode($stmt, $stmt->num, $scope, $nodeCallback, ExpressionContext::createDeep()); + $result = $this->processExprNode($stmt, $stmt->num, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); @@ -923,7 +925,7 @@ private function processStmtNode( } $hasAssign = true; - }, ExpressionContext::createTopLevel()); + }, ExpressionContext::createTopLevel(), null); $throwPoints = array_filter($result->getThrowPoints(), static fn ($throwPoint) => $throwPoint->isExplicit()); if ( count($result->getImpurePoints()) === 0 @@ -1052,7 +1054,7 @@ public function __invoke(Node $node, Scope $scope): void foreach ($stmt->props as $prop) { $nodeCallback($prop, $scope); if ($prop->default !== null) { - $this->processExprNode($stmt, $prop->default, $scope, $nodeCallback, ExpressionContext::createDeep()); + $this->processExprNode($stmt, $prop->default, $scope, $nodeCallback, ExpressionContext::createDeep(), null); } if (!$scope->isInClass()) { @@ -1111,7 +1113,7 @@ public function __invoke(Node $node, Scope $scope): void } elseif ($stmt instanceof If_) { $conditionType = ($this->treatPhpDocTypesAsCertain ? $scope->getType($stmt->cond) : $scope->getNativeType($stmt->cond))->toBoolean(); $ifAlwaysTrue = $conditionType->isTrue()->yes(); - $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $exitPoints = []; $throwPoints = $overridingThrowPoints ?? $condResult->getThrowPoints(); $impurePoints = $condResult->getImpurePoints(); @@ -1146,7 +1148,7 @@ public function __invoke(Node $node, Scope $scope): void foreach ($stmt->elseifs as $elseif) { $nodeCallback($elseif, $scope); $elseIfConditionType = ($this->treatPhpDocTypesAsCertain ? $condScope->getType($elseif->cond) : $scope->getNativeType($elseif->cond))->toBoolean(); - $condResult = $this->processExprNode($stmt, $elseif->cond, $condScope, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $elseif->cond, $condScope, $nodeCallback, ExpressionContext::createDeep(), null); $throwPoints = array_merge($throwPoints, $condResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $condResult->getImpurePoints()); $condScope = $condResult->getScope(); @@ -1225,7 +1227,7 @@ public function __invoke(Node $node, Scope $scope): void $impurePoints = []; $this->processTraitUse($stmt, $scope, $nodeCallback); } elseif ($stmt instanceof Foreach_) { - $condResult = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $throwPoints = $overridingThrowPoints ?? $condResult->getThrowPoints(); $impurePoints = $condResult->getImpurePoints(); $scope = $condResult->getScope(); @@ -1410,7 +1412,7 @@ public function __invoke(Node $node, Scope $scope): void ); } elseif ($stmt instanceof While_) { $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, static function (): void { - }, ExpressionContext::createDeep()); + }, ExpressionContext::createDeep(), null); $beforeCondBooleanType = ($this->treatPhpDocTypesAsCertain ? $scope->getType($stmt->cond) : $scope->getNativeType($stmt->cond))->toBoolean(); $condScope = $condResult->getFalseyScope(); if (!$context->isTopLevel() && $beforeCondBooleanType->isFalse()->yes()) { @@ -1435,7 +1437,7 @@ public function __invoke(Node $node, Scope $scope): void $prevScope = $bodyScope; $bodyScope = $bodyScope->mergeWith($scope); $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, static function (): void { - }, ExpressionContext::createDeep())->getTruthyScope(); + }, ExpressionContext::createDeep(), null)->getTruthyScope(); $bodyScopeResult = $this->processStmtNodesInternal($stmt, $stmt->stmts, $bodyScope, static function (): void { }, $context->enterDeep())->filterOutLoopExitPoints(); $bodyScope = $bodyScopeResult->getScope(); @@ -1455,7 +1457,7 @@ public function __invoke(Node $node, Scope $scope): void $bodyScope = $bodyScope->mergeWith($scope); $bodyScopeMaybeRan = $bodyScope; - $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope(); + $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $nodeCallback, ExpressionContext::createDeep(), null)->getTruthyScope(); $finalScopeResult = $this->processStmtNodesInternal($stmt, $stmt->stmts, $bodyScope, $nodeCallback, $context)->filterOutLoopExitPoints(); $finalScope = $finalScopeResult->getScope()->filterByFalseyValue($stmt->cond); @@ -1529,7 +1531,7 @@ public function __invoke(Node $node, Scope $scope): void $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); } $bodyScope = $this->processExprNode($stmt, $stmt->cond, $bodyScope, static function (): void { - }, ExpressionContext::createDeep())->getTruthyScope(); + }, ExpressionContext::createDeep(), null)->getTruthyScope(); if ($bodyScope->equals($prevScope)) { break; } @@ -1563,13 +1565,13 @@ public function __invoke(Node $node, Scope $scope): void $finalScope = $scope; } if (!$alwaysTerminating) { - $condResult = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $stmt->cond, $bodyScope, $nodeCallback, ExpressionContext::createDeep(), null); $hasYield = $condResult->hasYield(); $throwPoints = $condResult->getThrowPoints(); $impurePoints = $condResult->getImpurePoints(); $finalScope = $condResult->getFalseyScope(); } else { - $this->processExprNode($stmt, $stmt->cond, $bodyScope, $nodeCallback, ExpressionContext::createDeep()); + $this->processExprNode($stmt, $stmt->cond, $bodyScope, $nodeCallback, ExpressionContext::createDeep(), null); } foreach ($bodyScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) { $finalScope = $breakExitPoint->getScope()->mergeWith($finalScope); @@ -1589,7 +1591,7 @@ public function __invoke(Node $node, Scope $scope): void $throwPoints = []; $impurePoints = []; foreach ($stmt->init as $initExpr) { - $initResult = $this->processExprNode($stmt, $initExpr, $initScope, $nodeCallback, ExpressionContext::createTopLevel()); + $initResult = $this->processExprNode($stmt, $initExpr, $initScope, $nodeCallback, ExpressionContext::createTopLevel(), null); $initScope = $initResult->getScope(); $hasYield = $hasYield || $initResult->hasYield(); $throwPoints = array_merge($throwPoints, $initResult->getThrowPoints()); @@ -1601,7 +1603,7 @@ public function __invoke(Node $node, Scope $scope): void $lastCondExpr = array_last($stmt->cond) ?? null; foreach ($stmt->cond as $condExpr) { $condResult = $this->processExprNode($stmt, $condExpr, $bodyScope, static function (): void { - }, ExpressionContext::createDeep()); + }, ExpressionContext::createDeep(), null); $initScope = $condResult->getScope(); $condResultScope = $condResult->getScope(); @@ -1625,7 +1627,7 @@ public function __invoke(Node $node, Scope $scope): void $bodyScope = $bodyScope->mergeWith($initScope); if ($lastCondExpr !== null) { $bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, static function (): void { - }, ExpressionContext::createDeep())->getTruthyScope(); + }, ExpressionContext::createDeep(), null)->getTruthyScope(); } $bodyScopeResult = $this->processStmtNodesInternal($stmt, $stmt->stmts, $bodyScope, static function (): void { }, $context->enterDeep())->filterOutLoopExitPoints(); @@ -1636,7 +1638,7 @@ public function __invoke(Node $node, Scope $scope): void foreach ($stmt->loop as $loopExpr) { $exprResult = $this->processExprNode($stmt, $loopExpr, $bodyScope, static function (): void { - }, ExpressionContext::createTopLevel()); + }, ExpressionContext::createTopLevel(), null); $bodyScope = $exprResult->getScope(); $hasYield = $hasYield || $exprResult->hasYield(); $throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints()); @@ -1659,7 +1661,7 @@ public function __invoke(Node $node, Scope $scope): void $alwaysIterates = TrinaryLogic::createFromBoolean($context->isTopLevel()); if ($lastCondExpr !== null) { $alwaysIterates = $alwaysIterates->and($bodyScope->getType($lastCondExpr)->toBoolean()->isTrue()); - $bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $nodeCallback, ExpressionContext::createDeep())->getTruthyScope(); + $bodyScope = $this->processExprNode($stmt, $lastCondExpr, $bodyScope, $nodeCallback, ExpressionContext::createDeep(), null)->getTruthyScope(); $bodyScope = $this->inferForLoopExpressions($stmt, $lastCondExpr, $bodyScope); } @@ -1671,7 +1673,7 @@ public function __invoke(Node $node, Scope $scope): void $loopScope = $finalScope; foreach ($stmt->loop as $loopExpr) { - $loopScope = $this->processExprNode($stmt, $loopExpr, $loopScope, $nodeCallback, ExpressionContext::createTopLevel())->getScope(); + $loopScope = $this->processExprNode($stmt, $loopExpr, $loopScope, $nodeCallback, ExpressionContext::createTopLevel(), null)->getScope(); } $finalScope = $finalScope->generalizeWith($loopScope); @@ -1719,7 +1721,7 @@ public function __invoke(Node $node, Scope $scope): void array_merge($impurePoints, $finalScopeResult->getImpurePoints()), ); } elseif ($stmt instanceof Switch_) { - $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $nodeCallback, ExpressionContext::createDeep()); + $condResult = $this->processExprNode($stmt, $stmt->cond, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $condResult->getScope(); $scopeForBranches = $scope; $finalScope = null; @@ -1735,7 +1737,7 @@ public function __invoke(Node $node, Scope $scope): void 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, $nodeCallback, ExpressionContext::createDeep()); + $caseResult = $this->processExprNode($stmt, $caseNode->cond, $scopeForBranches, $nodeCallback, ExpressionContext::createDeep(), null); $scopeForBranches = $caseResult->getScope(); $hasYield = $hasYield || $caseResult->hasYield(); $throwPoints = array_merge($throwPoints, $caseResult->getThrowPoints()); @@ -2016,7 +2018,7 @@ public function __invoke(Node $node, Scope $scope): void $impurePoints = []; foreach ($stmt->vars as $var) { $scope = $this->lookForSetAllowedUndefinedExpressions($scope, $var); - $exprResult = $this->processExprNode($stmt, $var, $scope, $nodeCallback, ExpressionContext::createDeep()); + $exprResult = $this->processExprNode($stmt, $var, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $exprResult->getScope(); $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $var); $hasYield = $hasYield || $exprResult->hasYield(); @@ -2087,7 +2089,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch throw new ShouldNotHappenException(); } $scope = $this->lookForSetAllowedUndefinedExpressions($scope, $var); - $varResult = $this->processExprNode($stmt, $var, $scope, $nodeCallback, ExpressionContext::createDeep()); + $varResult = $this->processExprNode($stmt, $var, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $varResult->getImpurePoints()); $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $var); @@ -2119,12 +2121,12 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch } if ($var->default !== null) { - $defaultExprResult = $this->processExprNode($stmt, $var->default, $scope, $nodeCallback, ExpressionContext::createDeep()); + $defaultExprResult = $this->processExprNode($stmt, $var->default, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $defaultExprResult->getImpurePoints()); } $scope = $scope->enterExpressionAssign($var->var); - $varResult = $this->processExprNode($stmt, $var->var, $scope, $nodeCallback, ExpressionContext::createDeep()); + $varResult = $this->processExprNode($stmt, $var->var, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $varResult->getImpurePoints()); $scope = $scope->exitExpressionAssign($var->var); @@ -2139,7 +2141,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch $impurePoints = []; foreach ($stmt->consts as $const) { $nodeCallback($const, $scope); - $constResult = $this->processExprNode($stmt, $const->value, $scope, $nodeCallback, ExpressionContext::createDeep()); + $constResult = $this->processExprNode($stmt, $const->value, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $constResult->getImpurePoints()); if ($const->namespacedName !== null) { $constantName = new Name\FullyQualified($const->namespacedName->toString()); @@ -2155,7 +2157,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch $this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $nodeCallback); foreach ($stmt->consts as $const) { $nodeCallback($const, $scope); - $constResult = $this->processExprNode($stmt, $const->value, $scope, $nodeCallback, ExpressionContext::createDeep()); + $constResult = $this->processExprNode($stmt, $const->value, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = array_merge($impurePoints, $constResult->getImpurePoints()); if ($scope->getClassReflection() === null) { throw new ShouldNotHappenException(); @@ -2172,7 +2174,7 @@ public function leaveNode(Node $node): ?ExistingArrayDimFetch $this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $nodeCallback); $impurePoints = []; if ($stmt->expr !== null) { - $exprResult = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep()); + $exprResult = $this->processExprNode($stmt, $stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $impurePoints = $exprResult->getImpurePoints(); } } elseif ($stmt instanceof InlineHTML) { @@ -2512,7 +2514,7 @@ private function findEarlyTerminatingExpr(Expr $expr, Scope $scope): ?Expr /** * @param callable(Node $node, Scope $scope): void $nodeCallback */ - public function processExprNode(Node\Stmt $stmt, Expr $expr, MutatingScope $scope, callable $nodeCallback, ExpressionContext $context): ExpressionResult + public function processExprNode(Node\Stmt $stmt, Expr $expr, MutatingScope $scope, callable $nodeCallback, ExpressionContext $context, ?Type $overriddenType): ExpressionResult { if ($expr instanceof Expr\CallLike && $expr->isFirstClassCallable()) { if ($expr instanceof FuncCall) { @@ -2527,7 +2529,7 @@ public function processExprNode(Node\Stmt $stmt, Expr $expr, MutatingScope $scop throw new ShouldNotHappenException(); } - return $this->processExprNode($stmt, $newExpr, $scope, $nodeCallback, $context); + return $this->processExprNode($stmt, $newExpr, $scope, $nodeCallback, $context, null); } $this->callNodeCallbackWithExpression($nodeCallback, $expr, $scope, $context); @@ -2538,7 +2540,7 @@ public function processExprNode(Node\Stmt $stmt, Expr $expr, MutatingScope $scop $impurePoints = []; $isAlwaysTerminating = false; if ($expr->name instanceof Expr) { - return $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); + return $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep(), null); } elseif (in_array($expr->name, Scope::SUPERGLOBAL_VARIABLES, true)) { $impurePoints[] = new ImpurePoint($scope, $expr, 'superglobal', 'access to superglobal variable', true); } @@ -2578,7 +2580,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context): Exp ); } - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); @@ -2622,7 +2624,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context): Exp ); } - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); if ($expr instanceof Expr\AssignOp\Coalesce) { return new ExpressionResult( $result->getScope()->mergeWith($originalScope), @@ -2665,7 +2667,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context): Exp ); } - $nameResult = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); + $nameResult = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $nameResult->getScope(); $throwPoints = $nameResult->getThrowPoints(); $impurePoints = $nameResult->getImpurePoints(); @@ -2682,6 +2684,7 @@ function (MutatingScope $scope) use ($stmt, $expr, $nodeCallback, $context): Exp static function (): void { }, $context->enterDeep(), + null, ); $throwPoints = array_merge($throwPoints, $invokeResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $invokeResult->getImpurePoints()); @@ -2992,7 +2995,7 @@ static function (): void { ); } - $result = $this->processExprNode($stmt, $expr->var, $closureCallScope ?? $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->var, $closureCallScope ?? $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); @@ -3005,7 +3008,7 @@ static function (): void { $methodReflection = null; $calledOnType = $scope->getType($expr->var); if ($expr->name instanceof Expr) { - $methodNameResult = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); + $methodNameResult = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep(), null); $throwPoints = array_merge($throwPoints, $methodNameResult->getThrowPoints()); $scope = $methodNameResult->getScope(); } else { @@ -3106,7 +3109,7 @@ static function (): void { $isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating(); } elseif ($expr instanceof Expr\NullsafeMethodCall) { $nonNullabilityResult = $this->ensureShallowNonNullability($scope, $scope, $expr->var); - $exprResult = $this->processExprNode($stmt, new MethodCall($expr->var, $expr->name, $expr->args, array_merge($expr->getAttributes(), ['virtualNullsafeMethodCall' => true])), $nonNullabilityResult->getScope(), $nodeCallback, $context); + $exprResult = $this->processExprNode($stmt, new MethodCall($expr->var, $expr->name, $expr->args, array_merge($expr->getAttributes(), ['virtualNullsafeMethodCall' => true])), $nonNullabilityResult->getScope(), $nodeCallback, $context, null); $scope = $this->revertNonNullability($exprResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions()); return new ExpressionResult( @@ -3130,12 +3133,12 @@ static function (): void { } if (count($objectClasses) === 1) { $objectExprResult = $this->processExprNode($stmt, new StaticCall(new Name($objectClasses[0]), $expr->name, []), $scope, static function (): void { - }, $context->enterDeep()); + }, $context->enterDeep(), null); $additionalThrowPoints = $objectExprResult->getThrowPoints(); } else { $additionalThrowPoints = [InternalThrowPoint::createImplicit($scope, $expr)]; } - $classResult = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); + $classResult = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $classResult->hasYield(); $throwPoints = array_merge($throwPoints, $classResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $classResult->getImpurePoints()); @@ -3149,7 +3152,7 @@ static function (): void { $parametersAcceptor = null; $methodReflection = null; if ($expr->name instanceof Expr) { - $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); @@ -3283,14 +3286,14 @@ static function (): void { $isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating(); } elseif ($expr instanceof PropertyFetch) { $scopeBeforeVar = $scope; - $result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $isAlwaysTerminating = $result->isAlwaysTerminating(); $scope = $result->getScope(); if ($expr->name instanceof Expr) { - $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); @@ -3313,7 +3316,7 @@ static function (): void { } } elseif ($expr instanceof Expr\NullsafePropertyFetch) { $nonNullabilityResult = $this->ensureShallowNonNullability($scope, $scope, $expr->var); - $exprResult = $this->processExprNode($stmt, new PropertyFetch($expr->var, $expr->name, array_merge($expr->getAttributes(), ['virtualNullsafePropertyFetch' => true])), $nonNullabilityResult->getScope(), $nodeCallback, $context); + $exprResult = $this->processExprNode($stmt, new PropertyFetch($expr->var, $expr->name, array_merge($expr->getAttributes(), ['virtualNullsafePropertyFetch' => true])), $nonNullabilityResult->getScope(), $nodeCallback, $context, null); $scope = $this->revertNonNullability($exprResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions()); return new ExpressionResult( @@ -3339,7 +3342,7 @@ static function (): void { ]; $isAlwaysTerminating = false; if ($expr->class instanceof Expr) { - $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); @@ -3347,7 +3350,7 @@ static function (): void { $scope = $result->getScope(); } if ($expr->name instanceof Expr) { - $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); @@ -3355,7 +3358,7 @@ static function (): void { $scope = $result->getScope(); } } elseif ($expr instanceof Expr\Closure) { - $processClosureResult = $this->processClosureNode($stmt, $expr, $scope, $nodeCallback, $context, null); + $processClosureResult = $this->processClosureNode($stmt, $expr, $scope, $nodeCallback, $context, $overriddenType); return new ExpressionResult( $processClosureResult->getScope(), @@ -3365,7 +3368,7 @@ static function (): void { [], ); } elseif ($expr instanceof Expr\ArrowFunction) { - $result = $this->processArrowFunctionNode($stmt, $expr, $scope, $nodeCallback, null); + $result = $this->processArrowFunctionNode($stmt, $expr, $scope, $nodeCallback, $overriddenType); return new ExpressionResult( $result->getScope(), $result->hasYield(), @@ -3374,7 +3377,7 @@ static function (): void { [], ); } elseif ($expr instanceof ErrorSuppress) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context, null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); @@ -3390,7 +3393,7 @@ static function (): void { ]; $isAlwaysTerminating = true; if ($expr->expr !== null) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); @@ -3405,7 +3408,7 @@ static function (): void { if (!$part instanceof Expr) { continue; } - $result = $this->processExprNode($stmt, $part, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $part, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); @@ -3418,7 +3421,7 @@ static function (): void { $impurePoints = []; $isAlwaysTerminating = false; if ($expr->dim !== null) { - $result = $this->processExprNode($stmt, $expr->dim, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->dim, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); @@ -3426,7 +3429,7 @@ static function (): void { $scope = $result->getScope(); } - $result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); @@ -3438,11 +3441,14 @@ static function (): void { $throwPoints = []; $impurePoints = []; $isAlwaysTerminating = false; + $nextAutoIndex = 0; foreach ($expr->items as $arrayItem) { $itemNodes[] = new LiteralArrayItem($scope, $arrayItem); $nodeCallback($arrayItem, $scope); + $keyType = new ConstantIntegerType($nextAutoIndex); if ($arrayItem->key !== null) { - $keyResult = $this->processExprNode($stmt, $arrayItem->key, $scope, $nodeCallback, $context->enterDeep()); + $keyType = $scope->getType($arrayItem->key); + $keyResult = $this->processExprNode($stmt, $arrayItem->key, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $keyResult->hasYield(); $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); @@ -3450,7 +3456,18 @@ static function (): void { $scope = $keyResult->getScope(); } - $valueResult = $this->processExprNode($stmt, $arrayItem->value, $scope, $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 = $this->processExprNode($stmt, $arrayItem->value, $scope, $nodeCallback, $context->enterDeep(), $overriddenValueType); $hasYield = $hasYield || $valueResult->hasYield(); $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints()); @@ -3459,8 +3476,8 @@ static function (): void { } $nodeCallback(new LiteralArrayNode($expr, $itemNodes), $scope); } elseif ($expr instanceof BooleanAnd || $expr instanceof BinaryOp\LogicalAnd) { - $leftResult = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep()); - $rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getTruthyScope(), $nodeCallback, $context); + $leftResult = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep(), null); + $rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getTruthyScope(), $nodeCallback, $context, null); $rightExprType = $rightResult->getScope()->getType($expr->right); if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) { $leftMergedWithRightScope = $leftResult->getFalseyScope(); @@ -3480,8 +3497,8 @@ static function (): void { static fn (): MutatingScope => $leftMergedWithRightScope->filterByFalseyValue($expr), ); } elseif ($expr instanceof BooleanOr || $expr instanceof BinaryOp\LogicalOr) { - $leftResult = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep()); - $rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getFalseyScope(), $nodeCallback, $context); + $leftResult = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep(), null); + $rightResult = $this->processExprNode($stmt, $expr->right, $leftResult->getFalseyScope(), $nodeCallback, $context, null); $rightExprType = $rightResult->getScope()->getType($expr->right); if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) { $leftMergedWithRightScope = $leftResult->getTruthyScope(); @@ -3503,12 +3520,12 @@ static function (): void { } elseif ($expr instanceof Coalesce) { $nonNullabilityResult = $this->ensureNonNullability($scope, $expr->left); $condScope = $this->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $expr->left); - $condResult = $this->processExprNode($stmt, $expr->left, $condScope, $nodeCallback, $context->enterDeep()); + $condResult = $this->processExprNode($stmt, $expr->left, $condScope, $nodeCallback, $context->enterDeep(), null); $scope = $this->revertNonNullability($condResult->getScope(), $nonNullabilityResult->getSpecifiedExpressions()); $scope = $this->lookForUnsetAllowedUndefinedExpressions($scope, $expr->left); $rightScope = $scope->filterByFalseyValue($expr); - $rightResult = $this->processExprNode($stmt, $expr->right, $rightScope, $nodeCallback, $context->enterDeep()); + $rightResult = $this->processExprNode($stmt, $expr->right, $rightScope, $nodeCallback, $context->enterDeep(), null); $rightExprType = $scope->getType($expr->right); if ($rightExprType instanceof NeverType && $rightExprType->isExplicit()) { $scope = $scope->filterByTruthyValue(new Expr\Isset_([$expr->left])); @@ -3524,7 +3541,7 @@ static function (): void { if ($expr->right instanceof FuncCall && $expr->right->isFirstClassCallable()) { $exprResult = $this->processExprNode($stmt, new FuncCall($expr->right->name, [ new Arg($expr->left, attributes: $expr->getAttribute(ReversePipeTransformerVisitor::ARG_ATTRIBUTES_NAME, [])), - ], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $nodeCallback, $context); + ], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $nodeCallback, $context, null); $scope = $exprResult->getScope(); $hasYield = $exprResult->hasYield(); $throwPoints = $exprResult->getThrowPoints(); @@ -3533,7 +3550,7 @@ static function (): void { } elseif ($expr->right instanceof MethodCall && $expr->right->isFirstClassCallable()) { $exprResult = $this->processExprNode($stmt, new MethodCall($expr->right->var, $expr->right->name, [ new Arg($expr->left, attributes: $expr->getAttribute(ReversePipeTransformerVisitor::ARG_ATTRIBUTES_NAME, [])), - ], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $nodeCallback, $context); + ], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $nodeCallback, $context, null); $scope = $exprResult->getScope(); $hasYield = $exprResult->hasYield(); $throwPoints = $exprResult->getThrowPoints(); @@ -3542,7 +3559,7 @@ static function (): void { } elseif ($expr->right instanceof StaticCall && $expr->right->isFirstClassCallable()) { $exprResult = $this->processExprNode($stmt, new StaticCall($expr->right->class, $expr->right->name, [ new Arg($expr->left, attributes: $expr->getAttribute(ReversePipeTransformerVisitor::ARG_ATTRIBUTES_NAME, [])), - ], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $nodeCallback, $context); + ], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $nodeCallback, $context, null); $scope = $exprResult->getScope(); $hasYield = $exprResult->hasYield(); $throwPoints = $exprResult->getThrowPoints(); @@ -3551,7 +3568,7 @@ static function (): void { } else { $exprResult = $this->processExprNode($stmt, new FuncCall($expr->right, [ new Arg($expr->left, attributes: $expr->getAttribute(ReversePipeTransformerVisitor::ARG_ATTRIBUTES_NAME, [])), - ], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $nodeCallback, $context); + ], array_merge($expr->right->getAttributes(), ['virtualPipeOperatorCall' => true])), $scope, $nodeCallback, $context, null); $scope = $exprResult->getScope(); $hasYield = $exprResult->hasYield(); $throwPoints = $exprResult->getThrowPoints(); @@ -3559,13 +3576,13 @@ static function (): void { $isAlwaysTerminating = $exprResult->isAlwaysTerminating(); } } elseif ($expr instanceof BinaryOp) { - $result = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->left, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $isAlwaysTerminating = $result->isAlwaysTerminating(); - $result = $this->processExprNode($stmt, $expr->right, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->right, $scope, $nodeCallback, $context->enterDeep(), null); if ( ($expr instanceof BinaryOp\Div || $expr instanceof BinaryOp\Mod) && !$scope->getType($expr->right)->toNumber()->isSuperTypeOf(new ConstantIntegerType(0))->no() @@ -3578,7 +3595,7 @@ static function (): void { $impurePoints = array_merge($impurePoints, $result->getImpurePoints()); $isAlwaysTerminating = $isAlwaysTerminating || $result->isAlwaysTerminating(); } elseif ($expr instanceof Expr\Include_) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $throwPoints = $result->getThrowPoints(); $throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr); $impurePoints = $result->getImpurePoints(); @@ -3593,7 +3610,7 @@ static function (): void { $isAlwaysTerminating = $result->isAlwaysTerminating(); $scope = $result->getScope()->afterExtractCall(); } elseif ($expr instanceof Expr\Print_) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $isAlwaysTerminating = $result->isAlwaysTerminating(); @@ -3602,7 +3619,7 @@ static function (): void { $scope = $result->getScope(); } elseif ($expr instanceof Cast\String_) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $isAlwaysTerminating = $result->isAlwaysTerminating(); @@ -3630,7 +3647,7 @@ static function (): void { || $expr instanceof Expr\UnaryMinus || $expr instanceof Expr\UnaryPlus ) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $isAlwaysTerminating = $result->isAlwaysTerminating(); @@ -3638,7 +3655,7 @@ static function (): void { $scope = $result->getScope(); } elseif ($expr instanceof Expr\Eval_) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $throwPoints = $result->getThrowPoints(); $throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr); $impurePoints = $result->getImpurePoints(); @@ -3648,7 +3665,7 @@ static function (): void { $scope = $result->getScope(); } elseif ($expr instanceof Expr\YieldFrom) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $throwPoints = $result->getThrowPoints(); $throwPoints[] = InternalThrowPoint::createImplicit($scope, $expr); $impurePoints = $result->getImpurePoints(); @@ -3664,7 +3681,7 @@ static function (): void { $scope = $result->getScope(); } elseif ($expr instanceof BooleanNot) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); @@ -3674,7 +3691,7 @@ static function (): void { $isAlwaysTerminating = false; if ($expr->class instanceof Expr) { - $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); @@ -3688,7 +3705,7 @@ static function (): void { } if ($expr->name instanceof Expr) { - $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->name, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); @@ -3700,7 +3717,7 @@ static function (): void { } elseif ($expr instanceof Expr\Empty_) { $nonNullabilityResult = $this->ensureNonNullability($scope, $expr->expr); $scope = $this->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $expr->expr); - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); @@ -3717,7 +3734,7 @@ static function (): void { foreach ($expr->vars as $var) { $nonNullabilityResult = $this->ensureNonNullability($scope, $var); $scope = $this->lookForSetAllowedUndefinedExpressions($nonNullabilityResult->getScope(), $var); - $result = $this->processExprNode($stmt, $var, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $var, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); @@ -3732,14 +3749,14 @@ static function (): void { $scope = $this->revertNonNullability($scope, $nonNullabilityResult->getSpecifiedExpressions()); } } elseif ($expr instanceof Instanceof_) { - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $isAlwaysTerminating = $result->isAlwaysTerminating(); if ($expr->class instanceof Expr) { - $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); @@ -3762,14 +3779,14 @@ static function (): void { $objectClasses = $scope->getType($expr)->getObjectClassNames(); if (count($objectClasses) === 1) { $objectExprResult = $this->processExprNode($stmt, new New_(new Name($objectClasses[0])), $scope, static function (): void { - }, $context->enterDeep()); + }, $context->enterDeep(), null); $className = $objectClasses[0]; $additionalThrowPoints = $objectExprResult->getThrowPoints(); } else { $additionalThrowPoints = [InternalThrowPoint::createImplicit($scope, $expr)]; } - $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->class, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); @@ -3900,7 +3917,7 @@ static function (): void { || $expr instanceof Expr\PreDec || $expr instanceof Expr\PostDec ) { - $result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $expr->var, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); @@ -3922,7 +3939,7 @@ static function (): void { $nodeCallback, )->getScope(); } elseif ($expr instanceof Ternary) { - $ternaryCondResult = $this->processExprNode($stmt, $expr->cond, $scope, $nodeCallback, $context->enterDeep()); + $ternaryCondResult = $this->processExprNode($stmt, $expr->cond, $scope, $nodeCallback, $context->enterDeep(), null); $throwPoints = $ternaryCondResult->getThrowPoints(); $impurePoints = $ternaryCondResult->getImpurePoints(); $isAlwaysTerminating = $ternaryCondResult->isAlwaysTerminating(); @@ -3930,14 +3947,14 @@ static function (): void { $ifFalseScope = $ternaryCondResult->getFalseyScope(); $ifTrueType = null; if ($expr->if !== null) { - $ifResult = $this->processExprNode($stmt, $expr->if, $ifTrueScope, $nodeCallback, $context); + $ifResult = $this->processExprNode($stmt, $expr->if, $ifTrueScope, $nodeCallback, $context, null); $throwPoints = array_merge($throwPoints, $ifResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $ifResult->getImpurePoints()); $ifTrueScope = $ifResult->getScope(); $ifTrueType = $ifTrueScope->getType($expr->if); } - $elseResult = $this->processExprNode($stmt, $expr->else, $ifFalseScope, $nodeCallback, $context); + $elseResult = $this->processExprNode($stmt, $expr->else, $ifFalseScope, $nodeCallback, $context, null); $throwPoints = array_merge($throwPoints, $elseResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $elseResult->getImpurePoints()); $ifFalseScope = $elseResult->getScope(); @@ -3986,14 +4003,14 @@ static function (): void { ]; $isAlwaysTerminating = false; if ($expr->key !== null) { - $keyResult = $this->processExprNode($stmt, $expr->key, $scope, $nodeCallback, $context->enterDeep()); + $keyResult = $this->processExprNode($stmt, $expr->key, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $keyResult->getScope(); $throwPoints = $keyResult->getThrowPoints(); $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); $isAlwaysTerminating = $keyResult->isAlwaysTerminating(); } if ($expr->value !== null) { - $valueResult = $this->processExprNode($stmt, $expr->value, $scope, $nodeCallback, $context->enterDeep()); + $valueResult = $this->processExprNode($stmt, $expr->value, $scope, $nodeCallback, $context->enterDeep(), null); $scope = $valueResult->getScope(); $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints()); @@ -4003,7 +4020,7 @@ static function (): void { } elseif ($expr instanceof Expr\Match_) { $deepContext = $context->enterDeep(); $condType = $scope->getType($expr->cond); - $condResult = $this->processExprNode($stmt, $expr->cond, $scope, $nodeCallback, $deepContext); + $condResult = $this->processExprNode($stmt, $expr->cond, $scope, $nodeCallback, $deepContext, null); $scope = $condResult->getScope(); $hasYield = $condResult->hasYield(); $throwPoints = $condResult->getThrowPoints(); @@ -4084,7 +4101,7 @@ static function (): void { } } - $this->processExprNode($stmt, $cond, $armConditionScope, $nodeCallback, $deepContext); + $this->processExprNode($stmt, $cond, $armConditionScope, $nodeCallback, $deepContext, null); $condNodes[] = new MatchExpressionArmCondition( $cond, @@ -4119,6 +4136,7 @@ static function (): void { $matchArmBodyScope, $nodeCallback, ExpressionContext::createTopLevel(), + null, ); $armScope = $armResult->getScope(); $scope = $scope->mergeWith($armScope); @@ -4153,7 +4171,7 @@ static function (): void { $hasDefaultCond = true; $matchArmBody = new MatchExpressionArmBody($matchScope, $arm->body); $armNodes[$i] = new MatchExpressionArm($matchArmBody, [], $arm->getStartLine()); - $armResult = $this->processExprNode($stmt, $arm->body, $matchScope, $nodeCallback, ExpressionContext::createTopLevel()); + $armResult = $this->processExprNode($stmt, $arm->body, $matchScope, $nodeCallback, ExpressionContext::createTopLevel(), null); $matchScope = $armResult->getScope(); $hasYield = $hasYield || $armResult->hasYield(); $throwPoints = array_merge($throwPoints, $armResult->getThrowPoints()); @@ -4171,7 +4189,7 @@ static function (): void { $condNodes = []; foreach ($arm->conds as $armCond) { $condNodes[] = new MatchExpressionArmCondition($armCond, $armCondScope, $armCond->getStartLine()); - $armCondResult = $this->processExprNode($stmt, $armCond, $armCondScope, $nodeCallback, $deepContext); + $armCondResult = $this->processExprNode($stmt, $armCond, $armCondScope, $nodeCallback, $deepContext, null); $hasYield = $hasYield || $armCondResult->hasYield(); $throwPoints = array_merge($throwPoints, $armCondResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $armCondResult->getImpurePoints()); @@ -4188,7 +4206,7 @@ static function (): void { $filteringExpr = $this->getFilteringExprForMatchArm($expr, $filteringExprs); $bodyScope = $this->processExprNode($stmt, $filteringExpr, $matchScope, static function (): void { - }, $deepContext)->getTruthyScope(); + }, $deepContext, null)->getTruthyScope(); $matchArmBody = new MatchExpressionArmBody($bodyScope, $arm->body); $armNodes[$i] = new MatchExpressionArm($matchArmBody, $condNodes, $arm->getStartLine()); @@ -4198,6 +4216,7 @@ static function (): void { $bodyScope, $nodeCallback, ExpressionContext::createTopLevel(), + null, ); $armScope = $armResult->getScope(); $scope = $scope->mergeWith($armScope); @@ -4216,7 +4235,7 @@ static function (): void { $nodeCallback(new MatchExpressionNode($expr->cond, array_values($armNodes), $expr, $matchScope), $scope); } elseif ($expr instanceof AlwaysRememberedExpr) { - $result = $this->processExprNode($stmt, $expr->getExpr(), $scope, $nodeCallback, $context); + $result = $this->processExprNode($stmt, $expr->getExpr(), $scope, $nodeCallback, $context, null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); @@ -4224,7 +4243,7 @@ static function (): void { $scope = $result->getScope(); } elseif ($expr instanceof Expr\Throw_) { $hasYield = false; - $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, ExpressionContext::createDeep()); + $result = $this->processExprNode($stmt, $expr->expr, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $isAlwaysTerminating = $result->isAlwaysTerminating(); @@ -4235,7 +4254,7 @@ static function (): void { $hasYield = false; $isAlwaysTerminating = false; if ($expr->getName() instanceof Expr) { - $result = $this->processExprNode($stmt, $expr->getName(), $scope, $nodeCallback, ExpressionContext::createDeep()); + $result = $this->processExprNode($stmt, $expr->getName(), $scope, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); @@ -4243,14 +4262,14 @@ static function (): void { $isAlwaysTerminating = $result->isAlwaysTerminating(); } } elseif ($expr instanceof MethodCallableNode) { - $result = $this->processExprNode($stmt, $expr->getVar(), $scope, $nodeCallback, ExpressionContext::createDeep()); + $result = $this->processExprNode($stmt, $expr->getVar(), $scope, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $result->getScope(); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); $isAlwaysTerminating = false; if ($expr->getName() instanceof Expr) { - $nameResult = $this->processExprNode($stmt, $expr->getVar(), $scope, $nodeCallback, ExpressionContext::createDeep()); + $nameResult = $this->processExprNode($stmt, $expr->getVar(), $scope, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $nameResult->getScope(); $hasYield = $hasYield || $nameResult->hasYield(); $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); @@ -4263,7 +4282,7 @@ static function (): void { $hasYield = false; $isAlwaysTerminating = false; if ($expr->getClass() instanceof Expr) { - $classResult = $this->processExprNode($stmt, $expr->getClass(), $scope, $nodeCallback, ExpressionContext::createDeep()); + $classResult = $this->processExprNode($stmt, $expr->getClass(), $scope, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $classResult->getScope(); $hasYield = $classResult->hasYield(); $throwPoints = $classResult->getThrowPoints(); @@ -4271,7 +4290,7 @@ static function (): void { $isAlwaysTerminating = $classResult->isAlwaysTerminating(); } if ($expr->getName() instanceof Expr) { - $nameResult = $this->processExprNode($stmt, $expr->getName(), $scope, $nodeCallback, ExpressionContext::createDeep()); + $nameResult = $this->processExprNode($stmt, $expr->getName(), $scope, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $nameResult->getScope(); $hasYield = $hasYield || $nameResult->hasYield(); $throwPoints = array_merge($throwPoints, $nameResult->getThrowPoints()); @@ -4284,7 +4303,7 @@ static function (): void { $hasYield = false; $isAlwaysTerminating = false; if ($expr->getClass() instanceof Expr) { - $classResult = $this->processExprNode($stmt, $expr->getClass(), $scope, $nodeCallback, ExpressionContext::createDeep()); + $classResult = $this->processExprNode($stmt, $expr->getClass(), $scope, $nodeCallback, ExpressionContext::createDeep(), null); $scope = $classResult->getScope(); $hasYield = $classResult->hasYield(); $throwPoints = $classResult->getThrowPoints(); @@ -4795,7 +4814,7 @@ private function processClosureNode( MutatingScope $scope, callable $nodeCallback, ExpressionContext $context, - ?Type $passedToType, + ?Type $overriddenType, ): ProcessClosureResult { foreach ($expr->params as $param) { @@ -4809,7 +4828,7 @@ private function processClosureNode( $scope, $expr, $closureCallArgs, - $passedToType, + $overriddenType, ); $useScope = $scope; @@ -4849,7 +4868,7 @@ private function processClosureNode( $scope = $scope->assignVariable($inAssignRightSideVariableName, $variableType, $variableNativeType, TrinaryLogic::createYes()); } } - $this->processExprNode($stmt, $use->var, $useScope, $nodeCallback, $context); + $this->processExprNode($stmt, $use->var, $useScope, $nodeCallback, $context, null); if (!$use->byRef) { continue; } @@ -4921,6 +4940,7 @@ private function processClosureNode( $publicStatementResult, $executionEnds, array_merge($publicStatementResult->getImpurePoints(), $closureImpurePoints), + $overriddenType, ), $closureScope); return new ProcessClosureResult($scope, $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions, $isAlwaysTerminating); @@ -4968,6 +4988,7 @@ private function processClosureNode( $publicStatementResult, $executionEnds, array_merge($publicStatementResult->getImpurePoints(), $closureImpurePoints), + $overriddenType, ), $closureScope); return new ProcessClosureResult($scope->processClosureScope($closureResultScope, null, $byRefUses), $statementResult->getThrowPoints(), $statementResult->getImpurePoints(), $invalidateExpressions, $isAlwaysTerminating); @@ -5014,7 +5035,7 @@ private function processArrowFunctionNode( Expr\ArrowFunction $expr, MutatingScope $scope, callable $nodeCallback, - ?Type $passedToType, + ?Type $overriddenType, ): ExpressionResult { foreach ($expr->params as $param) { @@ -5029,14 +5050,14 @@ private function processArrowFunctionNode( $scope, $expr, $arrowFunctionCallArgs, - $passedToType, + $overriddenType, )); $arrowFunctionType = $arrowFunctionScope->getAnonymousFunctionReflection(); if ($arrowFunctionType === null) { throw new ShouldNotHappenException(); } - $nodeCallback(new InArrowFunctionNode($arrowFunctionType, $expr), $arrowFunctionScope); - $exprResult = $this->processExprNode($stmt, $expr->expr, $arrowFunctionScope, $nodeCallback, ExpressionContext::createTopLevel()); + $nodeCallback(new InArrowFunctionNode($arrowFunctionType, $expr, $overriddenType), $arrowFunctionScope); + $exprResult = $this->processExprNode($stmt, $expr->expr, $arrowFunctionScope, $nodeCallback, ExpressionContext::createTopLevel(), null); return new ExpressionResult($scope, false, $exprResult->isAlwaysTerminating(), $exprResult->getThrowPoints(), $exprResult->getImpurePoints()); } @@ -5045,7 +5066,7 @@ private function processArrowFunctionNode( * @param Node\Arg[] $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 { $callableParameters = null; if ($args !== null) { @@ -5075,16 +5096,16 @@ public function createCallableParameters(Scope $scope, Expr $closureExpr, ?array ); } } - } 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); if (count($acceptors) > 0) { foreach ($acceptors as $acceptor) { if ($callableParameters === null) { @@ -5143,7 +5164,7 @@ private function processParamNode( return; } - $this->processExprNode($stmt, $param->default, $scope, $nodeCallback, ExpressionContext::createDeep()); + $this->processExprNode($stmt, $param->default, $scope, $nodeCallback, ExpressionContext::createDeep(), null); } /** @@ -5179,7 +5200,7 @@ private function processAttributeGroups( } foreach ($attr->args as $arg) { - $this->processExprNode($stmt, $arg->value, $scope, $nodeCallback, ExpressionContext::createDeep()); + $this->processExprNode($stmt, $arg->value, $scope, $nodeCallback, ExpressionContext::createDeep(), null); $nodeCallback($arg, $scope); } $nodeCallback($attr, $scope); @@ -5386,6 +5407,7 @@ private function processArgs( $assignByReference = false; $parameter = null; $parameterType = null; + $overwritingParameterType = null; $parameterNativeType = null; if (isset($parameters) && $parametersAcceptor !== null) { if (isset($parameters[$i])) { @@ -5408,6 +5430,13 @@ private 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; @@ -5468,6 +5497,7 @@ private function processArgs( } } + // @todo remove once the closure type extensions are removed if ($parameter !== null) { $overwritingParameterType = $this->getParameterTypeFromParameterClosureTypeExtension($callLike, $calleeReflection, $parameter, $scopeToPass); @@ -5477,7 +5507,7 @@ private function processArgs( } $this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $context); - $closureResult = $this->processClosureNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context, $parameterType ?? null); + $closureResult = $this->processClosureNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context, $parameterType); if ($callCallbackImmediately) { $throwPoints = array_merge($throwPoints, array_map(static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $closureResult->getThrowPoints())); $impurePoints = array_merge($impurePoints, $closureResult->getImpurePoints()); @@ -5523,6 +5553,7 @@ private function processArgs( } } + // @todo remove once the closure type extensions are removed if ($parameter !== null) { $overwritingParameterType = $this->getParameterTypeFromParameterClosureTypeExtension($callLike, $calleeReflection, $parameter, $scopeToPass); @@ -5532,7 +5563,7 @@ private function processArgs( } $this->callNodeCallbackWithExpression($nodeCallback, $arg->value, $scopeToPass, $context); - $arrowFunctionResult = $this->processArrowFunctionNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $parameterType ?? null); + $arrowFunctionResult = $this->processArrowFunctionNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $parameterType); if ($callCallbackImmediately) { $throwPoints = array_merge($throwPoints, array_map(static fn (InternalThrowPoint $throwPoint) => $throwPoint->isExplicit() ? InternalThrowPoint::createExplicit($scope, $throwPoint->getType(), $arg->value, $throwPoint->canContainAnyThrowable()) : InternalThrowPoint::createImplicit($scope, $arg->value), $arrowFunctionResult->getThrowPoints())); $impurePoints = array_merge($impurePoints, $arrowFunctionResult->getImpurePoints()); @@ -5540,7 +5571,7 @@ private function processArgs( } } else { $exprType = $scope->getType($arg->value); - $exprResult = $this->processExprNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context->enterDeep()); + $exprResult = $this->processExprNode($stmt, $arg->value, $scopeToPass, $nodeCallback, $context->enterDeep(), $parameterType); $throwPoints = array_merge($throwPoints, $exprResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $exprResult->getImpurePoints()); $isAlwaysTerminating = $isAlwaysTerminating || $exprResult->isAlwaysTerminating(); @@ -5663,6 +5694,46 @@ private function processArgs( return new ExpressionResult($scope, $hasYield, $isAlwaysTerminating, $throwPoints, $impurePoints); } + /** + * @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 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 */ @@ -5788,7 +5859,7 @@ private function processAssignVar( $if = $assignedExpr->cond; } $condScope = $this->processExprNode($stmt, $assignedExpr->cond, $scope, static function (): void { - }, ExpressionContext::createDeep())->getScope(); + }, 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); @@ -5860,7 +5931,7 @@ private function processAssignVar( if ($enterExpressionAssign) { $scope = $scope->enterExpressionAssign($var); } - $result = $this->processExprNode($stmt, $var, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $var, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $result->hasYield(); $throwPoints = $result->getThrowPoints(); $impurePoints = $result->getImpurePoints(); @@ -5894,7 +5965,7 @@ private function processAssignVar( if ($enterExpressionAssign) { $scope->enterExpressionAssign($dimExpr); } - $result = $this->processExprNode($stmt, $dimExpr, $scope, $nodeCallback, $context->enterDeep()); + $result = $this->processExprNode($stmt, $dimExpr, $scope, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $result->hasYield(); $throwPoints = array_merge($throwPoints, $result->getThrowPoints()); $scope = $result->getScope(); @@ -6007,10 +6078,11 @@ private function processAssignVar( static function (): void { }, $context, + null, )->getThrowPoints()); } } elseif ($var instanceof PropertyFetch) { - $objectResult = $this->processExprNode($stmt, $var->var, $scope, $nodeCallback, $context); + $objectResult = $this->processExprNode($stmt, $var->var, $scope, $nodeCallback, $context, null); $hasYield = $objectResult->hasYield(); $throwPoints = $objectResult->getThrowPoints(); $impurePoints = $objectResult->getImpurePoints(); @@ -6021,7 +6093,7 @@ static function (): void { if ($var->name instanceof Node\Identifier) { $propertyName = $var->name->name; } else { - $propertyNameResult = $this->processExprNode($stmt, $var->name, $scope, $nodeCallback, $context); + $propertyNameResult = $this->processExprNode($stmt, $var->name, $scope, $nodeCallback, $context, null); $hasYield = $hasYield || $propertyNameResult->hasYield(); $throwPoints = array_merge($throwPoints, $propertyNameResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $propertyNameResult->getImpurePoints()); @@ -6102,6 +6174,7 @@ static function (): void { static function (): void { }, $context, + null, )->getThrowPoints()); } } @@ -6110,7 +6183,7 @@ static function (): void { if ($var->class instanceof Node\Name) { $propertyHolderType = $scope->resolveTypeByName($var->class); } else { - $this->processExprNode($stmt, $var->class, $scope, $nodeCallback, $context); + $this->processExprNode($stmt, $var->class, $scope, $nodeCallback, $context, null); $propertyHolderType = $scope->getType($var->class); } @@ -6118,7 +6191,7 @@ static function (): void { if ($var->name instanceof Node\Identifier) { $propertyName = $var->name->name; } else { - $propertyNameResult = $this->processExprNode($stmt, $var->name, $scope, $nodeCallback, $context); + $propertyNameResult = $this->processExprNode($stmt, $var->name, $scope, $nodeCallback, $context, null); $hasYield = $propertyNameResult->hasYield(); $throwPoints = $propertyNameResult->getThrowPoints(); $impurePoints = $propertyNameResult->getImpurePoints(); @@ -6190,7 +6263,7 @@ static function (): void { $itemScope = $this->lookForSetAllowedUndefinedExpressions($itemScope, $arrayItem->value); $nodeCallback($arrayItem, $itemScope); if ($arrayItem->key !== null) { - $keyResult = $this->processExprNode($stmt, $arrayItem->key, $itemScope, $nodeCallback, $context->enterDeep()); + $keyResult = $this->processExprNode($stmt, $arrayItem->key, $itemScope, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $keyResult->hasYield(); $throwPoints = array_merge($throwPoints, $keyResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $keyResult->getImpurePoints()); @@ -6198,7 +6271,7 @@ static function (): void { $itemScope = $keyResult->getScope(); } - $valueResult = $this->processExprNode($stmt, $arrayItem->value, $itemScope, $nodeCallback, $context->enterDeep()); + $valueResult = $this->processExprNode($stmt, $arrayItem->value, $itemScope, $nodeCallback, $context->enterDeep(), null); $hasYield = $hasYield || $valueResult->hasYield(); $throwPoints = array_merge($throwPoints, $valueResult->getThrowPoints()); $impurePoints = array_merge($impurePoints, $valueResult->getImpurePoints()); diff --git a/src/DependencyInjection/Type/DynamicParameterTypeExtensionProvider.php b/src/DependencyInjection/Type/DynamicParameterTypeExtensionProvider.php new file mode 100644 index 0000000000..c6bc6fd2a2 --- /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/DependencyInjection/Type/LazyParameterClosureTypeExtensionProvider.php b/src/DependencyInjection/Type/LazyParameterClosureTypeExtensionProvider.php index ecc30869f5..6941ee78de 100644 --- a/src/DependencyInjection/Type/LazyParameterClosureTypeExtensionProvider.php +++ b/src/DependencyInjection/Type/LazyParameterClosureTypeExtensionProvider.php @@ -5,6 +5,10 @@ use PHPStan\DependencyInjection\AutowiredService; use PHPStan\DependencyInjection\Container; +/** + * @deprecated + * @see \PHPStan\DependencyInjection\Type\LazyDynamicParameterTypeExtensionProvider + */ #[AutowiredService(as: ParameterClosureTypeExtensionProvider::class)] final class LazyParameterClosureTypeExtensionProvider implements ParameterClosureTypeExtensionProvider { diff --git a/src/DependencyInjection/Type/ParameterClosureTypeExtensionProvider.php b/src/DependencyInjection/Type/ParameterClosureTypeExtensionProvider.php index 817bd6acf1..93ac91971c 100644 --- a/src/DependencyInjection/Type/ParameterClosureTypeExtensionProvider.php +++ b/src/DependencyInjection/Type/ParameterClosureTypeExtensionProvider.php @@ -6,6 +6,10 @@ use PHPStan\Type\MethodParameterClosureTypeExtension; use PHPStan\Type\StaticMethodParameterClosureTypeExtension; +/** + * @deprecated + * @see \PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider + */ interface ParameterClosureTypeExtensionProvider { diff --git a/src/DependencyInjection/ValidateServiceTagsExtension.php b/src/DependencyInjection/ValidateServiceTagsExtension.php index 34f5edd110..3a47b8b49a 100644 --- a/src/DependencyInjection/ValidateServiceTagsExtension.php +++ b/src/DependencyInjection/ValidateServiceTagsExtension.php @@ -13,6 +13,7 @@ use PHPStan\Broker\BrokerFactory; use PHPStan\Collectors\Collector; use PHPStan\Collectors\RegistryFactory as CollectorRegistryFactory; +use PHPStan\DependencyInjection\Type\LazyDynamicParameterTypeExtensionProvider; use PHPStan\DependencyInjection\Type\LazyDynamicThrowTypeExtensionProvider; use PHPStan\DependencyInjection\Type\LazyParameterClosureThisExtensionProvider; use PHPStan\DependencyInjection\Type\LazyParameterClosureTypeExtensionProvider; @@ -44,10 +45,13 @@ use PHPStan\Rules\RestrictedUsage\RestrictedPropertyUsageExtension; use PHPStan\Rules\Rule; use PHPStan\ShouldNotHappenException; +use PHPStan\Type\DynamicFunctionParameterTypeExtension; use PHPStan\Type\DynamicFunctionReturnTypeExtension; use PHPStan\Type\DynamicFunctionThrowTypeExtension; +use PHPStan\Type\DynamicMethodParameterTypeExtension; use PHPStan\Type\DynamicMethodReturnTypeExtension; use PHPStan\Type\DynamicMethodThrowTypeExtension; +use PHPStan\Type\DynamicStaticMethodParameterTypeExtension; use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; use PHPStan\Type\DynamicStaticMethodThrowTypeExtension; use PHPStan\Type\ExpressionTypeResolverExtension; @@ -97,6 +101,9 @@ final class ValidateServiceTagsExtension extends CompilerExtension FunctionParameterClosureThisExtension::class => LazyParameterClosureThisExtensionProvider::FUNCTION_TAG, MethodParameterClosureThisExtension::class => LazyParameterClosureThisExtensionProvider::METHOD_TAG, StaticMethodParameterClosureThisExtension::class => LazyParameterClosureThisExtensionProvider::STATIC_METHOD_TAG, + DynamicFunctionParameterTypeExtension::class => LazyDynamicParameterTypeExtensionProvider::FUNCTION_TAG, + DynamicMethodParameterTypeExtension::class => LazyDynamicParameterTypeExtensionProvider::METHOD_TAG, + DynamicStaticMethodParameterTypeExtension::class => LazyDynamicParameterTypeExtensionProvider::STATIC_METHOD_TAG, FunctionParameterClosureTypeExtension::class => LazyParameterClosureTypeExtensionProvider::FUNCTION_TAG, MethodParameterClosureTypeExtension::class => LazyParameterClosureTypeExtensionProvider::METHOD_TAG, StaticMethodParameterClosureTypeExtension::class => LazyParameterClosureTypeExtensionProvider::STATIC_METHOD_TAG, diff --git a/src/Node/ClosureReturnStatementsNode.php b/src/Node/ClosureReturnStatementsNode.php index 87b63a60a4..8af87b65a7 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 6876978cec..c92fb487d5 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/AttributesCheck.php b/src/Rules/AttributesCheck.php index 36b3c6faa6..430ca73ee7 100644 --- a/src/Rules/AttributesCheck.php +++ b/src/Rules/AttributesCheck.php @@ -139,6 +139,7 @@ public function check( ), $scope, $attributeConstructor->getDeclaringClass()->isBuiltin(), + null, new New_($attribute->name, $attribute->args, $nodeAttributes), 'attribute', $attributeConstructor->acceptsNamedArguments(), diff --git a/src/Rules/Classes/InstantiationRule.php b/src/Rules/Classes/InstantiationRule.php index 6f8b322975..110f93c016 100644 --- a/src/Rules/Classes/InstantiationRule.php +++ b/src/Rules/Classes/InstantiationRule.php @@ -240,6 +240,7 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $ ), $scope, $constructorReflection->getDeclaringClass()->isBuiltin(), + null, $node, 'new', $constructorReflection->acceptsNamedArguments(), diff --git a/src/Rules/FunctionCallParametersCheck.php b/src/Rules/FunctionCallParametersCheck.php index 98b67ad4bc..6768cd8bf5 100644 --- a/src/Rules/FunctionCallParametersCheck.php +++ b/src/Rules/FunctionCallParametersCheck.php @@ -9,7 +9,10 @@ use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\AutowiredService; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Reflection\ExtendedParameterReflection; +use PHPStan\Reflection\FunctionReflection; +use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\ParameterReflection; use PHPStan\Reflection\ParametersAcceptor; use PHPStan\Reflection\ResolvedFunctionVariant; @@ -47,6 +50,7 @@ public function __construct( private NullsafeCheck $nullsafeCheck, private UnresolvableTypeHelper $unresolvableTypeHelper, private PropertyReflectionFinder $propertyReflectionFinder, + private DynamicParameterTypeExtensionProvider $dynamicParameterTypeExtensionProvider, #[AutowiredParameter(ref: '%checkFunctionArgumentTypes%')] private bool $checkArgumentTypes, #[AutowiredParameter] @@ -67,6 +71,7 @@ public function check( ParametersAcceptor $parametersAcceptor, Scope $scope, bool $isBuiltin, + MethodReflection|FunctionReflection|null $calleeReflection, Node\Expr\FuncCall|Node\Expr\MethodCall|Node\Expr\StaticCall|Node\Expr\New_ $funcCall, string $nodeType, TrinaryLogic $acceptsNamedArguments, @@ -350,6 +355,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) @@ -689,4 +701,50 @@ private function describeParameter(ParameterReflection $parameter, int|string|nu return implode(' ', $parts); } + 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 ab1fecfe73..70f2786cd9 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/CallCallablesRule.php b/src/Rules/Functions/CallCallablesRule.php index 4ab0af4b01..c7104fd2dd 100644 --- a/src/Rules/Functions/CallCallablesRule.php +++ b/src/Rules/Functions/CallCallablesRule.php @@ -121,6 +121,7 @@ public function processNode( $parametersAcceptor, $scope, false, + null, $node, 'callable', $acceptsNamedArguments, diff --git a/src/Rules/Functions/CallToFunctionParametersRule.php b/src/Rules/Functions/CallToFunctionParametersRule.php index 39f6f7cfea..3f8e95595c 100644 --- a/src/Rules/Functions/CallToFunctionParametersRule.php +++ b/src/Rules/Functions/CallToFunctionParametersRule.php @@ -50,6 +50,7 @@ public function processNode(Node $node, Scope $scope): array ), $scope, $function->isBuiltin(), + $function, $node, 'function', $function->acceptsNamedArguments(), diff --git a/src/Rules/Functions/CallUserFuncRule.php b/src/Rules/Functions/CallUserFuncRule.php index 3dae092b52..8a73e4a41c 100644 --- a/src/Rules/Functions/CallUserFuncRule.php +++ b/src/Rules/Functions/CallUserFuncRule.php @@ -66,6 +66,7 @@ public function processNode(Node $node, Scope $scope): array $parametersAcceptor, $scope, false, + null, $funcCall, 'function', $acceptsNamedArguments, diff --git a/src/Rules/Functions/ClosureReturnTypeRule.php b/src/Rules/Functions/ClosureReturnTypeRule.php index abeb7c3866..abfe196e39 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 1f042288f0..719c4b93e9 100644 --- a/src/Rules/Methods/CallMethodsRule.php +++ b/src/Rules/Methods/CallMethodsRule.php @@ -81,6 +81,7 @@ private function processSingleMethodCall(Scope $scope, MethodCall $node, string ), $scope, $declaringClass->isBuiltin(), + $methodReflection, $node, 'method', $methodReflection->acceptsNamedArguments(), diff --git a/src/Rules/Methods/CallStaticMethodsRule.php b/src/Rules/Methods/CallStaticMethodsRule.php index 55537f84f0..382419a692 100644 --- a/src/Rules/Methods/CallStaticMethodsRule.php +++ b/src/Rules/Methods/CallStaticMethodsRule.php @@ -90,6 +90,7 @@ private function processSingleMethodCall(Scope $scope, StaticCall $node, string ), $scope, $method->getDeclaringClass()->isBuiltin(), + $method, $node, 'staticMethod', $method->acceptsNamedArguments(), diff --git a/src/Testing/RuleTestCase.php b/src/Testing/RuleTestCase.php index a00040c931..b46dd2b3a6 100644 --- a/src/Testing/RuleTestCase.php +++ b/src/Testing/RuleTestCase.php @@ -19,6 +19,7 @@ use PHPStan\Collectors\Collector; use PHPStan\Collectors\Registry as CollectorRegistry; use PHPStan\Dependency\DependencyResolver; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider; use PHPStan\DependencyInjection\Type\ParameterClosureThisExtensionProvider; use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider; @@ -112,6 +113,7 @@ protected function createNodeScopeResolver(): NodeScopeResolver|GeneratorNodeSco self::getContainer()->getByType(DynamicThrowTypeExtensionProvider::class), $readWritePropertiesExtensions !== [] ? new DirectReadWritePropertiesExtensionProvider($readWritePropertiesExtensions) : self::getContainer()->getByType(ReadWritePropertiesExtensionProvider::class), self::getContainer()->getByType(ParameterClosureThisExtensionProvider::class), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), self::getContainer()->getByType(ParameterClosureTypeExtensionProvider::class), self::createScopeFactory($reflectionProvider, $typeSpecifier), $this->shouldPolluteScopeWithLoopInitialAssignments(), diff --git a/src/Testing/TypeInferenceTestCase.php b/src/Testing/TypeInferenceTestCase.php index f3c998d5dc..301ef583bf 100644 --- a/src/Testing/TypeInferenceTestCase.php +++ b/src/Testing/TypeInferenceTestCase.php @@ -12,6 +12,7 @@ use PHPStan\Analyser\NodeScopeResolver; use PHPStan\Analyser\Scope; use PHPStan\Analyser\ScopeContext; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider; use PHPStan\DependencyInjection\Type\ParameterClosureThisExtensionProvider; use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider; @@ -86,6 +87,7 @@ protected static function createNodeScopeResolver(): NodeScopeResolver|Generator $container->getByType(DynamicThrowTypeExtensionProvider::class), $container->getByType(ReadWritePropertiesExtensionProvider::class), $container->getByType(ParameterClosureThisExtensionProvider::class), + $container->getByType(DynamicParameterTypeExtensionProvider::class), $container->getByType(ParameterClosureTypeExtensionProvider::class), self::createScopeFactory($reflectionProvider, $typeSpecifier), $container->getParameter('polluteScopeWithLoopInitialAssignments'), diff --git a/src/Type/DynamicFunctionParameterTypeExtension.php b/src/Type/DynamicFunctionParameterTypeExtension.php new file mode 100644 index 0000000000..63d4c0389c --- /dev/null +++ b/src/Type/DynamicFunctionParameterTypeExtension.php @@ -0,0 +1,32 @@ +getByType(DynamicThrowTypeExtensionProvider::class), $container->getByType(ReadWritePropertiesExtensionProvider::class), $container->getByType(ParameterClosureThisExtensionProvider::class), + $container->getByType(DynamicParameterTypeExtensionProvider::class), $container->getByType(ParameterClosureTypeExtensionProvider::class), self::createScopeFactory($reflectionProvider, $typeSpecifier), false, diff --git a/tests/PHPStan/Analyser/Bug9307CallMethodsRuleTest.php b/tests/PHPStan/Analyser/Bug9307CallMethodsRuleTest.php index a7342e7506..d5bb33d170 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; @@ -24,7 +25,7 @@ protected function getRule(): Rule $ruleLevelHelper = new RuleLevelHelper($reflectionProvider, true, false, true, true, false, false, true); return new CallMethodsRule( new MethodCallCheck($reflectionProvider, $ruleLevelHelper, true, true), - new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true), + new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, true), ); } diff --git a/tests/PHPStan/Analyser/DynamicParameterTypeExtensionArraysTest.php b/tests/PHPStan/Analyser/DynamicParameterTypeExtensionArraysTest.php new file mode 100644 index 0000000000..00a1e28ec5 --- /dev/null +++ b/tests/PHPStan/Analyser/DynamicParameterTypeExtensionArraysTest.php @@ -0,0 +1,28 @@ +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 0000000000..e8c8a52829 --- /dev/null +++ b/tests/PHPStan/Analyser/DynamicParameterTypeExtensionClosuresTest.php @@ -0,0 +1,28 @@ +assertFileAsserts($assertType, $file, ...$args); + } + + public static function getAdditionalConfigFiles(): array + { + return [__DIR__ . '/dynamic-parameter-type-extension-closures.neon']; + } + +} diff --git a/tests/PHPStan/Analyser/ExpressionResultTest.php b/tests/PHPStan/Analyser/ExpressionResultTest.php index 56b699f1fc..784f920416 100644 --- a/tests/PHPStan/Analyser/ExpressionResultTest.php +++ b/tests/PHPStan/Analyser/ExpressionResultTest.php @@ -211,6 +211,7 @@ public function testIsAlwaysTerminating( static function (): void { }, ExpressionContext::createTopLevel(), + null, ); $this->assertSame($expectedIsAlwaysTerminating, $result->isAlwaysTerminating()); } 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 0000000000..8898adb444 --- /dev/null +++ b/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-arrays.php @@ -0,0 +1,181 @@ +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.php b/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-closures.php new file mode 100644 index 0000000000..eb407c5db6 --- /dev/null +++ b/tests/PHPStan/Analyser/data/dynamic-parameter-type-extension-closures.php @@ -0,0 +1,157 @@ +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 + { + return $methodReflection->getDeclaringClass()->getName() === Foo::class && $methodReflection->getName() === 'staticMethodWithCallable'; + } + + 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 + { + 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; + } +} + +/** + * @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())); + + + // 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()); }); +} 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 0000000000..82f6e9cb59 --- /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.neon b/tests/PHPStan/Analyser/dynamic-parameter-type-extension-closures.neon new file mode 100644 index 0000000000..13c6ef79d7 --- /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 978eb83e71..d753fa376b 100644 --- a/tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Classes/ClassAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Classes; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -37,6 +38,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php b/tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php index 8e594f53eb..19a02e7cdd 100644 --- a/tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Classes/ClassConstantAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Classes; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -32,6 +33,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php b/tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php index 64eedaa7cb..e7b2672210 100644 --- a/tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php +++ b/tests/PHPStan/Rules/Classes/ForbiddenNameCheckExtensionRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Classes; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; use PHPStan\Rules\ClassNameCheck; @@ -27,7 +28,7 @@ protected function getRule(): Rule return new InstantiationRule( $container, $reflectionProvider, - new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true), + new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, true), new ClassNameCheck( new ClassCaseSensitivityCheck($reflectionProvider, true), new ClassForbiddenNameCheck($container), diff --git a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php index 26a872d9e8..fcee0d8f57 100644 --- a/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php +++ b/tests/PHPStan/Rules/Classes/InstantiationRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Classes; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; use PHPStan\Rules\ClassNameCheck; @@ -27,7 +28,7 @@ protected function getRule(): Rule return new InstantiationRule( $container, $reflectionProvider, - new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true), + new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, false, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, true), new ClassNameCheck( new ClassCaseSensitivityCheck($reflectionProvider, true), new ClassForbiddenNameCheck($container), diff --git a/tests/PHPStan/Rules/Constants/ConstantAttributesRuleTest.php b/tests/PHPStan/Rules/Constants/ConstantAttributesRuleTest.php index 698d60678c..c0bf9e7024 100644 --- a/tests/PHPStan/Rules/Constants/ConstantAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Constants/ConstantAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Constants; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Php\PhpVersion; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; @@ -38,6 +39,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php b/tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php index 1b43de86af..17b5fcfb2a 100644 --- a/tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php +++ b/tests/PHPStan/Rules/EnumCases/EnumCaseAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\EnumCases; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -33,6 +34,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Functions/ArrowFunctionAttributesRuleTest.php b/tests/PHPStan/Rules/Functions/ArrowFunctionAttributesRuleTest.php index 7f26ab879e..488559634a 100644 --- a/tests/PHPStan/Rules/Functions/ArrowFunctionAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ArrowFunctionAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Functions; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -32,6 +33,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php b/tests/PHPStan/Rules/Functions/CallCallablesRuleTest.php index 6c71a1a7c0..eb3275bfca 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; @@ -30,6 +31,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 5f4bb1bf7c..fbf47428e8 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; @@ -29,7 +30,7 @@ protected function getRule(): Rule $broker = self::createReflectionProvider(); return new CallToFunctionParametersRule( $broker, - new FunctionCallParametersCheck(new RuleLevelHelper($broker, true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true), + new FunctionCallParametersCheck(new RuleLevelHelper($broker, true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, true), ); } diff --git a/tests/PHPStan/Rules/Functions/CallUserFuncRuleTest.php b/tests/PHPStan/Rules/Functions/CallUserFuncRuleTest.php index ba44a5bc0f..2089961bec 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; @@ -20,7 +21,7 @@ class CallUserFuncRuleTest extends RuleTestCase protected function getRule(): Rule { $reflectionProvider = self::createReflectionProvider(); - return new CallUserFuncRule($reflectionProvider, new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, true, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true)); + return new CallUserFuncRule($reflectionProvider, new FunctionCallParametersCheck(new RuleLevelHelper($reflectionProvider, true, false, true, true, false, false, true), new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, true)); } #[RequiresPhp('>= 8.0')] diff --git a/tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php b/tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php index ecc2531fe4..412e3a2762 100644 --- a/tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ClosureAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Functions; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -32,6 +33,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php b/tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php index 3b7253311e..5ac30734dd 100644 --- a/tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/FunctionAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Functions; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -32,6 +33,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php b/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php index d6e6c46744..29bf34915c 100644 --- a/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Functions/ParamAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Functions; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -32,6 +33,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php index 0dbebf0da3..1ffa883cab 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; @@ -35,7 +36,7 @@ protected function getRule(): Rule $ruleLevelHelper = new RuleLevelHelper($reflectionProvider, $this->checkNullables, $this->checkThisOnly, $this->checkUnionTypes, $this->checkExplicitMixed, $this->checkImplicitMixed, false, true); return new CallMethodsRule( new MethodCallCheck($reflectionProvider, $ruleLevelHelper, true, true), - new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), true, true, true, true), + new FunctionCallParametersCheck($ruleLevelHelper, new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, true), ); } diff --git a/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php b/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php index 558607e8f5..577aa6230d 100644 --- a/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php +++ b/tests/PHPStan/Rules/Methods/CallStaticMethodsRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Methods; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; use PHPStan\Rules\ClassNameCheck; @@ -53,6 +54,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php b/tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php index 00ecb75183..5c6c12d083 100644 --- a/tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Methods/MethodAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Methods; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -32,6 +33,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php b/tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php index a09e4e8f81..82c24c3277 100644 --- a/tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/PropertyAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Properties; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Php\PhpVersion; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; @@ -34,6 +35,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Properties/PropertyHookAttributesRuleTest.php b/tests/PHPStan/Rules/Properties/PropertyHookAttributesRuleTest.php index f1db640b3f..d123386f09 100644 --- a/tests/PHPStan/Rules/Properties/PropertyHookAttributesRuleTest.php +++ b/tests/PHPStan/Rules/Properties/PropertyHookAttributesRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Properties; +use PHPStan\DependencyInjection\Type\DynamicParameterTypeExtensionProvider; use PHPStan\Rules\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; use PHPStan\Rules\ClassForbiddenNameCheck; @@ -32,6 +33,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true, diff --git a/tests/PHPStan/Rules/Traits/TraitAttributesRuleTest.php b/tests/PHPStan/Rules/Traits/TraitAttributesRuleTest.php index 1a0ff65182..ea442d9f31 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\AttributesCheck; use PHPStan\Rules\ClassCaseSensitivityCheck; @@ -39,6 +40,7 @@ protected function getRule(): Rule new NullsafeCheck(), new UnresolvableTypeHelper(), new PropertyReflectionFinder(), + self::getContainer()->getByType(DynamicParameterTypeExtensionProvider::class), true, true, true,