-
Notifications
You must be signed in to change notification settings - Fork 280
Add PhpStan (PHP Static Code Analyser) PreCommit Hook #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
9e16644 to
8fdcc24
Compare
sds
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, @taufek. A few minor issues with the implementation but overall looks good. Fix those (and rebase) and we'll be good to merge here. Thanks!
| before do | ||
| sample_output = [ | ||
| '' | ||
| ].join("\n") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be better expressed as just an empty string.
sample_output = ''There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
| '/sample1.php:14:Call to an undefined static method Sample1::where()', | ||
| '/sample2.php:17:Anonymous function has an unused use $myVariable.' | ||
| ].join("\n") | ||
| # rubocop:enable Metrics/LineLength |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this comment here? It doesn't seem to be necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opps. Will remove it.
config/default.yml
Outdated
| include: '**/*.php' | ||
|
|
||
| PhpStan: | ||
| description: 'Analysing PHP codes' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's keep this consistent with what we do elsewhere and write:
Analyze with phpstan
| applicable_files.each do |file| | ||
| result = execute(command, args: [file]) | ||
| output = result.stdout.chomp | ||
| if result.status |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
status is a number, which will always be truthy, which I don't think is what you intended here. Let's use
result.success?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Will update.
| messages = [] | ||
|
|
||
| applicable_files.each do |file| | ||
| result = execute(command, args: [file]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason you're only running one file at a time? This will run very slowly for a large number of files, since you're running them in serial.
Unless there's something about how the tool manages this, would strongly recommend you pass all files to phpstan in one run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will update. It does make sense.
|
|
||
| applicable_files.each do |file| | ||
| result = execute(command, args: [file]) | ||
| output = result.stdout.chomp |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to assign this variable unless the exit status was successful, so perhaps only do this if result.success?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll give it a try.
Added [PhpStan](https://github.com/phpstan/phpstan) PreCommit Hook. **Example Files Change** ``` // app/foo.php <?php namespace App; class Foo { public function __construct($foo, $test) { $this->foo = $foo; } } // app/bar.php <?php namespace App; class Bar { public function __construct($foo) { $this->foo = $foo; } private function testMe() { return $this->hello; } } ``` **Example Error Messages*** ``` Running pre-commit hooks Analysing PHP codes...............................[PhpStan] FAILED Errors on modified lines: /app/bar.php:9:Access to an undefined property App\Bar::$foo. /app/bar.php:14:Access to an undefined property App\Bar::$hello. /app/foo.php:7:Constructor of class App\Foo has an unused parameter $test. /app/foo.php:9:Access to an undefined property App\Foo::$foo. ✗ One or more pre-commit hooks failed ```
|
@sds , Thanks for the feedback. I've updated accordingly. Ready for re-review. |
|
Thanks for addressing those comments, @taufek. Looks great! |
Added PhpStan PreCommit Hook.
Example Files Change
Example Error Messages