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

WIP: Add PHPStanBear #1468

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

WIP: Add PHPStanBear #1468

wants to merge 2 commits into from

Conversation

damngamerz
Copy link
Member

@damngamerz damngamerz commented Feb 28, 2017

Introduce PHPStanBear
Checks the code with phpstan analyze.
Closes #1426

For short term contributors: we understand that getting your commits well
defined like we require is a hard task and takes some learning. If you
look to help without wanting to contribute long term there's no need
for you to learn this. Just drop us a message and we'll take care of brushing
up your stuff for merge!

Checklist

  • I read the commit guidelines and I've followed
    them.
  • I ran coala over my code locally. (All commits have to pass
    individually.
    It is not sufficient to have "fixup commits" on your PR,
    our bot will still report the issues for the previous commit.) You will
    likely receive a lot of bot comments and build failures if coala does not
    pass on every single commit!

After you submit your pull request, DO NOT click the 'Update Branch' button.
When asked for a rebase, consult coala.io/rebase
instead.

Please consider helping us by reviewing other peoples pull requests as well:

The more you review, the more your score will grow at coala.io and we will
review your PRs faster!

LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Syntax'}
@staticmethod
def create_arguments(filename, file, config_file):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line contains following spacing inconsistencies:

  • Trailing whitespaces.

SpaceConsistencyBear, severity NORMAL, section python.

The issue can be fixed by applying the following patch:

--- a/bears/php/PHPStanBear.py
+++ b/bears/php/PHPStanBear.py
@@ -16,5 +16,5 @@
     LICENSE = 'AGPL-3.0'
     CAN_DETECT = {'Syntax'}
     @staticmethod
-    def create_arguments(filename, file, config_file):        
+    def create_arguments(filename, file, config_file):
         return ('analyse', '--no-ansi', filename)

LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Syntax'}
@staticmethod
def create_arguments(filename, file, config_file):
Copy link
Collaborator

Choose a reason for hiding this comment

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

W291 trailing whitespace'

PycodestyleBear (W291), severity NORMAL, section autopep8.

AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Syntax'}
@staticmethod
Copy link
Collaborator

Choose a reason for hiding this comment

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

E301 expected 1 blank line, found 0'

PycodestyleBear (E301), severity NORMAL, section autopep8.

AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Syntax'}
Copy link
Collaborator

Choose a reason for hiding this comment

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

The code does not comply to PEP8.

PEP8Bear, severity NORMAL, section autopep8.

The issue can be fixed by applying the following patch:

--- a/bears/php/PHPStanBear.py
+++ b/bears/php/PHPStanBear.py
@@ -15,6 +15,7 @@
     AUTHORS_EMAILS = {'[email protected]'}
     LICENSE = 'AGPL-3.0'
     CAN_DETECT = {'Syntax'}
+
     @staticmethod
-    def create_arguments(filename, file, config_file):        
+    def create_arguments(filename, file, config_file):
         return ('analyse', '--no-ansi', filename)

@damngamerz
Copy link
Member Author

The Bear works perfectly adding Screen shots for reference.
screenshot from 2017-02-28 01-52-50
Results for tests.(I think I covered it all 😄 )
screenshot from 2017-02-28 14-32-03

@damngamerz damngamerz force-pushed the phpstan-b branch 3 times, most recently from 4caf034 to 00e1649 Compare February 28, 2017 10:35

@staticmethod
def create_arguments(filename, file, config_file):
return ('analyse', '--no-ansi', filename)
Copy link
Member

Choose a reason for hiding this comment

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

PHPStan support different Rule levels as well. Please add arguments for rule_level as well. Check https://github.com/phpstan/phpstan#rule-levels

Copy link
Member

Choose a reason for hiding this comment

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

Also, you need to add the -c option followed by config_file for the configuration file to work.

Copy link
Member Author

@damngamerz damngamerz Feb 28, 2017

Choose a reason for hiding this comment

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

@satwikkansal There's something wrong with the rule_level i have tried to add argument this way

    def create_arguments(filename, file, config_file,phpstan_level: str='0'):        
        return ('analyse', '--no-ansi','-l '+phpstan_level,filename)

