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
193 changes: 193 additions & 0 deletions .github/workflows/e2e-tests.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion compiler/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"require": {
"php": "^8.2",
"nette/neon": "^3.0.0",
"ondrejmirtes/simple-downgrader": "^2.2.7",
"ondrejmirtes/simple-downgrader": "^2.2.9",
"seld/phar-utils": "^1.2",
"symfony/console": "^5.4.43",
"symfony/filesystem": "^5.4.43",
Expand Down
14 changes: 7 additions & 7 deletions compiler/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/result-cache-file-dependency/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/vendor
5 changes: 5 additions & 0 deletions e2e/result-cache-file-dependency/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"autoload-dev": {
"classmap": ["extension/"]
}
}
18 changes: 18 additions & 0 deletions e2e/result-cache-file-dependency/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/result-cache-file-dependency/data/deprecated.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
report
1 change: 1 addition & 0 deletions e2e/result-cache-file-dependency/data/function.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function-1
1 change: 1 addition & 0 deletions e2e/result-cache-file-dependency/data/holder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text
1 change: 1 addition & 0 deletions e2e/result-cache-file-dependency/data/method.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
method-1
1 change: 1 addition & 0 deletions e2e/result-cache-file-dependency/data/static-method.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
static-method-1
53 changes: 53 additions & 0 deletions e2e/result-cache-file-dependency/extension/ConfigRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFileDependency;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\DependencyEmitter;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* Reports calls to checkConfig() when src/Config.php asks for it. The file is read whether an
* error is reported or not, so the dependency is declared either way.
*
* @implements Rule<FuncCall>
*/
final class ConfigRule implements Rule
{

public function getNodeType(): string
{
return FuncCall::class;
}

/**
* @param Scope&DependencyEmitter $scope
*/
public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Name || $node->name->getLast() !== 'checkConfig') {
return [];
}

$configFile = dirname(__DIR__) . '/src/Config.php';
$scope->fileDependency($configFile);

if (!is_file($configFile)) {
return [];
}

$contents = file_get_contents($configFile);
if ($contents === false || !str_contains($contents, 'report')) {
return [];
}

return [
RuleErrorBuilder::message('Config asks for an error.')->identifier('resultCacheE2E.config')->build(),
];
}

}
38 changes: 38 additions & 0 deletions e2e/result-cache-file-dependency/extension/DataFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFileDependency;

use PHPStan\Analyser\DependencyEmitter;
use PHPStan\Analyser\Scope;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\Type;

final class DataFile
{

/**
* The return type comes from a data file the extension reads on its own: int when it says so,
* otherwise the string it contains, 'missing' when there is no such file.
*
* @param Scope&DependencyEmitter $scope
*/
public static function type(string $name, Scope $scope): Type
{
$file = dirname(__DIR__) . '/data/' . $name . '.txt';
$scope->fileDependency($file);

if (!is_file($file)) {
return new ConstantStringType('missing');
}

$contents = file_get_contents($file);
$contents = $contents === false ? '' : trim($contents);
if ($contents === 'int') {
return new IntegerType();
}

return new ConstantStringType($contents);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFileDependency;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\DependencyEmitter;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Type;

final class DataFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), ['ResultCacheE2EFileDependency\functionData', 'ResultCacheE2EFileDependency\holderData'], true);
}

/**
* @param Scope&DependencyEmitter $scope
*/
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
return DataFile::type($functionReflection->getName() === 'ResultCacheE2EFileDependency\functionData' ? 'function' : 'holder', $scope);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFileDependency;

use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\DependencyEmitter;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\Type;

final class DataMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
{

public function getClass(): string
{
return Repository::class;
}

public function isMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'methodData';
}

/**
* @param Scope&DependencyEmitter $scope
*/
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): Type
{
return DataFile::type('method', $scope);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFileDependency;

use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\DependencyEmitter;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension;
use PHPStan\Type\Type;

final class DataStaticMethodReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension
{

public function getClass(): string
{
return Repository::class;
}

public function isStaticMethodSupported(MethodReflection $methodReflection): bool
{
return $methodReflection->getName() === 'staticMethodData';
}

/**
* @param Scope&DependencyEmitter $scope
*/
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): Type
{
return DataFile::type('static-method', $scope);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace ResultCacheE2EFileDependency;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* Like ConfigRule, with the deprecated RuleErrorBuilder::fileDependency(), which can declare the
* dependency only along with an error.
*
* @implements Rule<FuncCall>
*/
final class DeprecatedConfigRule implements Rule
{

public function getNodeType(): string
{
return FuncCall::class;
}

public function processNode(Node $node, Scope $scope): array
{
if (!$node->name instanceof Name || $node->name->getLast() !== 'checkDeprecatedConfig') {
return [];
}

$configFile = dirname(__DIR__) . '/data/deprecated.txt';
$contents = @file_get_contents($configFile);
if ($contents === false || !str_contains($contents, 'report')) {
return [];
}

return [
// @phpstan-ignore method.deprecated
RuleErrorBuilder::message('Deprecated config asks for an error.')->identifier('resultCacheE2E.deprecatedConfig')->fileDependency($configFile)->build(),
];
}

}
28 changes: 28 additions & 0 deletions e2e/result-cache-file-dependency/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
parameters:
level: 8
tmpDir: tmp
paths:
- src
inferPrivatePropertyTypeFromConstructor: true

services:
-
class: ResultCacheE2EFileDependency\ConfigRule
tags:
- phpstan.rules.rule
-
class: ResultCacheE2EFileDependency\DeprecatedConfigRule
tags:
- phpstan.rules.rule
-
class: ResultCacheE2EFileDependency\DataFunctionReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
-
class: ResultCacheE2EFileDependency\DataMethodReturnTypeExtension
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
class: ResultCacheE2EFileDependency\DataStaticMethodReturnTypeExtension
tags:
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
3 changes: 3 additions & 0 deletions e2e/result-cache-file-dependency/src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return 'quiet';
13 changes: 13 additions & 0 deletions e2e/result-cache-file-dependency/src/ConfigHolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ResultCacheE2EFileDependency;

class ConfigHolder
{

public function check(): void
{
checkConfig();
}

}
20 changes: 20 additions & 0 deletions e2e/result-cache-file-dependency/src/Holder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace ResultCacheE2EFileDependency;

class Holder
{

private $value;

public function __construct()
{
$this->value = holderData();
}

public function get(): int
{
return $this->value;
}

}
Loading
Loading