Skip to content

Commit

Permalink
Merge pull request #14 from bmitch/bmitchell-#7
Browse files Browse the repository at this point in the history
Fixes #7
  • Loading branch information
bmitch committed Jun 15, 2017
2 parents b826d9b + 833b281 commit ea50bdd
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public function assess($filePath): int
$this->countTheElseIfStatements($contents);
$this->countTheWhileLoops($contents);
$this->countTheForLoops($contents);

$this->countTheCaseStatements($contents);
$this->countTheTernaryOperators($contents);
$this->countTheLogicalAnds($contents);
$this->countTheLogicalOrs($contents);

if ($this->score == 0) {
$this->score = 1;
Expand All @@ -36,7 +38,7 @@ public function assess($filePath): int

/**
* Count how many methods there are.
* @param string $contents Path and filename.
* @param string $contents File contents.
* @return void
*/
protected function countTheMethods($contents)
Expand All @@ -49,7 +51,7 @@ protected function countTheMethods($contents)

/**
* Count how many if statements there are.
* @param string $contents Path and filename.
* @param string $contents File contents.
* @return void
*/
protected function countTheIfStatements($contents)
Expand All @@ -59,7 +61,7 @@ protected function countTheIfStatements($contents)

/**
* Count how many else if statements there are.
* @param string $contents Path and filename.
* @param string $contents File contents.
* @return void
*/
protected function countTheElseIfStatements($contents)
Expand All @@ -69,7 +71,7 @@ protected function countTheElseIfStatements($contents)

/**
* Count how many while loops there are.
* @param string $contents Path and filename.
* @param string $contents File contents.
* @return void
*/
protected function countTheWhileLoops($contents)
Expand All @@ -79,25 +81,54 @@ protected function countTheWhileLoops($contents)

/**
* Count how many for loops there are.
* @param string $contents Path and filename.
* @param string $contents File contents.
* @return void
*/
protected function countTheForLoops($contents)
{
// dd($this->howmAnyPatternMatches("/[ ]for(each){0,1}[ ]{0,}\(/", $contents));
$this->score += $this->howmAnyPatternMatches("/[ ]for(each){0,1}[ ]{0,}\(/", $contents);
}

/**
* Count how many case statements there are.
* @param string $contents Path and filename.
* @param string $contents File contents.
* @return void
*/
protected function countTheCaseStatements($contents)
{
$this->score += $this->howmAnyPatternMatches("/[ ]case[ ]{1}(.*)\:/", $contents);
}

/**
* Count how many ternary operators there are.
* @param string $contents File contents.
* @return void
*/
protected function countTheTernaryOperators($contents)
{
$this->score += $this->howmAnyPatternMatches("/[ ]\?.*:.*;/", $contents);
}

/**
* Count how many '&&' there are.
* @param string $contents File contents.
* @return void
*/
protected function countTheLogicalAnds($contents)
{
$this->score += $this->howmAnyPatternMatches("/[ ]&&[ ]/", $contents);
}

/**
* Count how many '||' there are.
* @param string $contents File contents.
* @return void
*/
protected function countTheLogicalOrs($contents)
{
$this->score += $this->howmAnyPatternMatches("/[ ]\|\|[ ]/", $contents);
}

/**
* For the given $pattern on $string, how many matches are returned?
* @param string $pattern Regex pattern.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class ClassWithLogicalAnd
{
public function foobar()
{
return ($a == $b && $c == $d);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class ClassWithLogicalOr
{
public function foobar()
{
return ($a == $b || $c == $d);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

class ClassWithTernary
{
public function foobar()
{
$foo == 'bar' ? $baz++ : $zug;
}
}
125 changes: 0 additions & 125 deletions tests/Unit/Assessors/CyclomaticComplexity/Assets/foobar.inc

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,23 @@ public function this_class_with_many_methods_and_many_branches_should_have_a_com
$this->assertEquals(11, $this->assess('tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithManyMethodsAndLotsOfBranches.inc'));
}


/** @test */
public function a_class_with_a_ternary_opererator_should_return_two()
{
$this->assertEquals(2, $this->assess('tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithTernary.inc'));
}

/** @test */
public function a_class_with_a_logical_and_should_return_two()
{
$this->assertEquals(2, $this->assess('tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithLogicalAnd.inc'));
}

/** @test */
public function a_class_with_a_logical_or_should_return_two()
{
$this->assertEquals(2, $this->assess('tests/Unit/Assessors/CyclomaticComplexity/Assets/ClassWithLogicalOr.inc'));
}

protected function assess($filename)
{
Expand Down

0 comments on commit ea50bdd

Please sign in to comment.