now while executing coala over a bad_file

coala --bear-dirs=. --bears=PHPStanBear --files=hello-world.php -V --flush-cache
[WARNING][20:20:01] The default coafile '.coafile' was not found. You can generate a configuration file with your current options by adding the `--save` flag or suppress any use of config files with `-I`.
[DEBUG][20:20:01] Platform Linux -- Python 3.6.0, coalib 0.10.0.dev99999999999999
[DEBUG][20:20:01] The file cache was successfully flushed.
Executing section cli...
[DEBUG][20:20:01] coala is run only on changed files, bears' log messages from previous runs may not appear. You may use the `--flush-cache` flag to see them.
[DEBUG][20:20:01] Files that will be checked:
/home/damngamerz/bear/hello-world.php
[DEBUG][20:20:01] Running bear PHPStanBear...
[DEBUG][20:20:01] Running 'phpstan analyse --no-ansi -l 0 /home/damngamerz/bear/hello-world.php'

it doesn't catch any error.regex is fine cause earlier it was catching as you can see my screen shot which i have posted.
And similarly tried for -c argument for config-file

def create_arguments(filename, file, config_file,phpstan_config: str=os.devnull):        
        return ('analyse', '--no-ansi','-c '+phpstan_config,filename)

same thing happens while running coala for this.

 coala --bear-dirs=. --bears=PHPStanBear --files=hello-world.php -V --flush-cache
[WARNING][20:40:56] The default coafile '.coafile' was not found. You can generate a configuration file with your current options by adding the `--save` flag or suppress any use of config files with `-I`.
[DEBUG][20:40:56] Platform Linux -- Python 3.6.0, coalib 0.10.0.dev99999999999999
[DEBUG][20:40:56] The file cache was successfully flushed.
Executing section cli...
[DEBUG][20:40:56] coala is run only on changed files, bears' log messages from previous runs may not appear. You may use the `--flush-cache` flag to see them.
[DEBUG][20:40:56] Files that will be checked:
/home/damngamerz/bear/hello-world.php
[DEBUG][20:40:56] Running bear PHPStanBear...
[DEBUG][20:40:56] Running 'phpstan analyse --no-ansi -c /dev/null /home/damngamerz/bear/hello-world.php'

But i have noticed one thing, while invoking this following command in cli gives

phpstan analyse --no-ansi -c /dev/null /home/damngamerz/bear/hello-world.php
Project config file at path /dev/null does not exist.

So for config-file im doing something wrong.Any Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Project config file at path /dev/null does not exist

@damngamerz The error says that the path /dev/null which you provide as config file is not a valid path which is True as you've not create any configuration file yet.

Copy link
Member

Choose a reason for hiding this comment

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

Also regarding rule-level, try running phpstan directly with different levels and see if there is any output or if the regex changes. In your case, you've set the default level to 0 (which is the loosest), so I reckon that's why it wasn't able to capture your bad file errors.

Copy link
Member Author

@damngamerz damngamerz Feb 28, 2017

Choose a reason for hiding this comment

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

@satwikkansal well then what can i do with -c so the phpstan can execute over the file without this error?
By default while running phpstan the level is set to 0.And i had ran that command in cli

phpstan analyse --no-ansi -l 0 /home/damngamerz/bear/hello-world.php
 ------ ----------------------------------------------------------------- 
  Line   hello-world.php                                                  
 ------ ----------------------------------------------------------------- 
  7      Syntax error, unexpected T_ECHO, expecting ';' or '{' on line 7  
 ------ ----------------------------------------------------------------- 

 [ERROR] Found 1 error                                                          

Same error reproduced.

Copy link
Member

@satwikkansal satwikkansal Feb 28, 2017

Choose a reason for hiding this comment

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

@damngamerz You must be having some bugs in your code that are causing this.
Please read our Developer's guide thoroughly as most of the things we generally need are mentioned over there, read the documentation of PHPStan to know how exactly their config file works.
And lastly, in case of some error feel free to discuss on the Gitter channel (as I might miss your notifications, others will still be able to help you out) 😉

AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Syntax'}
Copy link
Member

Choose a reason for hiding this comment

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

For PHPStan's readme, it seems like it can probably detect Unused Code, 'Variable Misuse', etc. Make sure to add all labels that seem appropriate to CAN_DETECT set. You can refer bear class documentation for the labels ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yup added the followingCAN_DETECT = {'Syntax','Unused Code','Variable Misuse','Undefined Element','Missing Import','Spelling'}
Will be updating soon.


def test_run(self):
# Test a file with errors and warnings
self.check_validity(
Copy link
Member

Choose a reason for hiding this comment

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

check_validity asserts if your bear yields any results for a particular check with a list of strings. However, IMO it is good to check the result itself as well. Try using LocalBearTestHelper.check_results to check the results given by your bears.

Copy link
Member

Choose a reason for hiding this comment

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

Also, not necessary, but you can use verify_local_bear to create good_files and bad_files (good_file is a file which your bear considers as non-style-violating and a bad_file is one which has at least one error/warning/info)

Copy link
Member Author

Choose a reason for hiding this comment

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

@satwikkansal Well I have used check_validity as one of the other bear test file PHPLintBearTest uses those two test files (i.e.test_files/phplint_test1.php & test_files/phplint_test1.php).So if we have the resources why not use them.Should i really change it to check_results ? Cause i have read in the docs we can use any of those 3 methods to test a bear.

Copy link
Member

Choose a reason for hiding this comment

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

@damngamerz
check_validity just checks if there is a result from the bear or not, to test what exactly the result should be, we need to use check_results.
Here are the docs

Copy link
Member Author

Choose a reason for hiding this comment

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

@satwikkansal cool i will update 👍 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

Im changing to verify_local_bear as later i can append more tests easily 👍

"""
:param phpstan_config:
path to a custom configuration file.
When using a custom project config file, you have to pass the phpstan_level argument
Copy link
Collaborator

Choose a reason for hiding this comment

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

E501 line too long (96 > 79 characters)'

PycodestyleBear (E501), severity NORMAL, section autopep8.

'Undefined Element', 'Missing Import', 'Spelling'}

@staticmethod
def create_arguments(filename, file, config_file, phpstan_level: str='0', phpstan_config: str=''):
Copy link
Collaborator

Choose a reason for hiding this comment

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

E501 line too long (102 > 79 characters)'

PycodestyleBear (E501), severity NORMAL, section autopep8.

"""
:param phpstan_config:
path to a custom configuration file.
When using a custom project config file, you have to pass the phpstan_level argument
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is longer than allowed. (96 > 79)

LineLengthBear, severity NORMAL, section linelength.

'Undefined Element', 'Missing Import', 'Spelling'}

@staticmethod
def create_arguments(filename, file, config_file, phpstan_level: str='0', phpstan_config: str=''):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Line is longer than allowed. (102 > 79)

LineLengthBear, severity NORMAL, section linelength.

