-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
384 additions
and
43 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ build | |
composer.lock | ||
docs | ||
vendor | ||
coverage.xml |
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 was deleted.
Oops, something went wrong.
130 changes: 130 additions & 0 deletions
130
src/Assessors/CyclomaticComplexity/CyclomaticComplexityAssessor.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,130 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Churn\Assessors\CyclomaticComplexity; | ||
|
||
class CyclomaticComplexityAssessor | ||
{ | ||
/** | ||
* The total cyclomatic complexity score. | ||
* @var integer. | ||
*/ | ||
protected $score; | ||
|
||
/** | ||
* Class constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->score = 0; | ||
} | ||
|
||
/** | ||
* Asses the files cyclomatic complexity. | ||
* @param string $filePath Path and file name. | ||
* @return integer | ||
*/ | ||
public function assess($filePath): int | ||
{ | ||
$contents = $this->getFileContents($filePath); | ||
|
||
$this->countTheMethods($contents); | ||
$this->countTheIfStatements($contents); | ||
$this->countTheElseIfStatements($contents); | ||
$this->countTheWhileLoops($contents); | ||
$this->countTheForLoops($contents); | ||
$this->countTheCaseStatements($contents); | ||
|
||
if ($this->score == 0) { | ||
$this->score = 1; | ||
} | ||
return $this->score; | ||
} | ||
|
||
/** | ||
* Count how many methods there are. | ||
* @param string $contents Path and filename. | ||
* @return void | ||
*/ | ||
protected function countTheMethods($contents) | ||
{ | ||
preg_match("/[ ]function[ ]/", $contents, $matches); | ||
if (isset($matches[0])) { | ||
$this->score ++; | ||
} | ||
} | ||
|
||
/** | ||
* Count how many if statements there are. | ||
* @param string $contents Path and filename. | ||
* @return void | ||
*/ | ||
protected function countTheIfStatements($contents) | ||
{ | ||
$this->score += $this->howmAnyPatternMatches("/[ ]if[ ]/", $contents); | ||
} | ||
|
||
/** | ||
* Count how many else if statements there are. | ||
* @param string $contents Path and filename. | ||
* @return void | ||
*/ | ||
protected function countTheElseIfStatements($contents) | ||
{ | ||
$this->score += $this->howmAnyPatternMatches("/elseif/", $contents); | ||
} | ||
|
||
/** | ||
* Count how many while loops there are. | ||
* @param string $contents Path and filename. | ||
* @return void | ||
*/ | ||
protected function countTheWhileLoops($contents) | ||
{ | ||
$this->score += $this->howmAnyPatternMatches("/while/", $contents); | ||
} | ||
|
||
/** | ||
* Count how many for loops there are. | ||
* @param string $contents Path and filename. | ||
* @return void | ||
*/ | ||
protected function countTheForLoops($contents) | ||
{ | ||
$this->score += $this->howmAnyPatternMatches("/for/", $contents); | ||
} | ||
|
||
/** | ||
* Count how many case statements there are. | ||
* @param string $contents Path and filename. | ||
* @return void | ||
*/ | ||
protected function countTheCaseStatements($contents) | ||
{ | ||
$this->score += $this->howmAnyPatternMatches("/case/", $contents); | ||
} | ||
|
||
/** | ||
* For the given $pattern on $string, how many matches are returned? | ||
* @param string $pattern Regex pattern. | ||
* @param string $string Any string. | ||
* @return integer | ||
*/ | ||
protected function howManyPatternMatches($pattern, $string): int | ||
{ | ||
preg_match_all($pattern, $string, $matches); | ||
if (isset($matches[0])) { | ||
return count($matches[0]); | ||
} | ||
return 0; | ||
} | ||
|
||
/** | ||
* Return the contents of the provided file at $filePath. | ||
* @param string $filePath Path and filename. | ||
* @return string | ||
*/ | ||
protected function getFileContents($filePath): string | ||
{ | ||
return file_get_contents($filePath); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Churn\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
abstract class BaseTestCase extends TestCase | ||
{ | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithForLoop.inc
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 @@ | ||
<?php | ||
|
||
class ClassWithWhileLoop | ||
{ | ||
public function foobar() | ||
{ | ||
for ($n = 0; $n < $h; $n++) { | ||
foobar(); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithIfElseIf.inc
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 @@ | ||
<?php | ||
|
||
class ClassWithIfElseIf | ||
{ | ||
public function foobar() | ||
{ | ||
if ($true) { | ||
return true; | ||
} elseif($false) { | ||
return false; | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithManyMethodsAndLotsOfBranches.inc
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,60 @@ | ||
<?php | ||
|
||
class ClassWithWhileLoop | ||
{ | ||
public function foo() | ||
{ | ||
for ($n = 0; $n < $h; $n++) { | ||
foobar(); | ||
} | ||
} | ||
|
||
public function bar() | ||
{ | ||
if ($true) { | ||
return true; | ||
} elseif($false) { | ||
return false; | ||
} | ||
|
||
if ($true) { | ||
if ($true) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function baz() | ||
{ | ||
if ($true) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function uggh() | ||
{ | ||
switch ($z) { | ||
case 1: | ||
foobar(); | ||
break; | ||
case 2: | ||
foobar(); | ||
break; | ||
case 3: | ||
foobar(); | ||
break; | ||
default: | ||
foobar(); | ||
break; | ||
} | ||
|
||
while ($c == $d) { | ||
foobar(); | ||
} | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithOneEmptyMethod.inc
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,9 @@ | ||
<?php | ||
|
||
class ClassWithOneEmptyMethod | ||
{ | ||
public function foobar() | ||
{ | ||
|
||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithOneMethodWithNestedIf.inc
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,15 @@ | ||
<?php | ||
|
||
class ClassWithOneMethodWithNestedIf | ||
{ | ||
public function foobar() | ||
{ | ||
if ($true) { | ||
if ($true) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithOneMethodWithOneIf.inc
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 @@ | ||
<?php | ||
|
||
class ClassWithOneMethodWithOneIf | ||
{ | ||
public function foobar() | ||
{ | ||
if ($true) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithSwitchStatement.inc
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,23 @@ | ||
<?php | ||
|
||
class ClassWithWhileLoop | ||
{ | ||
public function foobar() | ||
{ | ||
switch ($z) { | ||
case 1: | ||
foobar(); | ||
break; | ||
case 2: | ||
foobar(); | ||
break; | ||
case 3: | ||
foobar(); | ||
break; | ||
default: | ||
foobar(); | ||
break; | ||
} | ||
|
||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithWhileLoop.inc
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 @@ | ||
<?php | ||
|
||
class ClassWithWhileLoop | ||
{ | ||
public function foobar() | ||
{ | ||
while ($c == $d) { | ||
foobar(); | ||
} | ||
} | ||
} |
Oops, something went wrong.