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
1 change: 1 addition & 0 deletions build/collision-detector.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"../tests/PHPStan/Parser/data/cleaning-property-hooks-after.php",
"../tests/PHPStan/Rules/Functions/data/duplicate-function.php",
"../tests/PHPStan/Rules/Classes/data/duplicate-class.php",
"../tests/PHPStan/Reflection/BetterReflection/SourceLocator/data/group/second",
"../tests/PHPStan/Rules/Names/data/multiple-namespaces.php",
"../tests/PHPStan/Rules/Names/data/repeated-namespaces.php",
"../tests/PHPStan/Rules/Names/data/repeated-braced-namespaces.php",
Expand Down
4 changes: 2 additions & 2 deletions e2e/ext-ds-version/ext-ds-version_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function test_changed_ext_ds_version_reanalyses_dependent_files() {
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
assert_same "1" "$CODE"
echo "$OUTPUT"
assert_contains 'PHP extension versions changed (ext-ds); re-analysing the files depending on them and the files with errors.' "$OUTPUT"
assert_contains 'PHP extension versions changed (ext-ds); re-analysing the files depending on them.' "$OUTPUT"
assert_contains 'Foo.php:11:Instantiated class Ds\Vector not found.' "$OUTPUT"
assert_contains 'Bar.php:15:Call to an undefined method Ds\Pair<string, int>::copy().' "$OUTPUT"
assert_not_contains 'Ds\Seq' "$OUTPUT"
Expand All @@ -39,7 +39,7 @@ function test_changed_ext_ds_version_reanalyses_dependent_files() {
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
assert_same "1" "$CODE"
echo "$OUTPUT"
assert_contains 'PHP extension versions changed (ext-ds); re-analysing the files depending on them and the files with errors.' "$OUTPUT"
assert_contains 'PHP extension versions changed (ext-ds); re-analysing the files depending on them.' "$OUTPUT"
assert_contains 'Foo.php:12:Instantiated class Ds\Seq not found.' "$OUTPUT"
assert_not_contains 'Ds\Vector' "$OUTPUT"
assert_not_contains 'Bar.php' "$OUTPUT"
Expand Down
2 changes: 2 additions & 0 deletions e2e/result-cache-autoload-entries/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
/composer.lock
Empty file.
29 changes: 29 additions & 0 deletions e2e/result-cache-autoload-entries/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "phpstan/result-cache-autoload-entries-e2e",
"repositories": [
{
"type": "path",
"url": "./path-lib"
}
],
"require": {
"test/path-lib": "*"
},
"autoload": {
"psr-4": {
"AutoloadEntries\\": "src/",
"AutoloadEntries\\Lib\\": "lib/",
"AutoloadEntries\\Root\\": ""
},
"psr-0": {
"AutoloadEntries_Legacy_": "legacy/"
},
"classmap": ["classmap/"],
"files": ["helpers.php"]
},
"autoload-dev": {
"psr-4": {
"AutoloadEntries\\Tests\\": "tests/"
}
}
}
3 changes: 3 additions & 0 deletions e2e/result-cache-autoload-entries/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php declare(strict_types = 1);

namespace AutoloadEntries;
Empty file.
Empty file.
1 change: 1 addition & 0 deletions e2e/result-cache-autoload-entries/path-lib/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "name": "test/path-lib", "version": "1.0.0", "autoload": { "psr-4": { "PathLib\\": "src/" }, "files": ["functions.php"] } }
3 changes: 3 additions & 0 deletions e2e/result-cache-autoload-entries/path-lib/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php declare(strict_types = 1);

namespace PathLib;
Empty file.
5 changes: 5 additions & 0 deletions e2e/result-cache-autoload-entries/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,201 @@
#!/usr/bin/env bash
# @tags result-cache

# The files of the project's own Composer autoload entries - PSR-4, PSR-0, classmap, files, and
# those of a package from a path repository - are tracked like scanned files: a class or function
# appearing in one of them re-analyses the files that looked it up, and nothing else.

function set_up() {
e2e::set_up
composer install --quiet
}

function tear_down() {
e2e::tear_down
}

# writes a file: write_php <path> <namespace> <code after the namespace>
function write_php() {
mkdir -p "$(dirname "$1")"
if [ -n "$2" ]; then
printf '<?php declare(strict_types = 1);\n\nnamespace %s;\n\n%s\n' "$2" "$3" > "$1"
else
printf '<?php declare(strict_types = 1);\n\n%s\n' "$3" > "$1"
fi
}

function assert_files_to_analyse() {
INFO=$(../../bin/phpstan result-cache-info --json)
echo "$INFO"
assert_equals 'true' "$(echo "$INFO" | jq --raw-output '.resultCacheUsed')"
assert_equals "$1" "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
}

function assert_same_as_cold_run() {
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
echo "$OUTPUT"
rm -rf tmp/resultCache.php
COLD_OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && COLD_CODE=0 || COLD_CODE=$?
echo "$COLD_OUTPUT"
assert_same "$COLD_CODE" "$CODE"
assert_same "$COLD_OUTPUT" "$OUTPUT"
}

