Skip to content

Conversation

@taufek
Copy link
Contributor

@taufek taufek commented Jan 28, 2018

Added 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

@taufek taufek force-pushed the tj-phpstan branch 3 times, most recently from 9e16644 to 8fdcc24 Compare January 29, 2018 15:26
Copy link
Owner

@sds sds left a 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")
Copy link
Owner

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 = ''

Copy link
Contributor Author

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

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opps. Will remove it.

include: '**/*.php'

PhpStan:
description: 'Analysing PHP codes'
Copy link
Owner

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

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?

Copy link
Contributor Author

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])
Copy link
Owner

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.

Copy link
Contributor Author

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

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?

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'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
```
@taufek
Copy link
Contributor Author

taufek commented Jan 30, 2018

@sds ,

Thanks for the feedback. I've updated accordingly. Ready for re-review.

@sds sds merged commit e29265e into sds:master Jan 30, 2018
@sds
Copy link
Owner

sds commented Jan 30, 2018

Thanks for addressing those comments, @taufek. Looks great!

@taufek taufek deleted the tj-phpstan branch February 7, 2018 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants