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

bears/php: Add PHPMessDetectorBear #1486

Merged
merged 1 commit into from Apr 22, 2017
Merged

bears/php: Add PHPMessDetectorBear #1486

merged 1 commit into from Apr 22, 2017

Conversation

yash-nisar
Copy link
Member

@yash-nisar yash-nisar commented Mar 5, 2017

Closes #22

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!

def load_testfile(name):
return open(get_testfile_path(name)).readlines()

@generate_skip_decorator(PHPMessDetectorBear)
Copy link
Collaborator

Choose a reason for hiding this comment

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

E302 expected 2 blank lines, found 1'

PycodestyleBear (E302), severity NORMAL, section autopep8.

@yash-nisar yash-nisar changed the title bears/php: Add PHPMessDetectorBear WIP bears/php: Add PHPMessDetectorBear Mar 6, 2017
@yash-nisar
Copy link
Member Author

Since PHPMD depends on composer for its installation, we shall wait till https://gitlab.com/coala/package_manager/issues/34 gets merged.

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

5 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!

@SanketDG
Copy link
Member

@yash-nisar this can be worked on now?

@yash-nisar
Copy link
Member Author

yash-nisar commented Apr 12, 2017

Since I had included it in my GSoC proposal, the plan was to do it during GSoC if at all I get selected but I'll complete this asap once I'm done with my exams. :)

@yash-nisar yash-nisar force-pushed the iss#22 branch 3 times, most recently from e0d4b9c to 13cb512 Compare April 16, 2017 14:11
curl -fsSL -o phpmd.phar http://static.phpmd.org/php/latest/phpmd.phar
sudo chmod +x phpmd.phar
sudo mv phpmd.phar ~/phpmd/phpmd
fi
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:

  • No newline at EOF.

SpaceConsistencyBear, severity NORMAL, section ci.

The issue can be fixed by applying the following patch:

--- a/.ci/deps.sh
+++ b/.ci/deps.sh
@@ -184,4 +184,4 @@
   curl -fsSL -o phpmd.phar http://static.phpmd.org/php/latest/phpmd.phar
   sudo chmod +x phpmd.phar
   sudo mv phpmd.phar ~/phpmd/phpmd
-fi+fi

@yash-nisar yash-nisar force-pushed the iss#22 branch 2 times, most recently from f7cd2bf to 95f0b60 Compare April 16, 2017 14:46
@yash-nisar
Copy link
Member Author

Aaaanddddd the tests pass again ! 🎉

@yash-nisar
Copy link
Member Author

@jayvdb @Makman2 Should I wait till we have ComposerRequirement or should I enhance this PR ?

@yash-nisar yash-nisar changed the title WIP bears/php: Add PHPMessDetectorBear bears/php: Add PHPMessDetectorBear Apr 17, 2017
Copy link
Member

@jayvdb jayvdb left a comment

Choose a reason for hiding this comment

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

No need to wait for ComposerRequirement for this; it is one line that can be added at the end of the release cycle.


@staticmethod
def create_arguments(filename, file, config_file,
phpmd_ruleset: typed_list(str)=''):
Copy link
Member

Choose a reason for hiding this comment

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

this is wrong on many levels.

never use an empty string as a default unless that is a valid value. Use None.

and this is supposed to be a list, but you give a string as a default.

But, since it is a mandatory argument, simply do not provide a default and coala will do the work for you.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jayvdb Do you mean something like phpmd_ruleset: list ?