function analyse() {
OUTPUT=$(../../bin/phpstan analyse --error-format raw 2>&1) && CODE=0 || CODE=$?
echo "$OUTPUT"
}

# src/Consumer.php uses the class, src/Unrelated.php does not: write_class_consumer <class>
function write_class_consumer() {
write_php src/Consumer.php AutoloadEntries "function consume(): int
{
return (new \\$1())->value();
}"
write_php src/Unrelated.php AutoloadEntries 'function unrelated(): int
{
return 1;
}'
analyse
assert_same '1' "$CODE"
assert_contains 'Consumer.php:7:Instantiated class' "$OUTPUT"
}

# the class appears, then its method body changes, then its signature does
function assert_class_appears() {
write_php "$1" "$2" "class $3
{

public function value(): int
{
return 1;
}

}"
assert_files_to_analyse 1
assert_same_as_cold_run
assert_same '0' "$CODE"

sed -i 's/return 1;/return 2;/' "$1"
assert_files_to_analyse 0
assert_same_as_cold_run

sed -i 's/): int/): string/; s/return 2;/return "x";/' "$1"
assert_files_to_analyse 1
assert_same_as_cold_run
assert_contains 'Consumer.php:7:Function AutoloadEntries\consume() should return int but returns string.' "$OUTPUT"

rm "$1"
assert_files_to_analyse 1
assert_same_as_cold_run
assert_contains 'Consumer.php:7:Instantiated class' "$OUTPUT"
}

function test_class_appears_in_a_psr4_directory() {
write_class_consumer 'AutoloadEntries\Lib\Thing'
assert_class_appears lib/Thing.php 'AutoloadEntries\Lib' Thing
}

function test_class_appears_in_a_psr0_directory() {
write_class_consumer 'AutoloadEntries_Legacy_Thing'
assert_class_appears legacy/AutoloadEntries/Legacy/Thing.php '' AutoloadEntries_Legacy_Thing
}

function test_class_appears_in_a_classmap_directory() {
write_class_consumer 'AutoloadEntries\Mapped\Thing'
# a classmap file is found by what it declares, not by its name
assert_class_appears classmap/whatever.php 'AutoloadEntries\Mapped' Thing
}

function test_class_appears_in_an_autoload_dev_directory() {
write_class_consumer 'AutoloadEntries\Tests\Thing'
assert_class_appears tests/Thing.php 'AutoloadEntries\Tests' Thing
}

function test_class_appears_in_a_path_package() {
write_class_consumer 'PathLib\Thing'
assert_class_appears path-lib/src/Thing.php PathLib Thing
}

function test_class_appears_under_a_psr4_namespace_mapped_to_the_project_root() {
write_class_consumer 'AutoloadEntries\Root\Thing'
# the tmpDir is under the project root too, but PHPStan's own files there are not scanned files
OUTPUT=$(../../bin/phpstan analyse -vv 2>&1) || true
assert_contains 'Result cache was not rewritten because it is unchanged.' "$OUTPUT"
assert_class_appears Thing.php 'AutoloadEntries\Root' Thing
}

function test_class_appears_in_an_existing_psr4_file_of_another_name() {
write_php lib/Existing.php 'AutoloadEntries\Lib' 'class Existing
{

}'
write_class_consumer 'AutoloadEntries\Lib\Thing'
# the edited file is read for what it declares, and Thing is looked up again - but no PSR-4 path
# leads to it, so a cold run does not find it either
printf '\nclass Thing\n{\n\n\tpublic function value(): int\n\t{\n\t\treturn 1;\n\t}\n\n}\n' >> lib/Existing.php
assert_files_to_analyse 0
assert_same_as_cold_run
assert_contains 'Consumer.php:7:Instantiated class' "$OUTPUT"
}

# src/Consumer.php calls the function, src/Unrelated.php does not: write_function_consumer <function>
function write_function_consumer() {
write_php src/Consumer.php AutoloadEntries "function consume(): int
{
return \\$1();
}"
write_php src/Unrelated.php AutoloadEntries 'function unrelated(): int
{
return 1;
}'
analyse
assert_same '1' "$CODE"
assert_contains 'Consumer.php:7:Function' "$OUTPUT"
}

# the function appears in an existing file, then its body changes, then its signature does
function assert_function_appears() {
printf '\nfunction %s(): int\n{\n\treturn 1;\n}\n' "$2" >> "$1"
assert_files_to_analyse 1
assert_same_as_cold_run
assert_same '0' "$CODE"

sed -i 's/return 1;/return 2;/' "$1"
assert_files_to_analyse 0
assert_same_as_cold_run

sed -i 's/): int/): string/; s/return 2;/return "x";/' "$1"
assert_files_to_analyse 1
assert_same_as_cold_run
assert_contains 'Consumer.php:7:Function AutoloadEntries\consume() should return int but returns string.' "$OUTPUT"
}

