Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions e2e/result-cache-function-import-type/alias.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
--- src/Aliases.php
+++ src/Aliases.php
@@ -3,7 +3,7 @@
namespace ResultCacheE2EFunctionImportType;

/**
- * @phpstan-type Id int
+ * @phpstan-type Id string
*/
final class Aliases
{
5 changes: 5 additions & 0 deletions e2e/result-cache-function-import-type/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
tmpDir: tmp
paths:
- src
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash
# @tags result-cache

# A type alias imported above a function or a method is only in its PHPDoc, so nothing else ties
# the files below to Aliases.php. Changing the alias there has to re-analyse:
# - body.php, whose function uses the alias in its body,
# - useFunction.php, which calls a function returning the alias,
# - useMethod.php, which calls a method returning the alias.
# Without the dependencies, their stale results come from the cache.

function set_up() {
e2e::set_up
}

function tear_down() {
e2e::tear_down
}

function test_result_cache_function_import_type() {
../../bin/phpstan analyse
patch -b -p0 < alias.patch
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>/dev/null | sed "s|$(pwd)/||" | sort || true)
echo "$OUTPUT"
assert_contains 'src/body.php:11:Function ResultCacheE2EFunctionImportType\idInBody() should return int but returns string.' "$OUTPUT"
assert_contains 'src/useFunction.php:7:Function ResultCacheE2EFunctionImportType\useFunction() should return int but returns string.' "$OUTPUT"
assert_contains 'src/useMethod.php:7:Function ResultCacheE2EFunctionImportType\useMethod() should return int but returns string.' "$OUTPUT"

../../bin/phpstan clear-result-cache
UNCACHED=$(../../bin/phpstan analyse --error-format raw 2>/dev/null | sed "s|$(pwd)/||" | sort || true)
assert_same "$UNCACHED" "$OUTPUT"
}
11 changes: 11 additions & 0 deletions e2e/result-cache-function-import-type/src/Aliases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFunctionImportType;

/**
* @phpstan-type Id int
*/
final class Aliases
{

}
18 changes: 18 additions & 0 deletions e2e/result-cache-function-import-type/src/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFunctionImportType;

final class Repository
{

/**
* @phpstan-import-type Id from Aliases
* @return Id
*/
public function getId(mixed $value)
{
/** @var Id $value */
return $value;
}

}
12 changes: 12 additions & 0 deletions e2e/result-cache-function-import-type/src/body.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFunctionImportType;

/**
* @phpstan-import-type Id from Aliases
*/
function idInBody(mixed $value): int
{
/** @var Id $value */
return $value;
}
13 changes: 13 additions & 0 deletions e2e/result-cache-function-import-type/src/getId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFunctionImportType;

/**
* @phpstan-import-type Id from Aliases
* @return Id
*/
function getId(mixed $value)
{
/** @var Id $value */
return $value;
}
8 changes: 8 additions & 0 deletions e2e/result-cache-function-import-type/src/useFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFunctionImportType;

function useFunction(): int
{
return getId(1);
}
8 changes: 8 additions & 0 deletions e2e/result-cache-function-import-type/src/useMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFunctionImportType;

function useMethod(Repository $repository): int
{
return $repository->getId(1);
}
2 changes: 2 additions & 0 deletions e2e/result-cache-function-import-type/tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.*
26 changes: 26 additions & 0 deletions src/Analyser/IntermediaryNameScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Analyser;

use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasImportTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TypeAliasTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;

final class IntermediaryNameScope
Expand All @@ -13,6 +15,8 @@ final class IntermediaryNameScope
* @param array<string, true> $typeAliasesMap
* @param array{string, string, string, string|null, string|null}|null $traitData
* @param array<string, TypeNode> $localTypeAliasNodes the type aliases declared next to the @template tags, inlined into their bounds
* @param array<string, TypeAliasTagValueNode> $functionTypeAliasTagValueNodes the type aliases declared above a function or a method
* @param array<string, TypeAliasImportTagValueNode> $functionTypeAliasImportTagValueNodes the type aliases imported above a function or a method, by their local names
*/
public function __construct(
private NamespaceUses $namespaceUses,
Expand All @@ -25,6 +29,8 @@ public function __construct(
private ?string $typeAliasClassName = null,
private ?array $traitData = null,
private array $localTypeAliasNodes = [],
private array $functionTypeAliasTagValueNodes = [],
private array $functionTypeAliasImportTagValueNodes = [],
)
{
}
Expand Down Expand Up @@ -84,6 +90,8 @@ public function withTraitData(string $fileName, string $className, string $trait
$this->typeAliasClassName,
[$fileName, $className, $traitName, $lookForTraitName, $docComment],
$this->localTypeAliasNodes,
$this->functionTypeAliasTagValueNodes,
$this->functionTypeAliasImportTagValueNodes,
);
}

Expand All @@ -107,6 +115,8 @@ public function unsetTemplatePhpDocNodes(array $namesToUnset): self
$this->typeAliasClassName,
$this->traitData,
$this->localTypeAliasNodes,
$this->functionTypeAliasTagValueNodes,
$this->functionTypeAliasImportTagValueNodes,
);
}

Expand Down Expand Up @@ -139,6 +149,22 @@ public function getLocalTypeAliasNodes(): array
return $this->localTypeAliasNodes;
}

/**
* @return array<string, TypeAliasTagValueNode>
*/
public function getFunctionTypeAliasTagValueNodes(): array
{
return $this->functionTypeAliasTagValueNodes;
}

/**
* @return array<string, TypeAliasImportTagValueNode>
*/
public function getFunctionTypeAliasImportTagValueNodes(): array
{
return $this->functionTypeAliasImportTagValueNodes;
}