Available rulesets: cleancode, codesize, controversial, design,
naming, unusedcode.
"""
if phpmd_ruleset == '': # pragma: no cover
Copy link
Member

Choose a reason for hiding this comment

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

this will all be removed due to the above comment, ...

but never use # pragma: no cover to avoid writing additional tests. It should be used only for insanely difficulty chunks of code to reach with a test.

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed it. :)

}
}
}
?>
Copy link
Member

Choose a reason for hiding this comment

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

no eol at eof

Copy link
Member Author

Choose a reason for hiding this comment

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

We should make coala do that. :P

Copy link
Member

Choose a reason for hiding this comment

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

why does coala not pick this up?

Copy link
Member Author

Choose a reason for hiding this comment

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

@SanketDG coala doesn't pick this up even when I run it individually on this file.
Command: coala -V -b SpaceConsistencyBear -f s.php --flush-cache
s.php is the following file without a newline at eof.

Copy link
Member

Choose a reason for hiding this comment

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

}
}
}
?>
Copy link
Member

Choose a reason for hiding this comment

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

no eol at eof

Copy link
Member Author

Choose a reason for hiding this comment

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

Rectified. :)

@@ -0,0 +1,39 @@
from coalib.settings.Setting import typed_list
Copy link
Collaborator

Choose a reason for hiding this comment

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

This file contains unused source code.

PyUnusedCodeBear, severity NORMAL, section flakes.

The issue can be fixed by applying the following patch:

--- a/bears/php/PHPMessDetectorBear.py
+++ b/bears/php/PHPMessDetectorBear.py
@@ -1,4 +1,3 @@
-from coalib.settings.Setting import typed_list
 from coalib.bearlib.abstractions.Linter import linter
 from dependency_management.requirements.DistributionRequirement import \
     DistributionRequirement

@yash-nisar yash-nisar force-pushed the iss#22 branch 2 times, most recently from ff7cc1d to cd2f965 Compare April 18, 2017 08:45
@@ -0,0 +1,39 @@
from coalib.settings.Setting import typed_list
Copy link
Collaborator

Choose a reason for hiding this comment

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

This file contains unused source code.

PyUnusedCodeBear, severity NORMAL, section flakes.

The issue can be fixed by applying the following patch:

--- a/bears/php/PHPMessDetectorBear.py
+++ b/bears/php/PHPMessDetectorBear.py
@@ -1,4 +1,3 @@
-from coalib.settings.Setting import typed_list
 from coalib.bearlib.abstractions.Linter import linter
 from dependency_management.requirements.DistributionRequirement import \
     DistributionRequirement

Copy link
Member Author

Choose a reason for hiding this comment

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

Why do I forget to run coala ?!!

phpmd_ruleset: list):
"""
:param phpmd_ruleset:
A ruleset filename or a comma-separated string of rulesetfilenames.
Copy link
Member

Choose a reason for hiding this comment

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

"rulesetfilenames." - what is that?

-> comma-separated string of ruleset filenames and built-in ruleset names.

But really we should be using config_file for the filename, which would mean that this ruleset variable is used only for the built-in ruleset names, which makes it more easily verified. (and the _config variable could then be managed by coala/coala#4072)

This comes at a slight loss of functionality, as only one config file could be used (It looks like phpmd supports multiple) , but presumably XInclude can be used to avoid that limitation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay so IIUC, we mention the rulesets in our config_file ? @jayvdb

Copy link
Member Author

Choose a reason for hiding this comment

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

phpmd -h gives this:

 yash  ~  phpmd -h
Mandatory arguments:
1) A php source code filename or directory. Can be a comma-separated string
2) A report format
3) A ruleset filename or a comma-separated string of rulesetfilenames

So, this is where I picked the description from 😛

Copy link
Member Author

Choose a reason for hiding this comment

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

Is this workaround satisfactory ? @jayvdb

@staticmethod
    def create_arguments(filename, file, config_file,
                         phpmd_ruleset: list):
        """
        :param phpmd_ruleset:
            A ruleset filename or a comma-separated string of rulesetfilenames.
            Available rulesets: cleancode, codesize, controversial, design,
            naming, unusedcode.
        """
        config_file = phpmd_ruleset
        return filename, 'text', ','.join(config_file)

Copy link
Member

Choose a reason for hiding this comment

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

So in any way it's a list, right? I think it just suffices to say

A list of rulesets to use for analysis. Available rulesets: ...

Copy link
Member

Choose a reason for hiding this comment

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

And then I would call this setting phpmd_rulesets (note the s)

Copy link
Member Author

Choose a reason for hiding this comment

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

Awesome. 🎉


@staticmethod
def create_arguments(filename, file, config_file,
phpmd_ruleset: list):
Copy link
Member

Choose a reason for hiding this comment

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

list(str) !

Copy link
Member Author

Choose a reason for hiding this comment

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

Getting a type error, TypeError: 'type' object is not iterable.

Copy link
Member

Choose a reason for hiding this comment

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

--> typed_list(str) is the right one ;)

@@ -0,0 +1,38 @@
from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.DistributionRequirement import \
DistributionRequirement
Copy link
Member

Choose a reason for hiding this comment

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

braces instead of \ please ;)


@linter(executable='phpmd',
output_format='regex',
output_regex=r'(?P<filename>.*):(?P<line>\d+)(?P<message>.*)')
Copy link
Member

Choose a reason for hiding this comment

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

isn't there some space between line and message? Could you post me an output example of phpmd?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, just a minute. :)

Copy link
Member Author

Choose a reason for hiding this comment

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

