Skip to content

Commit

Permalink
Fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
bmitch committed Jun 11, 2017
1 parent 1ba74ad commit 8ec5629
Show file tree
Hide file tree
Showing 18 changed files with 384 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build
composer.lock
docs
vendor
coverage.xml
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ before_script:

script:
- vendor/bin/phpcs --standard=psr2 src
- vendor/bin/phpcs --standard=phpcs.xml src tests
- if [[ ${TRAVIS_PHP_VERSION:0:3} == "7.0" ]]; then vendor/bin/phpcs --standard=phpcs.xml src --ignore=tests/Sniffs; fi
- vendor/bin/phpcs --standard=codor.xml src -spn
- vendor/bin/phpunit --debug --coverage-clover=coverage.xml
- vendor/bin/phpmd src text codesize,unusedcode,naming
Expand All @@ -20,4 +20,4 @@ script:


after_success:
# - bash <(curl -s https://codecov.io/bash)
- bash <(curl -s https://codecov.io/bash)
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "bmitch/ChurnPhp",
"name": "bmitch/churn-php",
"description": "Discover files in need of refactoring.",
"keywords": [
"bmitch",
"ChurnPhp"
"churn-php"
],
"homepage": "https://github.com/bmitch/ChurnPhp",
"homepage": "https://github.com/bmitch/churn-php",
"license": "MIT",
"authors": [
{
Expand All @@ -30,7 +30,7 @@
},
"autoload": {
"psr-4": {
"Codor\\": "src/Churn/"
"Churn\\": "src"
}
},
"autoload-dev": {
Expand All @@ -46,7 +46,7 @@
"vendor/bin/phpcs --standard=codor.xml src -spn",
"vendor/bin/phpunit --debug --coverage-clover=coverage.xml",
"vendor/bin/phpmd src text codesize,unusedcode,naming",
"vendor/bin/phploc src --progress",
"vendor/bin/phploc src",
"vendor/bin/phpcpd src",
"vendor/bin/phpunit"
]
Expand Down
14 changes: 0 additions & 14 deletions coverage.xml

This file was deleted.

130 changes: 130 additions & 0 deletions src/Assessors/CyclomaticComplexity/CyclomaticComplexityAssessor.php
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);
}
}
8 changes: 0 additions & 8 deletions src/Class.php

This file was deleted.

10 changes: 10 additions & 0 deletions tests/BaseTestCase.php
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
{

}
14 changes: 0 additions & 14 deletions tests/ExampleTest.php

This file was deleted.

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();
}
}
}
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;
}
}
}
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();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class ClassWithOneEmptyMethod
{
public function foobar()
{

}
}
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;
}
}
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;
}
}
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;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

class ClassWithWhileLoop
{
public function foobar()
{
while ($c == $d) {
foobar();
}
}
}
Loading

0 comments on commit 8ec5629

Please sign in to comment.