psalm / psalm-plugin-wordpress

WordPress stubs and plugin for Psalm
MIT License
69 stars 18 forks source link

Current Working Directory must not have Vendor Folder #9

Closed ktomk closed 3 years ago

ktomk commented 3 years ago

The project requires files out of its dependencies, that are stubs and wp-hooks metadata.

Previously while doing so the assumption is that the current working directory contains the vendor/... paths to the files.

This assumption is unchecked.

Fix is to verify the assumption of an existing directory and then continue with getcwd() as the base-path. If not and a path with a base relative to the plugin package itself resolves as a base-path for such paths, take it. Otherwise continue with getcwd() as base-path, even this may or may not result in non-existing file(s).

ktomk commented 3 years ago

I'm planning to file a PR for this, I have it under test in a project where it was just biting me.

ktomk commented 3 years ago

@joehoyle cr pls.

The vendor-dir problem could be easy to address now as (after creating the PR) I realized the current getcwd() behaviour is important to run the tests from sources (or at least I think so).

Therefore the new routine could cut away the leading vendor/ path and only traverse two directories (instead of three) and also verify those two are the ones of the project (humanmade/psalm-plugin-wordpress).

Also wondering how other projects are doing this in a similar situation.

ktomk commented 3 years ago

Updated the code to address the vendor-dir issue I noticed after the first push. This should be a more useful form now.

ktomk commented 3 years ago

full run that lead to the error:

/usr/bin/make -f /tmp/prj/Makefile-psalm.mk psalm
tools/psalm --config=.config/psalm/config.xml --no-cache
Scanning files...
Uncaught RuntimeException: PHP Error: file_get_contents(vendor/johnbillion/wp-hooks/hooks/actions.json): failed to open stream: No such file or directory in /tmp/prj/tools/vendor/humanmade/psalm-plugin-wordpress/Plugin.php:77 in /tmp/prj/tools/vendor/vimeo/psalm/src/Psalm/Internal/ErrorHandler.php:66
Stack trace:
#0 [internal function]: Psalm\Internal\ErrorHandler::Psalm\Internal\{closure}()
#1 /tmp/prj/tools/vendor/humanmade/psalm-plugin-wordpress/Plugin.php(77): file_get_contents()
#2 /tmp/prj/tools/vendor/humanmade/psalm-plugin-wordpress/Plugin.php(63): PsalmWordpress\Plugin::getHooksFromFile()
#3 /tmp/prj/tools/vendor/humanmade/psalm-plugin-wordpress/Plugin.php(43): PsalmWordpress\Plugin::loadStubbedHooks()
#4 /tmp/prj/tools/vendor/vimeo/psalm/src/Psalm/Config.php(1159): PsalmWordpress\Plugin->__invoke()
#5 /tmp/prj/tools/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php(585): Psalm\Config->initializePlugins()
#6 /tmp/prj/tools/vendor/vimeo/psalm/src/psalm.php(700): Psalm\Internal\Analyzer\ProjectAnalyzer->check()
#7 /tmp/prj/tools/vendor/vimeo/psalm/src/psalm.php(884): Psalm\{closure}()
#8 /tmp/prj/tools/vendor/vimeo/psalm/psalm(2): require_once('/home/mot/Phpst...')
#9 {main}

Next Psalm\Exception\ConfigException: Failed to load plugin PsalmWordPress\Plugin in /tmp/prj/tools/vendor/vimeo/psalm/src/Psalm/Config.php:1161
Stack trace:
#0 /tmp/prj/tools/vendor/vimeo/psalm/src/Psalm/Internal/Analyzer/ProjectAnalyzer.php(585): Psalm\Config->initializePlugins()
#1 /tmp/prj/tools/vendor/vimeo/psalm/src/psalm.php(700): Psalm\Internal\Analyzer\ProjectAnalyzer->check()
#2 /tmp/prj/tools/vendor/vimeo/psalm/src/psalm.php(884): Psalm\{closure}()
#3 /tmp/prj/tools/vendor/vimeo/psalm/psalm(2): require_once('/home/mot/Phpst...')
#4 {main}
(Psalm 4.7.2@83a0325c0a95c0ab531d6b90c877068b464377b5 crashed due to an uncaught Throwable)
make: *** [/tmp/prj/Makefile-psalm.mk:18: psalm] Error 1

Process finished with exit code 2

for this dependency configuration:

$ composer show | grep -E 'wp|stub|psalm'
humanmade/psalm-plugin-wordpress      2.0.4              WordPress stubs and plugin for Psalm.
johnbillion/wp-hooks                  0.4.4              All the actions and filters from WordPress core in machine-readable JSON format.
php-stubs/wordpress-stubs             v5.7.0             WordPress function and class declaration stubs for static analysis.
psalm/plugin-phpunit                  0.15.1             Psalm plugin for PHPUnit
vimeo/psalm                           4.7.2              A static analysis tool for finding errors in PHP applications
ktomk commented 3 years ago

@joehoyle thanks for taking care, can confirm fix in 2.0.5.