Skip to content

Commit

Permalink
analyze does not break in case of error
Browse files Browse the repository at this point in the history
  • Loading branch information
Halleck45 committed Nov 22, 2016
1 parent 9d90b7a commit c804977
Show file tree
Hide file tree
Showing 15 changed files with 224 additions and 46 deletions.
5 changes: 4 additions & 1 deletion src/Hal/Application/Analyze.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Hal\Application;

use Hal\Application\Config\Config;
use Hal\Component\Ast\NodeTraverser;
use Hal\Component\Issue\Issuer;
use Hal\Metric\Class_\ClassEnumVisitor;
use Hal\Metric\Class_\Complexity\CyclomaticComplexityVisitor;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function run($files)

// prepare parser
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$traverser = new \PhpParser\NodeTraverser();
$traverser = new NodeTraverser();
$traverser->addVisitor(new \PhpParser\NodeVisitor\NameResolver());
$traverser->addVisitor(new ClassEnumVisitor($metrics));
$traverser->addVisitor(new CyclomaticComplexityVisitor($metrics));
Expand Down Expand Up @@ -96,6 +97,8 @@ public function run($files)
} catch (Error $e) {
$this->output->writeln(sprintf('<error>Cannot parse %s</error>', $file));
}
$this->issuer->clear('filename');
$this->issuer->clear('statements');
}

//
Expand Down
2 changes: 1 addition & 1 deletion src/Hal/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function run($argv)
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, new OutputFormatter());

// issues and debug
$issuer = (new Issuer($output))->enable();
$issuer = (new Issuer($output));//->enable();

// config
$config = (new Parser())->parse($argv);
Expand Down
2 changes: 1 addition & 1 deletion src/Hal/Application/Config/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function validate(Config $config)

// excluded directories
if (!$config->has('exclude')) {
$config->set('exclude', 'vendor,test,Test,tests,Tests,testing,Testing,bower_components,node_modules,cache');
$config->set('exclude', 'vendor,test,Test,tests,Tests,testing,Testing,bower_components,node_modules,cache,spec');
}
$config->set('exclude', array_filter(explode(',', $config->get('exclude'))));

Expand Down
84 changes: 84 additions & 0 deletions src/Hal/Component/Ast/NodeTraverser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* (c) Jean-François Lépine <https://twitter.com/Halleck45>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Hal\Component\Ast;

use PhpParser\Node;
use PhpParser\NodeTraverser as Mother;

/**
* Custom Ast Traverser
*
* @author Jean-François Lépine <https://twitter.com/Halleck45>
*/
class NodeTraverser extends Mother
{
protected $stopCondition;

public function __construct($cloneNodes = false, $stopCondition = null)
{
parent::__construct($cloneNodes);
$this->stopCondition = $stopCondition;
}

protected function traverseArray(array $nodes) {
$doNodes = array();

foreach ($nodes as $i => &$node) {
if (is_array($node)) {
$node = $this->traverseArray($node);
} elseif ($node instanceof Node) {
$traverseChildren = true;

// custom stop condition
if($node instanceof Node\Stmt\Class_ || $node instanceof Node\Stmt\Interface_) {
$traverseChildren = false;
}

foreach ($this->visitors as $visitor) {
$return = $visitor->enterNode($node);
if (self::DONT_TRAVERSE_CHILDREN === $return) {
$traverseChildren = false;
} else if (null !== $return) {
$node = $return;
}
}

if ($traverseChildren) {
$node = $this->traverseNode($node);
}

foreach ($this->visitors as $visitor) {
$return = $visitor->leaveNode($node);

if (self::REMOVE_NODE === $return) {
$doNodes[] = array($i, array());
break;
} elseif (is_array($return)) {
$doNodes[] = array($i, $return);
break;
} elseif (null !== $return) {
$node = $return;
}
}
}
}

if (!empty($doNodes)) {
while (list($i, $replace) = array_pop($doNodes)) {
array_splice($nodes, $i, 1, $replace);
}
}

return $nodes;
}



}
24 changes: 18 additions & 6 deletions src/Hal/Component/Issue/Issuer.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,18 @@ public function onError($errno, $errstr, $errfile, $errline)
+ PHP: $php
+ PhpMetrics: $phpmetrics
+ Operating System: $os
**Backtrace**
+ File: $errfile (line $errline)
<details>
<summary>Details</summary>
```
$trace
**Debug**
```
$debug
```
</details>
EOT;

$this->output->write($message);
Expand Down Expand Up @@ -140,7 +141,8 @@ protected function terminate($status)
* @param $log
* @return $this
*/
protected function log($logfile, $log) {
protected function log($logfile, $log)
{
if (is_writable(getcwd())) {
file_put_contents($logfile, $log);
} else {
Expand All @@ -159,5 +161,15 @@ public function set($debugKey, $value)
$this->debug[$debugKey] = $value;
return $this;
}

/**
* @param $debugKey
* @return $this
*/
public function clear($debugKey)
{
unset($this->debug[$debugKey]);
return $this;
}
}

8 changes: 8 additions & 0 deletions src/Hal/Metric/BagTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ public function fromArray(array $array)
}
return $this;
}

/**
* @inheritdoc
*/
function jsonSerialize()
{
return array_merge($this->all(), ['_type' => get_class($this)]);
}
}
2 changes: 1 addition & 1 deletion src/Hal/Metric/ClassMetric.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Hal\Metric;

class ClassMetric implements Metric
class ClassMetric implements Metric, \JsonSerializable
{
use BagTrait;
}
2 changes: 1 addition & 1 deletion src/Hal/Metric/FileMetric.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Hal\Metric;

class FileMetric implements Metric
class FileMetric implements Metric, \JsonSerializable
{
use BagTrait;
}
2 changes: 1 addition & 1 deletion src/Hal/Metric/FunctionMetric.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Hal\Metric;

class FunctionMetric implements Metric
class FunctionMetric implements Metric, \JsonSerializable
{
use BagTrait;
}
10 changes: 9 additions & 1 deletion src/Hal/Metric/Metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Class Metrics
* @package Hal\Metric
*/
class Metrics
class Metrics implements \JsonSerializable
{

/**
Expand Down Expand Up @@ -48,4 +48,12 @@ public function all()
{
return $this->data;
}

/**
* @inheritdoc
*/
function jsonSerialize()
{
return $this->all();
}
}
2 changes: 1 addition & 1 deletion src/Hal/Metric/ProjectMetric.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace Hal\Metric;

class ProjectMetric implements Metric
class ProjectMetric implements Metric, \JsonSerializable
{
use BagTrait;
}
12 changes: 9 additions & 3 deletions src/Hal/Report/Html/Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ public function generate(Metrics $metrics)
}

// copy sources
file_exists($logDir . '/js') || mkdir($logDir.'/js', 0755, true);
file_exists($logDir . '/css') || mkdir($logDir.'/css', 0755, true);
file_exists($logDir . '/images') || mkdir($logDir.'/images', 0755, true);
if(!file_exists($logDir . '/js')) {
mkdir($logDir.'/js', 0755, true);
}
if(!file_exists($logDir . '/css')) {
mkdir($logDir.'/css', 0755, true);
}
if(!file_exists($logDir . '/images')) {
mkdir($logDir.'/images', 0755, true);
}
recurse_copy(__DIR__ . '/template/js', $logDir . '/js');
recurse_copy(__DIR__ . '/template/css', $logDir . '/css');
recurse_copy(__DIR__ . '/template/images', $logDir . '/images');
Expand Down

0 comments on commit c804977

Please sign in to comment.