Comments (14)
FYI, we've achieved some results when using PHPStan together with https://github.com/GiacoCorsiglia/wordpress-stubs, but we had to manually edit the generated file to overcome this issue: GiacoCorsiglia/php-stubs-generator#1
Perhaps this helps someone.
from phpstan.
You can try making it work using PHPStan autoloading settings (autoload_files
and autoload_directories
, see: Autoloading).
from phpstan.
I tried this:
autoload_files:
- %rootDir%/../../../../wp-load.php
Running PHPStan in Level 0 then gives me a PHP Notice: Undefined index: SERVER_PROTOCOL in ~wordpress-project/wp-includes/load.php on line 16
and a call stack:
PHP Notice: Undefined index: HTTP_HOST in <my-wordpress-dir>/wp-includes/ms-settings.php on line 48
PHP Stack trace:
PHP 1. {main}() <my-wordpress-dir>/repo/vendor/phpstan/phpstan/bin/phpstan:0
PHP 2. Symfony\Component\Console\Application->run() <my-wordpress-dir>/repo/vendor/phpstan/phpstan/bin/phpstan:26
PHP 3. Symfony\Component\Console\Application->doRun() <my-wordpress-dir>/repo/vendor/symfony/console/Application.php:130
PHP 4. Symfony\Component\Console\Application->doRunCommand() <my-wordpress-dir>/repo/vendor/symfony/console/Application.php:223
PHP 5. PHPStan\Command\AnalyseCommand->run() <my-wordpress-dir>/repo/vendor/symfony/console/Application.php:869
PHP 6. PHPStan\Command\AnalyseCommand->execute() <my-wordpress-dir>/repo/vendor/symfony/console/Command/Command.php:264
PHP 7. require_once() <my-wordpress-dir>/repo/vendor/phpstan/phpstan/src/Command/AnalyseCommand.php:179
PHP 8. require_once() <my-wordpress-dir>/wp-load.php:37
PHP 9. require_once() <my-wordpress-dir>/wp-config.php:175
PHP 10. require() <my-wordpress-dir>/wp-settings.php:123
Notice: Undefined index: HTTP_HOST in <my-wordpress-dir>/wp-includes/ms-settings.php on line 48
Call Stack:
0.0004 363792 1. {main}() <my-wordpress-dir>/repo/vendor/phpstan/phpstan/bin/phpstan:0
0.0328 5302024 2. Symfony\Component\Console\Application->run() <my-wordpress-dir>/repo/vendor/phpstan/phpstan/bin/phpstan:26
0.0404 5601464 3. Symfony\Component\Console\Application->doRun() <my-wordpress-dir>/repo/vendor/symfony/console/Application.php:130
0.0405 5601464 4. Symfony\Component\Console\Application->doRunCommand() <my-wordpress-dir>/repo/vendor/symfony/console/Application.php:223
0.0405 5601464 5. PHPStan\Command\AnalyseCommand->run() <my-wordpress-dir>/repo/vendor/symfony/console/Application.php:869
0.0407 5604584 6. PHPStan\Command\AnalyseCommand->execute() <my-wordpress-dir>/repo/vendor/symfony/console/Command/Command.php:264
0.0464 6317264 7. require_once('<my-wordpress-dir>/wp-load.php') <my-wordpress-dir>/repo/vendor/phpstan/phpstan/src/Command/AnalyseCommand.php:179
0.0465 6341288 8. require_once('<my-wordpress-dir>/wp-config.php') <my-wordpress-dir>/wp-load.php:37
0.0468 6430736 9. require_once('<my-wordpress-dir>/wp-settings.php') <my-wordpress-dir>/wp-config.php:175
0.0596 9416528 10. require('<my-wordpress-dir>/wp-includes/ms-settings.php') <my-wordpress-dir>/wp-settings.php:123
PHP Notice: Undefined index: SERVER_PROTOCOL in <my-wordpress-dir>/wp-includes/load.php on line 16
PHP Stack trace:
PHP 1. {main}() <my-wordpress-dir>/repo/vendor/phpstan/phpstan/bin/phpstan:0
PHP 2. Symfony\Component\Console\Application->run() <my-wordpress-dir>/repo/vendor/phpstan/phpstan/bin/phpstan:26
PHP 3. Symfony\Component\Console\Application->doRun() <my-wordpress-dir>/repo/vendor/symfony/console/Application.php:130
PHP 4. Symfony\Component\Console\Application->doRunCommand() <my-wordpress-dir>/repo/vendor/symfony/console/Application.php:223
PHP 5. PHPStan\Command\AnalyseCommand->run() <my-wordpress-dir>/repo/vendor/symfony/console/Application.php:869
PHP 6. PHPStan\Command\AnalyseCommand->execute() <my-wordpress-dir>/repo/vendor/symfony/console/Command/Command.php:264
PHP 7. require_once() <my-wordpress-dir>/repo/vendor/phpstan/phpstan/src/Command/AnalyseCommand.php:179
PHP 8. require_once() <my-wordpress-dir>/wp-load.php:37
PHP 9. require_once() <my-wordpress-dir>/wp-config.php:175
PHP 10. ms_site_check() <my-wordpress-dir>/wp-settings.php:452
PHP 11. apply_filters() <my-wordpress-dir>/wp-includes/ms-load.php:79
PHP 12. WP_Hook->apply_filters() <my-wordpress-dir>/wp-includes/plugin.php:203
PHP 13. VGL\Controller\SEOController->redirectInactiveBlogs() <my-wordpress-dir>/wp-includes/class-wp-hook.php:298
PHP 14. VGL\Service\WPRedirectService->redirectToHome() <my-wordpress-dir>/src/Controller/SEOController.php:287
PHP 15. wp_redirect() <my-wordpress-dir>/src/Service/WPRedirectService.php:25
PHP 16. status_header() <my-wordpress-dir>/wp-includes/pluggable.php:1208
PHP 17. wp_get_server_protocol() <my-wordpress-dir>/wp-includes/functions.php:1070
and a similar one about SERVER_PROTOCOL
being undefined. My guess is that due to the way things work in the wordpress codebase, we have some of the problems mentioned in #67 here (mixed class definitions and procedural code).
PHPStan doesn't even start analyzing, it seems the runtime simply quits after printing the Notices.
from phpstan.
from phpstan.
@Mte90 Try this in PHP or simple variables in shell: https://github.com/wp-cli/wp-cli/blob/851a650d8518d94acc91b17ee098f3cfcbfeabd3/php/wp-cli.php#L16-L20
from phpstan.
I think that variables are not enough, the scan need to run wordpress to analyze the plugin itself.
from phpstan.
I think the target market of phpstan is not WordPress. I am a WordPress expert so I know its weaknesses.
from phpstan.
Yeah I know, in the meantime there was more tool helpful on that with PHPCS and also Tide that is a work in progress so for that reason I didn't added any update to that ticket.
from phpstan.
Hi, PHPStan works best on modern object-oriented code. I might try to make some optimizations so that it's usable and useful on WP as well, but I think it's better to focus my efforts on codebases that might benefit from PHPStan far more.
from phpstan.
Seems cool maybe write a tutorial/wiki page with all the steps with also the manual patch to use everything can be very helpful :-D
from phpstan.
I will keep you posted as soon as I have a complete result. Might take time, though.
from phpstan.
For anyone interested: you may get started by autoloading https://github.com/GiacoCorsiglia/wordpress-stubs
parameters:
autoload_files:
- %currentWorkingDirectory%/wordpress-stubs.php
phpstan analyze --level=0 wp-admin/
it takes several minutes.
from phpstan.
It is also possible to use PHPStan without stubs, i just applied it in the following library:
https://github.com/wp-pay-gateways/omnikassa-2
We use the following libraries for this:
composer require --dev johnpbloch/wordpress
composer require --dev wp-phpunit/wp-phpunit
phpstan.neon.dist
parameters:
customRulesetUsed: false
level: max
bootstrap: tests/phpstan-bootstrap.php
paths:
- src
tests/phpstan-bootstrap.php
<?php
putenv( sprintf( 'WP_PHPUNIT__TESTS_CONFIG=%s', 'tests/wp-config.php' ) );
require_once __DIR__ . '/../vendor/autoload.php';
require_once getenv( 'WP_PHPUNIT__DIR' ) . '/includes/functions.php';
require getenv( 'WP_PHPUNIT__DIR' ) . '/includes/bootstrap.php';
from phpstan.
For googlers: see https://github.com/szepeviktor/phpstan-wordpress
Contributions welcome!
from phpstan.
Related Issues (20)
- False positive "instanceof.alwaysTrue"/"function.alreadyNarrowedType" for array_map with callback checking types and "treatPHPDocAsCertain:false" HOT 1
- Crash: Argument #3 ($step) must be less than the range spanned by argument #1 ($start) and argument #2 ($end) HOT 1
- Memory allocation issue in V1.11.0 (worked in V1.10.67) HOT 11
- modify super-global should be impure HOT 3
- Wrong filename is reported instead of trait HOT 1
- Add Configuration Option to Ignore PHPDoc in Favor of Native Type Hints HOT 4
- Unexpected `<array<array<T>>>` reported by PHPStan when using template types HOT 1
- Conditional types by enum not working
- Wrong: Strict comparison using === between '0' and non-falsy-string will always evaluate to false.
- Missed DocBlock instruction when using a mix of attributes and annotations HOT 3
- Removing/narrowing type does not work HOT 2
- Invalid int value range despite previous check
- Inconsistent error on generics
- Constants as keys in array shapes HOT 3
- Property with default value should be considered unused HOT 3
- False positove Variable might not be defined. HOT 2
- Internal error when adding return types with an ObjectType HOT 10
- $string + 1 reported as errorneous HOT 3
- Error identifiers in error output summary HOT 2
- Checked exceptions whitelisted methods configuration HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from phpstan.