Completed
Push — try/code-signature-diff ( 5d24fe...2d24ed )
by
unknown
107:52 queued 99:19
created

Declaration   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 0
1
<?php
2
3
namespace Automattic\Jetpack\Analyzer;
4
5
// use PhpParser\Error;
6
// use PhpParser\NodeDumper;
7
use PhpParser\ParserFactory;
8
// use PhpParser\Node\Stmt\Function_;
9
// use PhpParser\Node\Stmt\ClassMethod_;
10
use PhpParser\NodeTraverser;
11
// use PhpParser\NodeVisitorAbstract;
12
use PhpParser\NodeVisitor\NameResolver;
13
14
// const STATE_NONE = 0;
15
// const STATE_CLASS_DECLARATION = 1;
16
17
const VIS_PUBLIC  = 0;
18
const VIS_PRIVATE = 1;
19
20
class Analyzer {
21
	private $declarations;
22
	private $base_path;
23
	private $current_path;
24
	private $current_relative_path;
25
	private $parser;
26
27
	function __construct( $base_path ) {
28
		$this->parser       = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 );
29
		$this->base_path    = $this->slashit( $base_path );
30
	}
31
32
	private function slashit( $path ) {
33
		$path .= ( substr( $path, -1 ) == '/' ? '' : '/' );
34
		return $path;
35
	}
36
37
	public function scan() {
38
		$declarations = new Declarations();
39
40
		$exclude = array( '.git', 'vendor', 'tests', 'docker', 'bin', 'scss', 'images', 'docs', 'languages', 'node_modules' );
41
		$filter  = function ( $file, $key, $iterator ) use ( $exclude ) {
42
			if ( $iterator->hasChildren() && ! in_array( $file->getFilename(), $exclude ) ) {
43
				return true;
44
			}
45
			return $file->isFile();
46
		};
47
48
		$inner_iterator = new \RecursiveDirectoryIterator( $this->base_path, \RecursiveDirectoryIterator::SKIP_DOTS );
49
50
		$iterator = new \RecursiveIteratorIterator(
51
			new \RecursiveCallbackFilterIterator( $inner_iterator, $filter )
52
		);
53
54
		$display = array( 'php' );
55
		foreach ( $iterator as $file ) {
56
			if ( in_array( strtolower( array_pop( explode( '.', $file ) ) ), $display ) ) {
0 ignored issues
show
Bug introduced by
explode('.', $file) cannot be passed to array_pop() as the parameter $array expects a reference.
Loading history...
57
				$this->file( $file, $declarations );
58
			}
59
		}
60
61
		return $declarations;
62
	}
63
64
	public function file( $file_path, $declarations ) {
65
		$this->current_path = $file_path;
66
		$current_relative_path = str_replace( $this->base_path, '', $file_path );
67
68
		$source = file_get_contents( $file_path );
69
		try {
70
			$ast = $this->parser->parse( $source );
71
		} catch ( Error $error ) {
0 ignored issues
show
Bug introduced by
The class Automattic\Jetpack\Analyzer\Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
72
			echo "Parse error: {$error->getMessage()}\n";
73
			return;
74
		}
75
76
		// $dumper = new NodeDumper;
77
		// echo $dumper->dump($ast) . "\n";
78
79
		$traverser = new NodeTraverser();
80
		$nameResolver = new NameResolver();
81
		$traverser->addVisitor( $nameResolver );
82
83
		// Resolve names
84
		$ast = $traverser->traverse( $ast );
85
86
		// now scan for public methods etc
87
		$traverser = new NodeTraverser();
88
		$declaration_visitor = new Declarations\Visitor( $current_relative_path, $declarations );
89
		$traverser->addVisitor( $declaration_visitor );
90
		$ast = $traverser->traverse( $ast );
0 ignored issues
show
Unused Code introduced by
$ast is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
91
	}
92
93
	public function check_file_compatibility( $file_path ) {
94
		$source = file_get_contents( $file_path );
95
		try {
96
			$ast = $this->parser->parse( $source );
97
		} catch ( Error $error ) {
0 ignored issues
show
Bug introduced by
The class Automattic\Jetpack\Analyzer\Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
98
			echo "Parse error: {$error->getMessage()}\n";
99
			return;
100
		}
101
102
		// $dumper = new NodeDumper;
103
		// echo $dumper->dump($ast) . "\n";
104
105
		$traverser = new NodeTraverser();
106
		$invocation_finder = new Invocation_Visitor( $this );
107
		$traverser->addVisitor( $invocation_finder );
108
		$ast = $traverser->traverse( $ast );
0 ignored issues
show
Unused Code introduced by
$ast is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
	}
110
}
111
112
/*
113
class Difference_Params {
114
	public $declaration;
115
116
	function __construct( $declaration ) {
117
		$this->declaration = $declaration;
118
	}
119
120
	public function to_csv() {
121
		return 'params,' . implode( ',', $this->declaration->to_csv_array() );
122
	}
123
}
124
*/