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
Integrating PHPStan & fixes of found issues #842
Conversation
9c0284b
to
6881bd4
Compare
Hey @ondrejmirtes thanks for this PR (and also for this great PHPStan tool, that use on many of my other projects) Nevertheless the fix are totally valuable Thoughts on that @briannesbitt ? |
If unit tests pass there's still something that can be broken in your code that isn't covered by tests. Take for example the Translator fixes - if |
Also, PHPStan can find more errors during refactoring that you'd otherwise find during testing or even in production. |
@@ -0,0 +1,7 @@ | |||
parameters: |
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.
Can you add this file not to be exported in .gitattributes
too?
@@ -0,0 +1,7 @@ | |||
parameters: | |||
ignoreErrors: | |||
- '#Access to an undefined property#' |
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.
Can you sort error messages ?
@@ -1091,8 +1091,9 @@ public static function hasRelativeKeywords($time) | |||
protected static function translator() | |||
{ | |||
if (static::$translator === null) { | |||
static::$translator = new Translator('en'); | |||
static::$translator->addLoader('array', new ArrayLoader()); | |||
$translator = new Translator('en'); |
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.
👍
@@ -9,6 +9,8 @@ | |||
* file that was distributed with this source code. | |||
*/ | |||
|
|||
namespace Tests\Carbon; |
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.
👍
script: vendor/bin/phpunit --verbose --coverage-text --coverage-clover=coverage.xml | ||
script: | ||
- vendor/bin/phpunit --verbose --coverage-text --coverage-clover=coverage.xml | ||
- if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:^0.5.2 && vendor/bin/phpstan analyse -c phpstan.neon -l 3 src tests; fi |
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.
Can you use long argument notation, config and level ?
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 not level 4?
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 will use the long arg notation.
Level 4 is not suitable for Carbon, it's better for PHP 7 codebases with scalar typehints. Take this example:
/** @var bool */
private $foo;
/**
* @param bool $foo
*/
public function setFoo($foo)
{
$this->foo = (bool) $fool
}
PHPStan will complain that you're casting bool
to bool
. But you can't remove the casting because something else can be passed there and to keep the integrity of the object, you need to be sure that only bool
is assigned to $this->foo
.
But in this case:
public function setFoo(bool $foo)
{
$this->foo = (bool) $fool
}
The (bool)
can be safely removed.
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 this explanations @ondrejmirtes , we can stick to level 3 then
I'll fix your suggestions over the weekend. I'd also like to release 0.6 soon that brings some fixes that will lower the number of ignored errors. Thanks 👍 |
Make sure to rebase and fix the conflict on |
b61d38c
to
bffa561
Compare
Fixed, rebased, build passes :) |
- php: 7.1 | ||
env: setup=lowest | ||
- php: 7.1 | ||
env: | ||
- coverage=true | ||
- setup=stable | ||
- php: 7.1 | ||
env: PHPSTAN=1 |
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.
Can you use phpstan=true
(lowercase, pseudo boolean) instead - just to keep consistent ? ;-)
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.
@lucasmichot Updated :)
Thanks for the great work @ondrejmirtes ! |
@lucasmichot You're welcome 😊 I'd be glad if you considered using PHPStan on your other, maybe even commercial projects 😊 If you have any questions or suggestion for PHPStan, get in touch 😊 |
This is a PR for integrating static analysis tool PHPStan that focuses on finding bugs and running in your CI build. It found a few issues that I put in separate commits.