Skip to content

Commit

Permalink
Merge branch 'version2_ast' of github.com:phpmetrics/PhpMetrics into …
Browse files Browse the repository at this point in the history
…version2_ast
  • Loading branch information
clagiordano committed Dec 12, 2016
2 parents 203aaf6 + c804977 commit 8755576
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 55 deletions.
5 changes: 4 additions & 1 deletion src/Hal/Application/Analyze.php
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
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
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
@@ -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;
}



}
52 changes: 44 additions & 8 deletions src/Hal/Component/Issue/Issuer.php
Expand Up @@ -71,12 +71,20 @@ public function onError($errno, $errstr, $errfile, $errline)
$debug .= sprintf("%s: %s\n", $key, $value);
}

$logfile = './phpmetrics-error.log';

$message = <<<EOT
<error>An unexpected error occured. Can you open a new issue following this link please ?</error>
<error>We're sorry : an unexpected error occured.</error>
<question>Can you help us ?</question> Please open a new issue at https://github.com/phpmetrics/PhpMetrics/issues/new, and copy-paste the content
of this file: $logfile ?
Thanks for your help :)
## Link: https://github.com/phpmetrics/PhpMetrics/issues/new
EOT;

$log = <<<EOT
## Title: $errstr
## Message:
Expand All @@ -92,20 +100,23 @@ 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);

$this->log($logfile, $log);
$this->terminate(1);
}

Expand All @@ -121,10 +132,25 @@ public function enable()
/**
* @param $status
*/
protected function terminate($status) {
protected function terminate($status)
{
exit($status);
}

/**
* @param $log
* @return $this
*/
protected function log($logfile, $log)
{
if (is_writable(getcwd())) {
file_put_contents($logfile, $log);
} else {
$this->output->write($log);
}
return $this;
}

/**
* @param $debugKey
* @param $value
Expand All @@ -135,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
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
@@ -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
@@ -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
@@ -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
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
@@ -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
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 8755576

Please sign in to comment.