[WARNING][21:11:32] Implicit 'Default' section inheritance is deprecated. It will be removed soon. To silence this warning remove settings in the 'Default' section from your coafile. You can use dots to specify inheritance: the section 'all.python' will inherit all settings from 'all'.
Please enter a value for the setting "phpmd_ruleset" (A ruleset filename or a comma-separated string of rulesetfilenames. Available rulesets: cleancode, codesize, controversial, design, naming, unusedcode.) needed by PHPMessDetectorBear for section "cli": 
cleancode
[DEBUG][21:11:37] Platform Linux -- Python 3.5.2, coalib 0.11.0.dev99999999999999
[DEBUG][21:11:37] The file cache was successfully flushed.
Executing section cli...
[DEBUG][21:11:37] Files that will be checked:
/home/yash/s.php
[DEBUG][21:11:37] 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][21:11:37] Running bear PHPMessDetectorBear...
[DEBUG][21:11:37] Running 'phpmd /home/yash/s.php text cleancode'

s.php
|   8| ········}·else·{
|    | [NORMAL] PHPMessDetectorBear:
|    | The method bar uses an else expression. Else is never necessary and you can simplify the code to work without else.
|    | *0: Do nothing
|    |  1: Open file(s)
|    |  2: Add ignore comment
|    | Enter number (Ctrl-D to exit): 0```

Copy link
Member Author

Choose a reason for hiding this comment

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

This is the output of the phpmd tool:

 yash  (e) venv  ~  phpmd /home/yash/s.php text cleancode

/home/yash/s.php:8	The method bar uses an else expression. Else is never necessary and you can simplify the code to work without else.

Copy link
Member Author

Choose a reason for hiding this comment

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

Will update my regex to (?P<filename>.*):(?P<line>\d+)\s*(?P<message>.*), note that I've added an \s* to capture spaces. :)

Copy link
Member

Choose a reason for hiding this comment

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

Interesting though, looks like coala does strip the message. And now I recall that from somewhen... but still: better explicit than implicit 👍

Available rulesets: cleancode, codesize, controversial, design,
naming, unusedcode.
"""
return filename, 'text', ','.join(phpmd_ruleset)
Copy link
Member

Choose a reason for hiding this comment

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

what's text?

Copy link
Member Author

Choose a reason for hiding this comment

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

An available report format. It can be xml, text or html. :)


@staticmethod
def create_arguments(filename, file, config_file,
phpmd_ruleset: list):
Copy link
Member

@Makman2 Makman2 Apr 18, 2017

Choose a reason for hiding this comment

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

Maybe we want to make this setting optional by kicking in all available rulesets by default? Not sure...

Copy link
Member Author

Choose a reason for hiding this comment

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

So IIUC, do we provide all the rulesets as default like phpmd_ruleset: list=[rule1, rule2, ...] ?

Copy link
Member

Choose a reason for hiding this comment

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

yeah exactly, though I'm really not sure if we wanna do that...

Copy link
Member

Choose a reason for hiding this comment

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

What are the current defaults of phpmd?

Copy link
Member Author

Choose a reason for hiding this comment

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

The ruleset is a mandatory argument, so phpmd s.php text yields nothing whereas phpmd s.php text cleancode yields results.

Copy link
Member

Choose a reason for hiding this comment

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

okay let's keep it mandatory for now^^

@yash-nisar
Copy link
Member Author

yash-nisar commented Apr 22, 2017

Reviewers v1.0


@linter(executable='phpmd',
output_format='regex',
output_regex=r'(?P<filename>.*):(?P<line>\d+)\s*(?P<message>.*)')
Copy link
Member

Choose a reason for hiding this comment

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

do not capture filename, as it is useless information that the bear already has. (please check our docs dont recommend capturing filename)

and don't use .* unless there is no other option.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is everything else good ?

@jayvdb
Copy link
Member

jayvdb commented Apr 22, 2017

ack bb46ca3

@jayvdb
Copy link
Member

jayvdb commented Apr 22, 2017

@rultor merge

@rultor
Copy link

rultor commented Apr 22, 2017

@rultor merge

@jayvdb OK, I'll try to merge now. You can check the progress of the merge here

@rultor rultor merged commit bb46ca3 into coala:master Apr 22, 2017
@rultor
Copy link

rultor commented Apr 22, 2017

@rultor merge

@jayvdb Done! FYI, the full log is here (took me 1min)

@yash-nisar yash-nisar deleted the iss#22 branch May 8, 2017 20:51
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.

Detect messy code in PHP
7 participants