public function shouldBypassTypeAliases(): bool
{
return $this->bypassTypeAliases;
Expand Down
43 changes: 41 additions & 2 deletions src/Analyser/NameScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace PHPStan\Analyser;

use PHPStan\PhpDoc\Tag\TemplateTag;
use PHPStan\PhpDoc\Tag\TypeAliasImportTag;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeScope;
use PHPStan\Type\Type;
use PHPStan\Type\TypeAlias;
use function array_key_exists;
use function array_merge;
use function array_shift;
Expand All @@ -32,6 +34,7 @@ final class NameScope
* @param array<string, string> $constUses alias(string) => fullName(string)
* @param array<string, TemplateTag> $templateTags
* @param array<string, true> $typeAliasesMap
* @param array<string, array{TypeAlias|TypeAliasImportTag, string}> $functionTypeAliases aliases declared or imported above the enclosing functions and methods, with the key they are cached under
*/
public function __construct(
private ?string $namespace,
Expand All @@ -45,6 +48,7 @@ public function __construct(
private array $constUses = [],
private ?string $typeAliasClassName = null,
private ?string $fileName = null,
private array $functionTypeAliases = [],
)
{
$this->templateTypeMap = $templateTypeMap ?? TemplateTypeMap::createEmpty();
Expand Down Expand Up @@ -211,6 +215,7 @@ public function withTemplateTypeMap(TemplateTypeMap $map, array $templateTags):
$this->bypassTypeAliases,
$this->constUses,
fileName: $this->fileName,
functionTypeAliases: $this->functionTypeAliases,
);
}

Expand All @@ -227,6 +232,7 @@ public function withoutNamespaceAndUses(): self
$this->bypassTypeAliases,
$this->constUses,
fileName: $this->fileName,
functionTypeAliases: $this->functionTypeAliases,
);
}

Expand All @@ -243,6 +249,7 @@ public function withClassName(string $className): self
$this->bypassTypeAliases,
$this->constUses,
fileName: $this->fileName,
functionTypeAliases: $this->functionTypeAliases,
);
}

Expand All @@ -264,12 +271,13 @@ public function unsetTemplateType(string $name): self
$this->bypassTypeAliases,
$this->constUses,
fileName: $this->fileName,
functionTypeAliases: $this->functionTypeAliases,
);
}

public function bypassTypeAliases(): self
{
return new self($this->namespace, $this->uses, $this->className, $this->functionName, $this->templateTypeMap, $this->templateTags, $this->typeAliasesMap, true, $this->constUses, fileName: $this->fileName);
return new self($this->namespace, $this->uses, $this->className, $this->functionName, $this->templateTypeMap, $this->templateTags, $this->typeAliasesMap, true, $this->constUses, fileName: $this->fileName, functionTypeAliases: $this->functionTypeAliases);
}

public function shouldBypassTypeAliases(): bool
Expand All @@ -279,7 +287,38 @@ public function shouldBypassTypeAliases(): bool

public function hasTypeAlias(string $alias): bool
{
return array_key_exists($alias, $this->typeAliasesMap);
return array_key_exists($alias, $this->typeAliasesMap) || array_key_exists($alias, $this->functionTypeAliases);
}

/**
* An alias declared or imported above an enclosing function or method, with the key it is cached under.
* It takes precedence over an alias of the class.
*
* @return array{TypeAlias|TypeAliasImportTag, string}|null
*/
public function getFunctionTypeAlias(string $alias): ?array
{
return $this->functionTypeAliases[$alias] ?? null;
}

/**
* @return array<string, array{TypeAlias|TypeAliasImportTag, string}>
*/
public function getFunctionTypeAliases(): array
{
return $this->functionTypeAliases;
}

/**
* @param callable(self): array<string, array{TypeAlias|TypeAliasImportTag, string}> $createFunctionTypeAliases gets the new name scope,
* which the aliases declared in it resolve in
*/
public function withFunctionTypeAliases(callable $createFunctionTypeAliases): self
{
$self = clone $this;
$self->functionTypeAliases = $createFunctionTypeAliases($self);

return $self;
}

}
72 changes: 72 additions & 0 deletions src/Rules/Classes/FunctionTypeAliasesRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Classes;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\DependencyInjection\ValidatesStubFiles;
use PHPStan\Rules\Rule;
use PHPStan\Type\FileTypeMapper;
use function sprintf;

/**
* @implements Rule<Node\FunctionLike>
*/
#[RegisteredRule(level: 0)]
#[ValidatesStubFiles]
final class FunctionTypeAliasesRule implements Rule
{

public function __construct(
private FileTypeMapper $fileTypeMapper,
private LocalTypeAliasesCheck $check,
)
{
}

public function getNodeType(): string
{
return Node\FunctionLike::class;
}

public function processNode(Node $node, Scope $scope): array
{
if ($node instanceof Node\Stmt\Function_) {
if (!isset($node->namespacedName)) {
return [];
}
$className = null;
$traitName = null;
$functionName = (string) $node->namespacedName;
$description = sprintf('function %s()', $functionName);
} elseif ($node instanceof Node\Stmt\ClassMethod) {
if (!$scope->isInClass()) {
return [];
}
$classReflection = $scope->getClassReflection();
$className = $classReflection->getName();
$traitName = $scope->isInTrait() ? $scope->getTraitReflection()->getName() : null;
$functionName = $node->name->toString();
$description = sprintf('method %s::%s()', $classReflection->getDisplayName(), $functionName);
} else {
return [];
}

$docComment = $node->getDocComment();
if ($docComment === null) {
return [];
}

$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$scope->getFile(),
$className,
$traitName,
$functionName,
$docComment->getText(),
);

return $this->check->checkFunction($scope, $resolvedPhpDoc, $description, $node);
}

}
Loading
Loading