Completed
Push — try/code-signature-diff ( 2d24ed...5d4d06 )
by
unknown
121:31 queued 111:30
created

Analyzer::check_file_compatibility()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack\Analyzer;
4
5
use PhpParser\ParserFactory;
6
use PhpParser\NodeTraverser;
7
use PhpParser\NodeVisitor\NameResolver;
8
9
class Analyzer {
10
	private $declarations;
11
	private $base_path;
12
	private $current_path;
13
	private $current_relative_path;
14
	private $parser;
15
16
	function __construct( $base_path ) {
17
		$this->parser    = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 );
18
		$this->base_path = $this->slashit( $base_path );
19
	}
20
21
	private function slashit( $path ) {
22
		$path .= ( substr( $path, -1 ) == '/' ? '' : '/' );
23
		return $path;
24
	}
25
26
	public function scan() {
27
		$declarations = new Declarations();
28
29
		$exclude = array( '.git', 'vendor', 'tests', 'docker', 'bin', 'scss', 'images', 'docs', 'languages', 'node_modules' );
30
		$filter  = function ( $file, $key, $iterator ) use ( $exclude ) {
31
			if ( $iterator->hasChildren() && ! in_array( $file->getFilename(), $exclude ) ) {
32
				return true;
33
			}
34
			return $file->isFile();
35
		};
36
37
		$inner_iterator = new \RecursiveDirectoryIterator( $this->base_path, \RecursiveDirectoryIterator::SKIP_DOTS );
38
39
		$iterator = new \RecursiveIteratorIterator(
40
			new \RecursiveCallbackFilterIterator( $inner_iterator, $filter )
41
		);
42
43
		$display = array( 'php' );
44
		foreach ( $iterator as $file ) {
45
			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...
46
				$this->file( $file, $declarations );
47
			}
48
		}
49
50
		return $declarations;
51
	}
52
53
	public function file( $file_path, $declarations ) {
54
		$this->current_path    = $file_path;
55
		$current_relative_path = str_replace( $this->base_path, '', $file_path );
56
57
		$source = file_get_contents( $file_path );
58
		try {
59
			$ast = $this->parser->parse( $source );
60
		} 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...
61
			echo "Parse error: {$error->getMessage()}\n";
62
			return;
63
		}
64
65
		// $dumper = new NodeDumper;
66
		// echo $dumper->dump($ast) . "\n";
67
68
		$traverser    = new NodeTraverser();
69
		$nameResolver = new NameResolver();
70
		$traverser->addVisitor( $nameResolver );
71
72
		// Resolve names
73
		$ast = $traverser->traverse( $ast );
74
75
		// now scan for public methods etc
76
		$traverser           = new NodeTraverser();
77
		$declaration_visitor = new Declarations\Visitor( $current_relative_path, $declarations );
78
		$traverser->addVisitor( $declaration_visitor );
79
		$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...
80
	}
81
}
82
83
/*
84
class Difference_Params {
85
	public $declaration;
86
87
	function __construct( $declaration ) {
88
		$this->declaration = $declaration;
89
	}
90
91
	public function to_csv() {
92
		return 'params,' . implode( ',', $this->declaration->to_csv_array() );
93
	}
94
}
95
*/
96