diff --git a/src/Hal/Application/Command/Job/Analyze/FileAnalyzer.php b/src/Hal/Application/Command/Job/Analyze/FileAnalyzer.php index 56305111..44cf12b0 100644 --- a/src/Hal/Application/Command/Job/Analyze/FileAnalyzer.php +++ b/src/Hal/Application/Command/Job/Analyze/FileAnalyzer.php @@ -10,6 +10,7 @@ namespace Hal\Application\Command\Job\Analyze; use Hal\Component\OOP\Extractor\ClassMap; use Hal\Component\OOP\Extractor\Extractor; +use Hal\Component\Token\TokenCollection; use Hal\Metrics\Complexity\Component\McCabe\McCabe; use Hal\Metrics\Complexity\Component\Myer\Myer; use Hal\Metrics\Complexity\Text\Halstead\Halstead; @@ -111,19 +112,19 @@ public function __construct( $this->classMap = $classMap; } - /** * Run analyze * - * @param $filename + * @param string $filename + * @param TokenCollection $tokens * @return \Hal\Component\Result\ResultSet */ - public function execute($filename) { + public function execute($filename, $tokens) { - $rHalstead = $this->halstead->calculate($filename); - $rLoc = $this->loc->calculate($filename); - $rMcCabe = $this->mcCabe->calculate($filename); - $rMyer = $this->myer->calculate($filename); + $rHalstead = $this->halstead->calculate($tokens); + $rLoc = $this->loc->calculate($filename, $tokens); + $rMcCabe = $this->mcCabe->calculate($tokens); + $rMyer = $this->myer->calculate($tokens); $rMaintainability = $this->maintainabilityIndex->calculate($rHalstead, $rLoc, $rMcCabe); $resultSet = new \Hal\Component\Result\ResultSet($filename); @@ -135,7 +136,7 @@ public function execute($filename) { ->setMaintainabilityIndex($rMaintainability); if($this->withOOP) { - $rOOP = $this->extractor->extract($filename); + $rOOP = $this->extractor->extract($tokens); $this->classMap->push($filename, $rOOP); $resultSet->setOOP($rOOP); } diff --git a/src/Hal/Application/Command/Job/DoAnalyze.php b/src/Hal/Application/Command/Job/DoAnalyze.php index 95f5924b..785adddf 100644 --- a/src/Hal/Application/Command/Job/DoAnalyze.php +++ b/src/Hal/Application/Command/Job/DoAnalyze.php @@ -13,7 +13,6 @@ use Hal\Application\Command\Job\Analyze\FileAnalyzer; use Hal\Application\Command\Job\Analyze\LcomAnalyzer; use Hal\Component\Compatibility\PhpCompatibility; -use Hal\Component\Cache\CacheMemory; use Hal\Component\File\Finder; use Hal\Component\File\SyntaxChecker; use Hal\Component\OOP\Extractor\ClassMap; @@ -101,18 +100,18 @@ public function execute(ResultCollection $collection, ResultCollection $aggregat // tools $classMap = new ClassMap(); - $tokenizer = new Tokenizer(new CacheMemory()); + $tokenizer = new Tokenizer(); $syntaxChecker = new SyntaxChecker(); $fileAnalyzer = new FileAnalyzer( $this->output , $this->withOOP - , new Extractor($tokenizer) - , new \Hal\Metrics\Complexity\Text\Halstead\Halstead($tokenizer, new \Hal\Component\Token\TokenType()) - , new \Hal\Metrics\Complexity\Text\Length\Loc($tokenizer) + , new Extractor() + , new \Hal\Metrics\Complexity\Text\Halstead\Halstead(new \Hal\Component\Token\TokenType()) + , new \Hal\Metrics\Complexity\Text\Length\Loc() , new \Hal\Metrics\Design\Component\MaintainabilityIndex\MaintainabilityIndex() - , new \Hal\Metrics\Complexity\Component\McCabe\McCabe($tokenizer) - , new \Hal\Metrics\Complexity\Component\Myer\Myer($tokenizer) + , new \Hal\Metrics\Complexity\Component\McCabe\McCabe() + , new \Hal\Metrics\Complexity\Component\Myer\Myer() , $classMap ); @@ -129,7 +128,8 @@ public function execute(ResultCollection $collection, ResultCollection $aggregat // Analyze try { - $resultSet = $fileAnalyzer->execute($filename); + $tokens = $tokenizer->tokenize($filename); + $resultSet = $fileAnalyzer->execute($filename, $tokens); } catch(NoTokenizableException $e) { $this->output->writeln(sprintf("file %s has been skipped: \n%s", $filename, $e->getMessage())); unset($files[$k]); diff --git a/src/Hal/Component/Cache/Cache.php b/src/Hal/Component/Cache/Cache.php index ac0eda78..0ffc6a35 100644 --- a/src/Hal/Component/Cache/Cache.php +++ b/src/Hal/Component/Cache/Cache.php @@ -39,4 +39,4 @@ public function has($key); * @return mixed */ public function clear($key); -} \ No newline at end of file +} diff --git a/src/Hal/Component/Cache/CacheMemory.php b/src/Hal/Component/Cache/CacheMemory.php index 58d6865d..760864da 100644 --- a/src/Hal/Component/Cache/CacheMemory.php +++ b/src/Hal/Component/Cache/CacheMemory.php @@ -8,7 +8,6 @@ */ namespace Hal\Component\Cache; -use Hal\Component\Result\ResultCollection; /** * Cache @@ -49,4 +48,4 @@ public function has($key) { public function clear($key) { $this->data = array(); } -} \ No newline at end of file +} diff --git a/src/Hal/Component/Cache/CacheNull.php b/src/Hal/Component/Cache/CacheNull.php index e2676853..a9a694df 100644 --- a/src/Hal/Component/Cache/CacheNull.php +++ b/src/Hal/Component/Cache/CacheNull.php @@ -41,4 +41,4 @@ public function has($key) { */ public function clear($key) { } -} \ No newline at end of file +} diff --git a/src/Hal/Component/OOP/Extractor/Extractor.php b/src/Hal/Component/OOP/Extractor/Extractor.php index 14d93d5e..7dd65659 100644 --- a/src/Hal/Component/OOP/Extractor/Extractor.php +++ b/src/Hal/Component/OOP/Extractor/Extractor.php @@ -10,7 +10,7 @@ namespace Hal\Component\OOP\Extractor; use Hal\Component\OOP\Reflected\ReflectedClass\ReflectedAnonymousClass; use Hal\Component\OOP\Resolver\NameResolver; -use Hal\Component\Token\Tokenizer; +use Hal\Component\Token\TokenCollection; /** @@ -36,17 +36,11 @@ class Extractor { */ private $extractors; - /** - * @var \Hal\Component\Token\Tokenizer - */ - private $tokenizer; - /** * Constructor */ - public function __construct(Tokenizer $tokenizer) { + public function __construct() { - $this->tokenizer = $tokenizer; $this->searcher = new Searcher(); $this->result= new Result; @@ -62,15 +56,14 @@ public function __construct(Tokenizer $tokenizer) { /** * Extract infos from file * - * @param $filename + * @param TokenCollection $tokens * @return Result */ - public function extract($filename) + public function extract($tokens) { $result = new Result; - $tokens = $this->tokenizer->tokenize($filename); $nameResolver = new NameResolver(); // default current values @@ -174,4 +167,4 @@ public function extract($filename) return $result; } -}; \ No newline at end of file +}; diff --git a/src/Hal/Component/Token/Tokenizer.php b/src/Hal/Component/Token/Tokenizer.php index e82fe8c9..7b2e7335 100644 --- a/src/Hal/Component/Token/Tokenizer.php +++ b/src/Hal/Component/Token/Tokenizer.php @@ -8,8 +8,6 @@ */ namespace Hal\Component\Token; -use Hal\Component\Cache\Cache; -use Hal\Component\Cache\CacheNull; /** * Tokenize file @@ -18,21 +16,6 @@ */ class Tokenizer { - private $cache; - - /** - * Tokenizer constructor. - * @param $cache - */ - public function __construct(Cache $cache = null) - { - if(null == $cache) { - $cache = new CacheNull(); - } - $this->cache = $cache; - } - - /** * Tokenize file * @@ -41,19 +24,14 @@ public function __construct(Cache $cache = null) */ public function tokenize($filename) { - if($this->cache->has($filename)) { - return new TokenCollection($this->cache->get($filename)); - } - $size = filesize($filename); $limit = 102400; // around 100 Ko - if($size > $limit) { + if ($size > $limit) { $tokens = $this->tokenizeLargeFile($filename); } else { $tokens = token_get_all($this->cleanup(file_get_contents($filename))); } - $this->cache->set($filename, $tokens); return new TokenCollection($tokens); } @@ -74,7 +52,7 @@ protected function tokenizeLargeFile($filename) { EOT; $output = shell_exec('php -r \''.$code.'\''); $tokens = unserialize($output); - if(false === $tokens) { + if (false === $tokens) { throw new NoTokenizableException(sprintf('Cannot tokenize "%s". This file is probably too big. Please try to increase memory_limit', $filename)); } return $tokens; diff --git a/src/Hal/Metrics/Complexity/Component/McCabe/McCabe.php b/src/Hal/Metrics/Complexity/Component/McCabe/McCabe.php index 741384c2..3bdd317e 100644 --- a/src/Hal/Metrics/Complexity/Component/McCabe/McCabe.php +++ b/src/Hal/Metrics/Complexity/Component/McCabe/McCabe.php @@ -8,7 +8,7 @@ */ namespace Hal\Metrics\Complexity\Component\McCabe; -use Hal\Component\Token\Tokenizer; +use Hal\Component\Token\TokenCollection; /** * Calculates cyclomatic complexity @@ -17,22 +17,6 @@ */ class McCabe { - /** - * Tokenizer - * - * @var \Hal\Component\Token\Tokenizer - */ - private $tokenizer; - - /** - * Constructor - * - * @param Tokenizer $tokenizer - */ - public function __construct(Tokenizer $tokenizer) { - $this->tokenizer = $tokenizer; - } - /** * Calculate cyclomatic complexity number * @@ -46,14 +30,12 @@ public function __construct(Tokenizer $tokenizer) { * * 2. CC = Number of each decision point * - * @param string $filename + * @param TokenCollection $tokens * @return Result */ - public function calculate($filename) + public function calculate($tokens) { - $info = new Result; - $tokens = $this->tokenizer->tokenize($filename); $ccn = 1; // default path foreach($tokens as $token) { @@ -91,4 +73,4 @@ public function calculate($filename) $info->setCyclomaticComplexityNumber(max(1, $ccn)); return $info; } -} \ No newline at end of file +} diff --git a/src/Hal/Metrics/Complexity/Component/Myer/Myer.php b/src/Hal/Metrics/Complexity/Component/Myer/Myer.php index 9c5b7a73..14407376 100644 --- a/src/Hal/Metrics/Complexity/Component/Myer/Myer.php +++ b/src/Hal/Metrics/Complexity/Component/Myer/Myer.php @@ -9,7 +9,7 @@ namespace Hal\Metrics\Complexity\Component\Myer; -use Hal\Component\Token\Tokenizer; +use Hal\Component\Token\TokenCollection; use Hal\Metrics\Complexity\Component\McCabe\McCabe; /** @@ -19,41 +19,22 @@ */ class Myer { - /** - * Tokenizer - * - * @var \Hal\Component\Token\Tokenizer - */ - private $tokenizer; - - /** - * Constructor - * - * @param Tokenizer $tokenizer - */ - public function __construct(Tokenizer $tokenizer) { - $this->tokenizer = $tokenizer; - - } - /** * Calculates Myer's interval * * Cyclomatic complexity : Cyclomatic complexity + L * where L is the number of logical operators * - * @param string $filename + * @param TokenCollection $tokens * @return Result */ - public function calculate($filename) + public function calculate($tokens) { - $mcCabe = new McCabe($this->tokenizer); + $mcCabe = new McCabe(); $result = new Result; - $tokens = $this->tokenizer->tokenize($filename); - // Cyclomatic complexity - $cc = $mcCabe->calculate($filename); + $cc = $mcCabe->calculate($tokens); // Number of operator $L = 0; @@ -75,4 +56,4 @@ public function calculate($filename) return $result; } -} \ No newline at end of file +} diff --git a/src/Hal/Metrics/Complexity/Text/Halstead/Halstead.php b/src/Hal/Metrics/Complexity/Text/Halstead/Halstead.php index 3d0641f3..1011311f 100644 --- a/src/Hal/Metrics/Complexity/Text/Halstead/Halstead.php +++ b/src/Hal/Metrics/Complexity/Text/Halstead/Halstead.php @@ -8,6 +8,7 @@ */ namespace Hal\Metrics\Complexity\Text\Halstead; +use Hal\Component\Token\TokenCollection; use Hal\Component\Token\TokenType; @@ -46,37 +47,26 @@ class Halstead { */ private $tokenType; - /** - * Tokenizer - * - * @var \Hal\Component\Token\Tokenizer - */ - private $tokenizer; - /** * Constructor * - * @param \Hal\Component\Token\Tokenizer $tokenizer * @param \Hal\Component\Token\TokenType $tokenType */ - public function __construct(\Hal\Component\Token\Tokenizer $tokenizer, \Hal\Component\Token\TokenType $tokenType) + public function __construct(\Hal\Component\Token\TokenType $tokenType) { - $this->tokenizer = $tokenizer; $this->tokenType = $tokenType; } /** * Inventories tokens * - * @param string $filename + * @param TokenCollection $tokens * @return $this */ - private function inventory($filename) + private function inventory($tokens) { $this->operators = $this->operands = array(); - $tokens = $this->tokenizer->tokenize($filename); - foreach($tokens as $token) { if($this->tokenType->isOperator($token)) { $this->operators[] = $token; @@ -91,12 +81,12 @@ private function inventory($filename) /** * Calculate Halstead metrics * - * @param $filename + * @param TokenCollection $tokens * @return Result */ - public function calculate($filename) + public function calculate($tokens) { - $this->inventory($filename); + $this->inventory($tokens); $result = new Result; $uniqueOperators = array_map( 'unserialize', array_unique( array_map( 'serialize', $this->operators ) ) ); @@ -141,4 +131,4 @@ public function calculate($filename) return $result; } -}; \ No newline at end of file +}; diff --git a/src/Hal/Metrics/Complexity/Text/Length/Loc.php b/src/Hal/Metrics/Complexity/Text/Length/Loc.php index dce7e630..dd9799a1 100644 --- a/src/Hal/Metrics/Complexity/Text/Length/Loc.php +++ b/src/Hal/Metrics/Complexity/Text/Length/Loc.php @@ -8,7 +8,7 @@ */ namespace Hal\Metrics\Complexity\Text\Length; -use Hal\Component\Token\Tokenizer; +use Hal\Component\Token\TokenCollection; /** * Calculates McCaybe measure @@ -17,36 +17,18 @@ */ class Loc { - /** - * Tokenizer - * - * @var \Hal\Component\Token\Tokenizer - */ - private $tokenizer; - - /** - * Constructor - * - * @param Tokenizer $tokenizer - */ - public function __construct(Tokenizer $tokenizer) { - $this->tokenizer = $tokenizer; - } - /** * Calculates Lines of code * * @param string $filename + * @param TokenCollection $tokens * @return Result */ - public function calculate($filename) + public function calculate($filename, $tokens) { $info = new Result; - $tokens = $this->tokenizer->tokenize($filename); - $content = file_get_contents($filename); - $cloc = $lloc = 0; foreach($tokens as $token) { @@ -65,6 +47,7 @@ public function calculate($filename) } } + $content = file_get_contents($filename); $info ->setLoc(count(preg_split('/\r\n|\r|\n/', $content)) - 1) ->setCommentLoc($cloc) diff --git a/tests/Hal/Component/OOP/AbstractnessExtractorTest.php b/tests/Hal/Component/OOP/AbstractnessExtractorTest.php index 1a6760c0..759ef68f 100644 --- a/tests/Hal/Component/OOP/AbstractnessExtractorTest.php +++ b/tests/Hal/Component/OOP/AbstractnessExtractorTest.php @@ -17,8 +17,9 @@ class AbstractnessExtractorTest extends \PHPUnit_Framework_TestCase { */ public function testInterfacesAreFound($filename, $interfaces) { - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $found = 0; foreach($result->getClasses() as $class) { @@ -46,8 +47,9 @@ public function provideInterfaces() { */ public function testAbstractClassesAreFound($filename, $expected) { - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $found = 0; foreach($result->getClasses() as $class) { @@ -67,4 +69,4 @@ public function provideAbstractClasses() { ); } -} \ No newline at end of file +} diff --git a/tests/Hal/Component/OOP/MethodExtractorTest.php b/tests/Hal/Component/OOP/MethodExtractorTest.php index 9d3b0316..28efb90d 100644 --- a/tests/Hal/Component/OOP/MethodExtractorTest.php +++ b/tests/Hal/Component/OOP/MethodExtractorTest.php @@ -22,8 +22,9 @@ class MethodExtractorTest extends \PHPUnit_Framework_TestCase { */ public function testMethodsAreFound($filename, $expectedMethods) { - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); foreach($result->getClasses() as $index => $class) { @@ -63,8 +64,10 @@ public function testMethodsAreFound($filename, $expectedMethods) { public function testContentOfMethodIsFound() { + $filename = __DIR__.'/../../../resources/oop/f6.php'; + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract(__DIR__.'/../../../resources/oop/f6.php'); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $class = $classes[0]; $methods = $class->getMethods(); @@ -163,8 +166,10 @@ public function provideCodeForNew() { } public function testGetterAreFound() { + $filename = __DIR__.'/../../../resources/oop/f8.php'; + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract(__DIR__.'/../../../resources/oop/f8.php'); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $class = $classes[0]; $methods = $class->getMethods(); @@ -177,8 +182,10 @@ public function testGetterAreFound() { } public function testSetterAreFound() { - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract(__DIR__.'/../../../resources/oop/f8.php'); + $filename = __DIR__.'/../../../resources/oop/f8.php'; + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $class = $classes[0]; $methods = $class->getMethods(); @@ -192,8 +199,9 @@ public function testSetterAreFound() { * @dataProvider provideCodeForVisibility */ public function testVisibilityIsFound($filename, $expected) { - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $class = $classes[0]; $methods = $class->getMethods(); @@ -214,8 +222,9 @@ public function provideCodeForVisibility() { * @dataProvider provideCodeForState */ public function testStateIsFound($filename, $expected) { - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $class = $classes[0]; $methods = $class->getMethods(); @@ -238,8 +247,9 @@ public function provideCodeForState() { * @dataProvider provideCodeForCalls */ public function testCallsAReFound($filename, $expectedExternalCalls, $expectedInternalCalls) { - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(1, sizeof($classes)); $class = $classes[0]; @@ -260,8 +270,9 @@ public function provideCodeForCalls() { public function testMagicMethodsAreWellConsidered() { $filename = __DIR__.'/../../../resources/oop/magicmethods1.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $class = $classes[0]; $methods = $class->getMethods(); @@ -274,8 +285,9 @@ public function testMagicMethodsAreWellConsidered() { public function testInstanciationAndCallsOfMagicMethodsAreWellConsideredAsDependency() { $filename = __DIR__.'/../../../resources/oop/magicmethods2.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(2 , sizeof($classes)); $class = $classes[1]; @@ -292,12 +304,13 @@ public function testInstanciationAndCallsOfMagicMethodsAreWellConsideredAsDepend public function testReturningNewStaticIsWellParsed() { $filename = __DIR__.'/../../../resources/oop/php7-static-as-dep1.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(1 , sizeof($classes)); $class = $classes[0]; $this->assertEquals(array('\\My\\MyClass'), $class->getDependencies()); } -} \ No newline at end of file +} diff --git a/tests/Hal/Component/OOP/OOPExtractorTest.php b/tests/Hal/Component/OOP/OOPExtractorTest.php index 5650f606..362aa1c5 100644 --- a/tests/Hal/Component/OOP/OOPExtractorTest.php +++ b/tests/Hal/Component/OOP/OOPExtractorTest.php @@ -17,8 +17,9 @@ class OOPExtractorTest extends \PHPUnit_Framework_TestCase { */ public function testClassnameIsFound($filename, $expected) { - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $this->assertCount(sizeof($expected), $result->getClasses()); foreach($result->getClasses() as $index => $class) { @@ -39,11 +40,12 @@ public function providesForClassnames() { /** * @dataProvider providesForDependenciesWithoutAlias */ - public function testDependenciesAreGivenWithoutAlias($file, $expected) { + public function testDependenciesAreGivenWithoutAlias($filename, $expected) { + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); $result = new \Hal\Component\OOP\Extractor\Result(); - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($file); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertCount(1, $classes, 'all classes are found'); @@ -63,9 +65,10 @@ public function providesForDependenciesWithoutAlias() { } public function testCallsAreFoundAsDependencies() { - $file = __DIR__.'/../../../resources/oop/f5.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($file); + $filename = __DIR__.'/../../../resources/oop/f5.php'; + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertCount(1, $classes, 'all classes are found'); $class = $classes[0]; @@ -79,9 +82,10 @@ public function testCallsAreFoundAsDependencies() { public function testClassesThatDoesNotExtendOtherClassesShouldNotHaveAParentClass() { - $file = __DIR__.'/../../../resources/oop/f1.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($file); + $filename = __DIR__.'/../../../resources/oop/f1.php'; + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $this->assertCount(1, $result->getClasses()); $class = current($result->getClasses()); @@ -95,8 +99,9 @@ public function testInterfacesAreFound() { // only one contract $filename = __DIR__.'/../../../resources/oop/interface1.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertCount(2, $classes); $class = $classes[1]; @@ -104,20 +109,22 @@ public function testInterfacesAreFound() { // multiple contracts $filename = __DIR__.'/../../../resources/oop/interface2.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertCount(3, $classes); $class = $classes[2]; $this->assertEquals(array('\My\Contract1', '\My\Contract2'), $class->getInterfaces(), 'multiple interfaces of class are found'); } - + public function testReservedWordClassDoesNotCount() { $filename = __DIR__.'/../../../resources/oop/reserved-word-class.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(1, sizeof($classes)); $class = $classes[0]; @@ -125,4 +132,4 @@ public function testReservedWordClassDoesNotCount() } -} \ No newline at end of file +} diff --git a/tests/Hal/Component/OOP/Php7AnonymousClassExtractorTest.php b/tests/Hal/Component/OOP/Php7AnonymousClassExtractorTest.php index b351dbd4..563b76eb 100644 --- a/tests/Hal/Component/OOP/Php7AnonymousClassExtractorTest.php +++ b/tests/Hal/Component/OOP/Php7AnonymousClassExtractorTest.php @@ -18,8 +18,9 @@ class Php7AnonymousClassExtractorTest extends \PHPUnit_Framework_TestCase { public function testAnonymousClassIsFound() { $filename = __DIR__.'/../../../resources/oop/php7-1.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(2, sizeof($classes)); @@ -35,8 +36,9 @@ public function testAnonymousClassIsFound() { public function testInterfacesOfAnonymousClassAreFound() { $filename = __DIR__.'/../../../resources/oop/php7-2.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(4, sizeof($classes)); @@ -55,8 +57,9 @@ public function testInterfacesOfAnonymousClassAreFound() { public function testAnonymousClassDoesNotBreakMainClassExtractor() { $filename = __DIR__.'/../../../resources/oop/php7-3.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(2, sizeof($classes)); @@ -72,8 +75,9 @@ public function testAnonymousClassDoesNotBreakMainClassExtractor() { public function testInnerClassAreAttachedToMainClassExtractor() { $filename = __DIR__.'/../../../resources/oop/php7-4.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(4, sizeof($classes)); @@ -86,8 +90,9 @@ public function testInnerClassAreAttachedToMainClassExtractor() { public function testMethodsOfAnonymousClassesAreFound() { $filename = __DIR__.'/../../../resources/oop/php7-7.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(2, sizeof($classes)); @@ -100,8 +105,9 @@ public function testMethodsOfAnonymousClassesAreFound() { public function testDependenciesOfMethodsIncludeDependenciesOfAnonymousClasses() { $filename = __DIR__ . '/../../../resources/oop/php7-5.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(7, sizeof($classes)); @@ -118,8 +124,9 @@ public function testDependenciesOfMethodsIncludeDependenciesOfAnonymousClasses() public function testMultipleClassesWithSameMethodName() { $filename = __DIR__ . '/../../../resources/oop/php7-6.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(7, sizeof($classes)); @@ -134,4 +141,4 @@ public function testMultipleClassesWithSameMethodName() } -} \ No newline at end of file +} diff --git a/tests/Hal/Component/OOP/Php7ReturnExtractorTest.php b/tests/Hal/Component/OOP/Php7ReturnExtractorTest.php index f08df9bc..636d710e 100644 --- a/tests/Hal/Component/OOP/Php7ReturnExtractorTest.php +++ b/tests/Hal/Component/OOP/Php7ReturnExtractorTest.php @@ -20,8 +20,9 @@ class Php7ReturnExtractorTest extends \PHPUnit_Framework_TestCase { public function testAnonymousClassIsFound() { $filename = __DIR__.'/../../../resources/oop/php7-return1.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(1, sizeof($classes)); @@ -41,8 +42,9 @@ public function testAnonymousClassIsFound() { public function testAnonymousClassIsFoundAndNamespaced() { $filename = __DIR__.'/../../../resources/oop/php7-return2.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(2, sizeof($classes)); @@ -62,8 +64,9 @@ public function testAnonymousClassIsFoundAndNamespaced() { public function testReturnedTypeIsFoundEvenOnAbstractMethod() { $filename = __DIR__.'/../../../resources/oop/php7-return3.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(1, sizeof($classes)); @@ -83,8 +86,9 @@ public function testReturnedTypeIsFoundEvenOnAbstractMethod() { public function testUsageOfConstantInParameterDoesNotBreakEngine() { $filename = __DIR__.'/../../../resources/oop/php7-return-bugfix1.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(1, sizeof($classes)); @@ -102,4 +106,4 @@ public function testUsageOfConstantInParameterDoesNotBreakEngine() { $this->assertEquals(array($expected), $method->getReturns()); } -} \ No newline at end of file +} diff --git a/tests/Hal/Component/OOP/Php7ScalarTypeHintTest.php b/tests/Hal/Component/OOP/Php7ScalarTypeHintTest.php index ea82a72d..fe9738d3 100644 --- a/tests/Hal/Component/OOP/Php7ScalarTypeHintTest.php +++ b/tests/Hal/Component/OOP/Php7ScalarTypeHintTest.php @@ -19,8 +19,9 @@ class Php7ScalarTypeHintTest extends \PHPUnit_Framework_TestCase { public function testAnonymousClassIsFound() { $filename = __DIR__.'/../../../resources/oop/php7-scalarhint1.php'; - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $this->assertEquals(1, sizeof($classes)); @@ -29,4 +30,4 @@ public function testAnonymousClassIsFound() { $this->assertEquals(0, sizeof($main->getDependencies())); } -} \ No newline at end of file +} diff --git a/tests/Hal/Metrics/Complexity/Component/McCabe/McCabeTest.php b/tests/Hal/Metrics/Complexity/Component/McCabe/McCabeTest.php index 5d008b69..1ee1c659 100644 --- a/tests/Hal/Metrics/Complexity/Component/McCabe/McCabeTest.php +++ b/tests/Hal/Metrics/Complexity/Component/McCabe/McCabeTest.php @@ -15,8 +15,9 @@ class MacCaybe extends \PHPUnit_Framework_TestCase { */ public function testICanCountComplexity($filename, $expectedCCN) { - $loc = new McCabe(new \Hal\Component\Token\Tokenizer()); - $r = $loc->calculate($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $loc = new McCabe(); + $r = $loc->calculate($tokens); $this->assertEquals($expectedCCN, $r->getCyclomaticComplexityNumber()); } @@ -43,8 +44,9 @@ public function testMcCaybeResultCanBeConvertedToArray() { */ public function testICanCountComplexityOfAnonymousClass($filename, $expectedCCN) { - $loc = new McCabe(new \Hal\Component\Token\Tokenizer()); - $r = $loc->calculate($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $loc = new McCabe(); + $r = $loc->calculate($tokens); $this->assertEquals($expectedCCN, $r->getCyclomaticComplexityNumber()); } @@ -59,8 +61,9 @@ public function provideCCNPHP7() { */ public function testNullCoalesceOperatorIsWellConsidered() { $filename = __DIR__.'/../../../../../resources/mccaybe/php7-null-coalesce-operator.php'; - $loc = new McCabe(new \Hal\Component\Token\Tokenizer()); - $r = $loc->calculate($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $loc = new McCabe(); + $r = $loc->calculate($tokens); $this->assertEquals(3, $r->getCyclomaticComplexityNumber()); } @@ -69,8 +72,9 @@ public function testNullCoalesceOperatorIsWellConsidered() { */ public function testSpaceshipOperatorIsWellConsidered() { $filename = __DIR__.'/../../../../../resources/mccaybe/php7-spaceship-operator.php'; - $loc = new McCabe(new \Hal\Component\Token\Tokenizer()); - $r = $loc->calculate($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $loc = new McCabe(); + $r = $loc->calculate($tokens); $this->assertEquals(2, $r->getCyclomaticComplexityNumber()); } -} \ No newline at end of file +} diff --git a/tests/Hal/Metrics/Complexity/Component/Myer/MyerTest.php b/tests/Hal/Metrics/Complexity/Component/Myer/MyerTest.php index a82ad3e2..b50d005d 100644 --- a/tests/Hal/Metrics/Complexity/Component/Myer/MyerTest.php +++ b/tests/Hal/Metrics/Complexity/Component/Myer/MyerTest.php @@ -14,8 +14,9 @@ class MyerTest extends \PHPUnit_Framework_TestCase { */ public function testICanGetMyerInterval($filename, $interval, $distance) { - $object = new Myer(new \Hal\Component\Token\Tokenizer()); - $result = $object->calculate($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $object = new Myer(); + $result = $object->calculate($tokens); $this->assertEquals($interval, $result->getInterval()); $this->assertEquals($distance, $result->getDistance()); } @@ -34,4 +35,4 @@ public function testMyerResultCanBeConvertedToArray() { $this->assertArrayHasKey('myerInterval', $array); $this->assertArrayHasKey('myerDistance', $array); } -} \ No newline at end of file +} diff --git a/tests/Hal/Metrics/Complexity/Structural/CardAndAgresti/SystemComplexityTest.php b/tests/Hal/Metrics/Complexity/Structural/CardAndAgresti/SystemComplexityTest.php index 8db3bf56..40909bb3 100644 --- a/tests/Hal/Metrics/Complexity/Structural/CardAndAgresti/SystemComplexityTest.php +++ b/tests/Hal/Metrics/Complexity/Structural/CardAndAgresti/SystemComplexityTest.php @@ -20,8 +20,9 @@ public function testICanCalculateSystemComplexityOfClass($totalDataComplexity, $ $filename = tempnam(sys_get_temp_dir(), 'unit-phpmetrics-syc'); file_put_contents($filename, $code); - $extractor = new Extractor(new Tokenizer()); - $classes = $extractor->extract($filename)->getClasses(); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $classes = $extractor->extract($tokens)->getClasses(); $class = $classes[0]; $metric = new SystemComplexity(); $result = $metric->calculate($class); @@ -53,4 +54,4 @@ public function testICanConvertSystemComplexityResultToArray() { $this->assertArrayHasKey('sysc', $array); $this->assertArrayHasKey('dc', $array); } -} \ No newline at end of file +} diff --git a/tests/Hal/Metrics/Complexity/Structural/HenryAndKafura/CouplingTest.php b/tests/Hal/Metrics/Complexity/Structural/HenryAndKafura/CouplingTest.php index 3ebc80bf..b4a2a46f 100644 --- a/tests/Hal/Metrics/Complexity/Structural/HenryAndKafura/CouplingTest.php +++ b/tests/Hal/Metrics/Complexity/Structural/HenryAndKafura/CouplingTest.php @@ -36,8 +36,8 @@ public function setup() { $coupling = new Coupling(); $this->result = $coupling->calculate($classMap); } - - + + public function testICanGetInfoAboutCoupling() { $this->assertEquals(2, $this->result->get('\Ns1\Class1')->getEfferentCoupling()); $this->assertEquals(0, $this->result->get('\Ns1\Class1')->getAfferentCoupling()); @@ -63,9 +63,10 @@ public function testICanExportCoupling() { public function testCouplingTakesCareOfReturnedValues() { $filename = __DIR__.'/../../../../../resources/coupling/f1.php'; - $extractor = new Extractor(new Tokenizer()); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); $classmap = new ClassMap(); - $classmap->push($filename, $extractor->extract($filename)); + $classmap->push($filename, $extractor->extract($tokens)); $coupling = new Coupling(); $this->result = $coupling->calculate($classmap); @@ -74,4 +75,4 @@ public function testCouplingTakesCareOfReturnedValues() { $this->assertEquals(1, $this->result->get('\\ClassWhoIsUsed')->getAfferentCoupling()); $this->assertEquals(0, $this->result->get('\\ClassWhoIsUsed')->getEfferentCoupling()); } -} \ No newline at end of file +} diff --git a/tests/Hal/Metrics/Complexity/Structural/LCOM/LackOfCohesionTest.php b/tests/Hal/Metrics/Complexity/Structural/LCOM/LackOfCohesionTest.php index ce9e46fb..ccd74c2d 100644 --- a/tests/Hal/Metrics/Complexity/Structural/LCOM/LackOfCohesionTest.php +++ b/tests/Hal/Metrics/Complexity/Structural/LCOM/LackOfCohesionTest.php @@ -14,10 +14,11 @@ class LackOfCohesionTest extends \PHPUnit_Framework_TestCase { /** * @dataProvider providesFilesForLcom */ - public function testICanCalculateLackOfCohesionOfClass($file, $expected) { + public function testICanCalculateLackOfCohesionOfClass($filename, $expected) { - $extractor = new Extractor(new \Hal\Component\Token\Tokenizer()); - $result = $extractor->extract($file); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $extractor = new Extractor(); + $result = $extractor->extract($tokens); $classes = $result->getClasses(); $class = $classes[0]; @@ -37,4 +38,4 @@ public function providesFilesForLcom() { , array(__DIR__.'/../../../../../resources/lcom/f5.php', 2) ); } -} \ No newline at end of file +} diff --git a/tests/Hal/Metrics/Complexity/Text/Halstead/HalsteadTest.php b/tests/Hal/Metrics/Complexity/Text/Halstead/HalsteadTest.php index 031c595c..78b4c56d 100644 --- a/tests/Hal/Metrics/Complexity/Text/Halstead/HalsteadTest.php +++ b/tests/Hal/Metrics/Complexity/Text/Halstead/HalsteadTest.php @@ -18,10 +18,10 @@ public function testHalsteadServiceReturnsResult() { $tokenType->expects($this->any()) ->method('isOperand') ->will($this->returnValue(true)); - $tokenizer = new Tokenizer(); - $object = new Halstead($tokenizer, $tokenType); $filename = tempnam(sys_get_temp_dir(), 'tmp-unit'); - $this->assertInstanceOf("\Hal\Metrics\Complexity\Text\Halstead\Result", $object->calculate($filename)); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $object = new Halstead($tokenType); + $this->assertInstanceOf("\Hal\Metrics\Complexity\Text\Halstead\Result", $object->calculate($tokens)); unlink($filename); } @@ -43,11 +43,11 @@ public function testHalsteadResultCanBeConvertedToArray() { /** * @dataProvider provideFilesAndCounts */ - public function testHalsteadGiveValidValues($file, $N1, $N2, $n1, $n2, $N, $V, $L, $D, $E, $T, $I) { + public function testHalsteadGiveValidValues($filename, $N1, $N2, $n1, $n2, $N, $V, $L, $D, $E, $T, $I) { $tokenType = new TokenType(); // please don't mock this: it make no sense else - $tokenizer = new Tokenizer(); - $halstead = new Halstead($tokenizer, $tokenType); - $r = $halstead->calculate($file); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $halstead = new Halstead($tokenType); + $r = $halstead->calculate($tokens); $this->assertEquals($N1, $r->getNumberOfOperators()); $this->assertEquals($n1, $r->getNumberOfUniqueOperators()); @@ -74,4 +74,4 @@ public function provideFilesAndCounts() { , array(__DIR__.'/../../../../../resources/halstead/f3.php', 19, 10, 8, 6 , 29 , 110.41 , 0.15 , 6.67 , 736.09 , 41 , 16.56) // f_switch ); } -} \ No newline at end of file +} diff --git a/tests/Hal/Metrics/Complexity/Text/Length/LocTest.php b/tests/Hal/Metrics/Complexity/Text/Length/LocTest.php index d5257db3..8dd538d7 100644 --- a/tests/Hal/Metrics/Complexity/Text/Length/LocTest.php +++ b/tests/Hal/Metrics/Complexity/Text/Length/LocTest.php @@ -13,8 +13,9 @@ class LocTest extends \PHPUnit_Framework_TestCase { public function testICanCountLoc() { $filename = __DIR__.'/../../../../../resources/loc/f1.php'; - $loc = new Loc(new \Hal\Component\Token\Tokenizer()); - $r = $loc->calculate($filename); + $tokens = (new \Hal\Component\Token\Tokenizer())->tokenize($filename); + $loc = new Loc(); + $r = $loc->calculate($filename, $tokens); $this->assertEquals(14, $r->getCommentLoc()); $this->assertEquals(33, $r->getLoc()); $this->assertEquals(2, $r->getLogicalLoc()); @@ -29,4 +30,4 @@ public function testLocResultCanBeConvertedToArray() { $this->assertArrayHasKey('loc', $array); $this->assertArrayHasKey('logicalLoc', $array); } -} \ No newline at end of file +}