"""


PHPStanBearTest = verify_local_bear(
Copy link
Member

Choose a reason for hiding this comment

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

We have been trying to increase test quality and we are trying to move away form verify_local_bear. Please check the bear output to make sure it is outputting the right error.

Copy link
Member Author

@damngamerz damngamerz Feb 28, 2017

Choose a reason for hiding this comment

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

@Mixih uhhh earlier i was using check_validity then i moved towards verify_local_bear on the suggestion of @satwikkansal . 😰

Copy link
Member

Choose a reason for hiding this comment

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

@damngamerz Sorry didn't knew that regarding verify_local_bear 😞
btw we should still use check_results over check_validity ( @Mixih please correct me if I'm wrong 😅 )

Copy link
Member Author

Choose a reason for hiding this comment

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

@satwikkansal @Mixih Most of the bear tests which i have seen are using check_validity and verify_local_bear.Till now i haven't seen a bear test using check_results so I'm really not that much comfortable with it.Decide guys 😛

Copy link
Member

Choose a reason for hiding this comment

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

The last line in testing docs says,

Use check_results as much as possible to test your bears.

Copy link
Member

@satwikkansal satwikkansal Feb 28, 2017

Choose a reason for hiding this comment

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

So using check_results won't harm because it is more rigorous than check_validity

Copy link
Member Author

@damngamerz damngamerz Feb 28, 2017

Choose a reason for hiding this comment

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

@satwikkansal Sorry but i need a second opinion this time.I don't want my effort to go vain again.As i said im not that much comfortable with check_results. And I haven't found any particular bear test using it.Most of the bear tests use check_validity and verify_local_bear.If i have to roll back to check_validity i can do that.
@Mixih Any thoughts about it ?

Copy link
Member

Choose a reason for hiding this comment

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

weeeell, we've been wanting proper test classes for a while. Please do use check_results with a proper test class with a skiptest decorator. The old way needs to go as it allows bears to spontaneously break.


@staticmethod
def create_arguments(filename, file, config_file,
phpstan_level: str='0', phpstan_config: str=''):
Copy link
Collaborator

Choose a reason for hiding this comment

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

E128 continuation line under-indented for visual indent'

PycodestyleBear (E128), severity NORMAL, section autopep8.


@staticmethod
def create_arguments(filename, file, config_file,
phpstan_level: str='0', phpstan_config: str=''):
Copy link
Collaborator

Choose a reason for hiding this comment

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

The code does not comply to PEP8.

PEP8Bear, severity NORMAL, section autopep8.

The issue can be fixed by applying the following patch:

--- a/bears/php/PHPStanBear.py
+++ b/bears/php/PHPStanBear.py
@@ -21,7 +21,7 @@
 
     @staticmethod
     def create_arguments(filename, file, config_file,
-                        phpstan_level: str='0', phpstan_config: str=''):
+                         phpstan_level: str='0', phpstan_config: str=''):
         """
         :param phpstan_config:
             path to a custom configuration file.

When using a custom project config file,
you have to pass the phpstan_level argument
(default value 0 does not apply here).
:param phpstan_level:
Copy link
Member

@satwikkansal satwikkansal Feb 28, 2017

Choose a reason for hiding this comment

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

When using a custom project config file, you have to pass the phpstan_level argument (default value 0 does not apply here).

If that's the case, please add check for this situation in the code so that it doesn't result in a runtime error.

Copy link
Member Author

@damngamerz damngamerz Feb 28, 2017

Choose a reason for hiding this comment

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

@satwikkansal uhhh I knew someone will point this out 😄 well i was searching for a solution in coala API docs and existing bears.But i couldn't find any interrupt kind of function, which can warn the user about this.Is there any?
One thing which i can propose is that setting the default value of rule_level as 1 whenever phpstan_config is used.And add these details in the Docstring.

Copy link
Member

Choose a reason for hiding this comment

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

Don't know if something relevant exists in coala api , but maybe you can do something like

if phpstan_config and phpstan_level:
    # add -c and phpstan_config to arguments

Copy link
Member Author

@damngamerz damngamerz Mar 1, 2017

Choose a reason for hiding this comment

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

I have initialized phpstan_level='0' as you can see, so that won't work.
Instead as i have suggested this works.Tested 👍

        args = ('analyse',)
        if phpstan_config:
            phpstan_level='1'
            args += ('--level='+phpstan_level,'-c '+phpstan_config, filename,)
        else:
            args += ('--level='+phpstan_level, filename,)
        return args

@damngamerz damngamerz force-pushed the phpstan-b branch 6 times, most recently from 988c7fc to b025699 Compare April 25, 2017 17:55
@jayvdb jayvdb modified the milestones: 0.11, 0.12 Apr 27, 2017
Introduce PHPStanBear
Checks the code with ``phpstan analyze``.
Closes coala#1426
@damngamerz damngamerz force-pushed the phpstan-b branch 3 times, most recently from ffb80de to 22fdbcb Compare April 28, 2017 18:45
@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

12 similar comments
@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

@gitmate-bot
Copy link
Collaborator

Hey! This pull request hasn't been updated for a while :/ It would be nice if we could get this going again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Integrate phpstan
7 participants