Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
11 changed files
with
262 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from coalib.bearlib.abstractions.Linter import linter | ||
from coalib.settings.Setting import typed_list | ||
from dependency_management.requirements.DistributionRequirement import ( | ||
DistributionRequirement) | ||
|
||
|
||
@linter(executable='phpmd', | ||
output_format='regex', | ||
output_regex=r':(?P<line>\d+)\s*(?P<message>.*)') | ||
class PHPMessDetectorBear: | ||
""" | ||
The bear takes a given PHP source code base and looks for several | ||
potential problems within that source. These problems can be things like: | ||
- Possible bugs | ||
- Suboptimal code | ||
- Overcomplicated expressions | ||
- Unused parameters, methods, properties | ||
""" | ||
|
||
LANGUAGES = {'PHP'} | ||
REQUIREMENTS = {DistributionRequirement(apt_get='phpmd')} | ||
AUTHORS = {'The coala developers'} | ||
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'} | ||
LICENSE = 'AGPL-3.0' | ||
CAN_DETECT = {'Formatting', 'Complexity', 'Unused Code', 'Redundancy', | ||
'Variable Misuse'} | ||
SEE_MORE = 'https://phpmd.org/about.html' | ||
|
||
@staticmethod | ||
def create_arguments(filename, file, config_file, | ||
phpmd_rulesets: typed_list(str)): | ||
""" | ||
:param phpmd_rulesets: | ||
A list of rulesets to use for analysis. | ||
Available rulesets: cleancode, codesize, controversial, design, | ||
naming, unusedcode. | ||
""" | ||
return filename, 'text', ','.join(phpmd_rulesets) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import os | ||
from queue import Queue | ||
|
||
from bears.php.PHPMessDetectorBear import PHPMessDetectorBear | ||
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper | ||
from coalib.testing.BearTestHelper import generate_skip_decorator | ||
from coalib.results.Result import Result | ||
from coalib.settings.Section import Section | ||
from coalib.settings.Setting import Setting | ||
|
||
|
||
def get_testfile_path(name): | ||
return os.path.join(os.path.dirname(__file__), | ||
'phpmessdetector_test_files', | ||
name) | ||
|
||
|
||
def load_testfile(name): | ||
return open(get_testfile_path(name)).readlines() | ||
|
||
|
||
@generate_skip_decorator(PHPMessDetectorBear) | ||
class PHPMessDetectorBearTest(LocalBearTestHelper): | ||
|
||
def setUp(self): | ||
self.section = Section('name') | ||
self.uut = PHPMessDetectorBear(self.section, Queue()) | ||
|
||
def test_cleancode_violation(self): | ||
file_contents = load_testfile('cleancode_violation.php') | ||
self.section.append(Setting('phpmd_rulesets', 'cleancode')) | ||
self.check_results( | ||
self.uut, | ||
file_contents, | ||
[Result.from_values('PHPMessDetectorBear', | ||
'The method bar uses an else expression. Else ' | ||
'is never necessary and you can simplify the ' | ||
'code to work without else.', | ||
file=get_testfile_path( | ||
'cleancode_violation.php'), | ||
line=8)], | ||
filename=get_testfile_path('cleancode_violation.php')) | ||
|
||
def test_codesize_violation(self): | ||
file_contents = load_testfile('codesize_violation.php') | ||
self.section.append(Setting('phpmd_rulesets', 'codesize')) | ||
self.check_results( | ||
self.uut, | ||
file_contents, | ||
[Result.from_values('PHPMessDetectorBear', | ||
'The method example() has a Cyclomatic ' | ||
'Complexity of 11. The configured cyclomatic ' | ||
'complexity threshold is 10.', | ||
file=get_testfile_path( | ||
'codesize_violation.php'), | ||
line=4)], | ||
filename=get_testfile_path('codesize_violation.php')) | ||
|
||
def test_design_violation(self): | ||
file_contents = load_testfile('design_violation.php') | ||
self.section.append(Setting('phpmd_rulesets', 'design')) | ||
self.check_results( | ||
self.uut, | ||
file_contents, | ||
[Result.from_values('PHPMessDetectorBear', | ||
'The method bar() contains an exit ' | ||
'expression.', | ||
file=get_testfile_path( | ||
'design_violation.php'), | ||
line=5), | ||
Result.from_values('PHPMessDetectorBear', | ||
'The method foo() contains an eval ' | ||
'expression.', | ||
file=get_testfile_path( | ||
'design_violation.php'), | ||
line=12)], | ||
filename=get_testfile_path('design_violation.php')) | ||
|
||
def test_naming_violation(self): | ||
file_contents = load_testfile('naming_violation.php') | ||
self.section.append(Setting('phpmd_rulesets', 'naming')) | ||
self.check_results( | ||
self.uut, | ||
file_contents, | ||
[Result.from_values('PHPMessDetectorBear', | ||
'Avoid variables with short names like $q. ' | ||
'Configured minimum length is 3.', | ||
file=get_testfile_path( | ||
'naming_violation.php'), | ||
line=3), | ||
Result.from_values('PHPMessDetectorBear', | ||
'Avoid variables with short names like $as. ' | ||
'Configured minimum length is 3.', | ||
file=get_testfile_path( | ||
'naming_violation.php'), | ||
line=4), | ||
Result.from_values('PHPMessDetectorBear', | ||
'Avoid variables with short names like $r. ' | ||
'Configured minimum length is 3.', | ||
file=get_testfile_path( | ||
'naming_violation.php'), | ||
line=5)], | ||
filename=get_testfile_path('naming_violation.php')) | ||
|
||
def test_unusedcode_violation(self): | ||
file_contents = load_testfile('unusedcode_violation.php') | ||
self.section.append(Setting('phpmd_rulesets', 'unusedcode')) | ||
self.check_results( | ||
self.uut, | ||
file_contents, | ||
[Result.from_values('PHPMessDetectorBear', | ||
'Avoid unused local variables such as \'$i\'.', | ||
file=get_testfile_path( | ||
'unusedcode_violation.php'), | ||
line=5), | ||
Result.from_values('PHPMessDetectorBear', | ||
'Avoid unused private methods such ' | ||
'as \'foo\'.', | ||
file=get_testfile_path( | ||
'unusedcode_violation.php'), | ||
line=9)], | ||
filename=get_testfile_path('unusedcode_violation.php')) |
Empty file.
13 changes: 13 additions & 0 deletions
13
tests/php/phpmessdetector_test_files/cleancode_violation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<? | ||
class Foo | ||
{ | ||
public function bar($flag) | ||
{ | ||
if ($flag) { | ||
// one branch | ||
} else { | ||
// another branch | ||
} | ||
} | ||
} | ||
?> |
39 changes: 39 additions & 0 deletions
39
tests/php/phpmessdetector_test_files/codesize_violation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<? | ||
// Cyclomatic Complexity = 11 | ||
class Foo { | ||
public function example() { | ||
if ($a == $b) { | ||
if ($a1 == $b1) { | ||
fiddle(); | ||
} elseif ($a2 == $b2) { | ||
fiddle(); | ||
} else { | ||
fiddle(); | ||
} | ||
} elseif ($c == $d) { | ||
while ($c == $d) { | ||
fiddle(); | ||
} | ||
} elseif ($e == $f) { | ||
for ($n = 0; $n < $h; $n++) { | ||
fiddle(); | ||
} | ||
} else { | ||
switch ($z) { | ||
case 1: | ||
fiddle(); | ||
break; | ||
case 2: | ||
fiddle(); | ||
break; | ||
case 3: | ||
fiddle(); | ||
break; | ||
default: | ||
fiddle(); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<? | ||
class Foo { | ||
public function bar($param) { | ||
if ($param === 42) { | ||
exit(23); | ||
} | ||
} | ||
} | ||
class Bar { | ||
public function foo($param) { | ||
if ($param === 42) { | ||
eval('$param = 23;'); | ||
} | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<? | ||
class Something { | ||
private $q = 15; // VIOLATION - Field | ||
public static function main( array $as ) { // VIOLATION - Formal | ||
$r = 20 + $this->q; // VIOLATION - Local | ||
for (int $i = 0; $i < 10; $i++) { // Not a Violation (inside FOR) | ||
$r += $this->q; | ||
} | ||
} | ||
} | ||
?> |
11 changes: 11 additions & 0 deletions
11
tests/php/phpmessdetector_test_files/unusedcode_violation.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<? | ||
class Foo { | ||
public function doSomething() | ||
{ | ||
$i = 5; // Unused | ||
} | ||
} | ||
class Something { | ||
private function foo() {} // unused | ||
} | ||
?> |