Skip to content
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

Merged
merged 6 commits into from Jan 16, 2017

Conversation

ondrejmirtes
Copy link
Contributor

@ondrejmirtes ondrejmirtes commented Jan 8, 2017

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.

@lucasmichot
Copy link
Collaborator

Hey @ondrejmirtes thanks for this PR (and also for this great PHPStan tool, that use on many of my other projects)
I am not totally sure that it make sense to break a build if unit tests pass though, or to integrate it in TravisCI

Nevertheless the fix are totally valuable

Thoughts on that @briannesbitt ?

@ondrejmirtes
Copy link
Contributor Author

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 static::translator() returned an instance that doesn't have the addResource method, the code would break. PHPStan enforces more type safety and as a result, you can even write less tests - you don't have to write tests for "boring code" that just calls other methods. For example, people usually don't write tests for getters and setters because that would be a waste of time. But PHPStan can find a mistake such as a typo in a setter, or point out a wrong typehint. Some teams that use PHPStan even run it before running their unit tests.

@ondrejmirtes
Copy link
Contributor Author

Also, PHPStan can find more errors during refactoring that you'd otherwise find during testing or even in production.

@@ -0,0 +1,7 @@
parameters:
Copy link
Collaborator

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#'
Copy link
Collaborator

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');
Copy link
Collaborator

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;
Copy link
Collaborator

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
Copy link
Collaborator

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 ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not level 4?

Copy link
Contributor Author

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.

Copy link
Collaborator

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

@ondrejmirtes
Copy link
Contributor Author

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 👍

@lucasmichot
Copy link
Collaborator

Make sure to rebase and fix the conflict on .travis.yml @ondrejmirtes

@ondrejmirtes
Copy link
Contributor Author

Fixed, rebased, build passes :)

@lucasmichot lucasmichot removed the WIP label Jan 16, 2017
- php: 7.1
env: setup=lowest
- php: 7.1
env:
- coverage=true
- setup=stable
- php: 7.1
env: PHPSTAN=1
Copy link
Collaborator

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 ? ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucasmichot Updated :)

@lucasmichot
Copy link
Collaborator

Thanks for the great work @ondrejmirtes !

@ondrejmirtes
Copy link
Contributor Author

@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 😊

@ondrejmirtes ondrejmirtes deleted the phpstan branch January 16, 2017 20:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants