Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pass TokenCollection instead of Tokenizer to Metrics #208

Merged
merged 2 commits into from
Apr 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/Hal/Application/Command/Job/Analyze/FileAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
16 changes: 8 additions & 8 deletions src/Hal/Application/Command/Job/DoAnalyze.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pavarnos Cache is used at least here 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes... in the current master branch. But once this pull request is merged the code does not need it. (I searched through the whole code base: "CacheMemory" is not mentioned anywhere). Tokenizer now has no constructor.


$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
);

Expand All @@ -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("<error>file %s has been skipped: \n%s</error>", $filename, $e->getMessage()));
unset($files[$k]);
Expand Down
2 changes: 1 addition & 1 deletion src/Hal/Component/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ public function has($key);
* @return mixed
*/
public function clear($key);
}
}
3 changes: 1 addition & 2 deletions src/Hal/Component/Cache/CacheMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/

namespace Hal\Component\Cache;
use Hal\Component\Result\ResultCollection;

/**
* Cache
Expand Down Expand Up @@ -49,4 +48,4 @@ public function has($key) {
public function clear($key) {
$this->data = array();
}
}
}
2 changes: 1 addition & 1 deletion src/Hal/Component/Cache/CacheNull.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ public function has($key) {
*/
public function clear($key) {
}
}
}
17 changes: 5 additions & 12 deletions src/Hal/Component/OOP/Extractor/Extractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -174,4 +167,4 @@ public function extract($filename)
return $result;
}

};
};
26 changes: 2 additions & 24 deletions src/Hal/Component/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
*/

namespace Hal\Component\Token;
use Hal\Component\Cache\Cache;
use Hal\Component\Cache\CacheNull;

/**
* Tokenize file
Expand All @@ -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
*
Expand All @@ -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);
}

Expand All @@ -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;
Expand Down
26 changes: 4 additions & 22 deletions src/Hal/Metrics/Complexity/Component/McCabe/McCabe.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/

namespace Hal\Metrics\Complexity\Component\McCabe;
use Hal\Component\Token\Tokenizer;
use Hal\Component\Token\TokenCollection;

/**
* Calculates cyclomatic complexity
Expand All @@ -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
*
Expand All @@ -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) {
Expand Down Expand Up @@ -91,4 +73,4 @@ public function calculate($filename)
$info->setCyclomaticComplexityNumber(max(1, $ccn));
return $info;
}
}
}
31 changes: 6 additions & 25 deletions src/Hal/Metrics/Complexity/Component/Myer/Myer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;
Expand All @@ -75,4 +56,4 @@ public function calculate($filename)

return $result;
}
}
}
Loading