function test_function_appears_in_a_files_entry() {
write_function_consumer 'AutoloadEntries\helper'
assert_function_appears helpers.php helper
}

function test_function_appears_in_a_files_entry_of_a_path_package() {
write_function_consumer 'PathLib\helper'
assert_function_appears path-lib/functions.php helper
}

function test_class_appears_in_a_classmap_file_excluded_from_scanning() {
# excludePaths keeps the file out of the scanned files, and out of what PHPStan's classmap
# locator finds too - the class stays unknown, and nothing has to be analysed again
printf 'parameters:\n\tlevel: 8\n\ttmpDir: tmp\n\tpaths:\n\t\t- src\n\texcludePaths:\n\t\t- classmap/excluded/*\n' > phpstan.neon
write_class_consumer 'AutoloadEntries\Mapped\Thing'
write_php classmap/excluded/Thing.php 'AutoloadEntries\Mapped' 'class Thing
{

public function value(): int
{
return 1;
}

}'
assert_files_to_analyse 0
assert_same_as_cold_run
assert_contains 'Consumer.php:7:Instantiated class' "$OUTPUT"
}
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions e2e/result-cache-autoload-entries/tmp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.*
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ function test_result_cache_collector_sweep() {
echo "$OUTPUT"
assert_contains 'PlainCaller.php:8:Method ResultCacheE2ECollectorSweep\PlainCaller::doPlainCaller() has invalid return type ResultCacheE2ECollectorSweep\Missing.' "$OUTPUT"
assert_contains 'Caller.php:7:Call to function resultCacheE2ECollectorHelper() on a separate line has no effect.' "$OUTPUT"
# One new file defines both missing symbols. The files with errors are re-analysed - including
# the file holding the error of the collector-backed rule, which is not in the cached error map.
# One new file defines both missing symbols. Both lookups were recorded as missing, so the
# files making them are re-analysed - including the file holding the error of the
# collector-backed rule, which is not in the cached error map.
cp appears/nsfns.php src/nsfns.php
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
assert_same "0" "$CODE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ function test_result_cache_empty_file_gains_symbol() {
assert_same "1" "$CODE"
echo "$OUTPUT"
assert_contains 'user.php:7:Instantiated class ResultCacheE2EEmptyFileGainsSymbol\Thing not found.' "$OUTPUT"
# That file now declares a class. A root symbol appeared, so the files with errors have to
# be re-analysed, the same way a brand new file makes them re-analysed - the missing
# exported nodes entry must not be read as "nothing changed".
# That file now declares a class. src/user.php recorded it as missing, so it is re-analysed
# no matter whether the file declaring it was known to the cache before.
mv gains/script.php src/script.php
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
assert_same "0" "$CODE"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ function test_result_cache_file_dependency() {
assert_not_contains 'Holder.php:17:Method ResultCacheE2EFileDependency\Holder::get() should return int but returns string.' "$OUTPUT"
assert_not_contains 'UsesHolder.php:9:Function ResultCacheE2EFileDependency\usesHolder() should return int but returns string.' "$OUTPUT"
# And for a declared file that is analysed itself: deleted, it is no longer analysed, and the files
# of the rule's nodes are re-analysed; created again, all three are - and as with any new file, so
# are the three files with errors.
# of the rule's nodes are re-analysed; created again, all three are - but not the three files with
# errors, the file declares no symbol they could be waiting for.
rm src/Config.php
INFO=$(../../bin/phpstan result-cache-info --json)
echo "$INFO"
Expand All @@ -145,7 +145,7 @@ function test_result_cache_file_dependency() {
printf "<?php\n\nreturn 'report';\n" > src/Config.php
INFO=$(../../bin/phpstan result-cache-info --json)
echo "$INFO"
assert_equals '6' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
assert_equals '3' "$(echo "$INFO" | jq --raw-output '.filesToAnalyseCount')"
OUTPUT=$(../../bin/phpstan analyse -vv --error-format raw 2>&1) && CODE=0 || CODE=$?
assert_same "1" "$CODE"
echo "$OUTPUT"
Expand Down
2 changes: 2 additions & 0 deletions e2e/result-cache-missing-classes/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
/composer.lock
10 changes: 10 additions & 0 deletions e2e/result-cache-missing-classes/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "phpstan/result-cache-missing-classes-e2e",
"autoload": {
"psr-4": {
"ResultCacheE2EMissingClasses\\": "src/",
"ResultCacheE2EMissingClasses\\Lib\\": "lib/",
"ResultCacheE2EMissingClassesRules\\": "rules/"
}
}
}
Empty file.
8 changes: 8 additions & 0 deletions e2e/result-cache-missing-classes/phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 8
tmpDir: tmp
paths:
- src

rules:
- ResultCacheE2EMissingClassesRules\ReplacementRule
Loading
Loading