Completed
Push — try/code-signature-diff ( 76da74...88bced )
by
unknown
261:36 queued 253:34
created

Invocations::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
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\NodeDumper;
8
use PhpParser\NodeVisitor\NameResolver;
9
10
11
/**
12
 * TODO: share this file loading code w/ Declarations
13
 */
14
class Invocations extends PersistentList {
15
	private $parser;
16
17
	function __construct() {
18
		$this->parser    = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 );
19
		parent::__construct();
20
	}
21
22
	private function slashit( $path ) {
23
		$path .= ( substr( $path, -1 ) == '/' ? '' : '/' );
24
		return $path;
25
	}
26
27
	/**
28
	 * Scan every PHP in the root
29
	 */
30 View Code Duplication
	public function scan( $root, $exclude = array() ) {
31
		if ( is_dir( $root ) ) {
32
			return $this->scan_dir( $this->slashit( $root ), $exclude );
33
		} elseif( is_file( $root ) ) {
34
			return $this->scan_file( $this->slashit( dirname( $root ) ), $root );
0 ignored issues
show
Bug introduced by
The call to scan_file() misses a required argument $differences.

This check looks for function calls that miss required arguments.

Loading history...
35
		} else {
36
			throw new \Exception( 'input_error', "Expected $root to be a file or directory" );
37
		}
38
	}
39
40 View Code Duplication
	public function scan_dir( $root, $exclude = array() ) {
41
42
		$filter  = function ( $file, $key, $iterator ) use ( $exclude ) {
43
			if ( $iterator->hasChildren() && ! in_array( $file->getFilename(), $exclude ) ) {
44
				return true;
45
			}
46
			return $file->isFile();
47
		};
48
49
		$inner_iterator = new \RecursiveDirectoryIterator( $root, \RecursiveDirectoryIterator::SKIP_DOTS );
50
51
		$iterator = new \RecursiveIteratorIterator(
52
			new \RecursiveCallbackFilterIterator( $inner_iterator, $filter )
53
		);
54
55
		$valid_extensions = array( 'php' );
56
		foreach ( $iterator as $file ) {
57
			if ( in_array( strtolower( array_pop( explode( '.', $file ) ) ), $valid_extensions ) ) {
0 ignored issues
show
Bug introduced by
explode('.', $file) cannot be passed to array_pop() as the parameter $array expects a reference.
Loading history...
58
				$this->scan_file( $root, $file );
0 ignored issues
show
Bug introduced by
The call to scan_file() misses a required argument $differences.

This check looks for function calls that miss required arguments.

Loading history...
59
			}
60
		}
61
	}
62
63
	// public function scan_file( $root, $file_path ) {
64
	// 	$file_path_relative = str_replace( $root, '', $file_path );
65
66
	// 	$source = file_get_contents( $file_path );
67
	// 	try {
68
	// 		$ast = $this->parser->parse( $source );
69
	// 	} catch ( Error $error ) {
70
	// 		echo "Parse error: {$error->getMessage()}\n";
71
	// 		return;
72
	// 	}
73
74
	// 	// $dumper = new NodeDumper;
75
	// 	// echo $dumper->dump($ast) . "\n";
76
77
	// 	$traverser    = new NodeTraverser();
78
	// 	$nameResolver = new NameResolver();
79
	// 	$traverser->addVisitor( $nameResolver );
80
81
	// 	// Resolve names
82
	// 	$ast = $traverser->traverse( $ast );
83
84
	// 	// now scan for public methods etc
85
	// 	$traverser           = new NodeTraverser();
86
	// 	$declaration_visitor = new Declarations\Visitor( $file_path_relative, $this );
87
	// 	$traverser->addVisitor( $declaration_visitor );
88
	// 	$ast = $traverser->traverse( $ast );
89
	// }
90
91
	/**
92
	 * Scans the file for any invocations that depend on missing or different classes, methods, properties and functions
93
	 */
94
	public function scan_file( $root, $file_path, $differences ) {
95
		$source = file_get_contents( $file_path );
96
		$parser = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 );
97
		try {
98
			$ast    = $parser->parse( $source );
99
		} 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...
100
			echo "Parse error: {$error->getMessage()}\n";
101
			return;
102
		}
103
104
		// $dumper = new NodeDumper;
105
		// echo $dumper->dump($ast) . "\n";
106
107
		// before parsing, make sure we try to resolve class names
108
		$traverser    = new NodeTraverser();
109
		$nameResolver = new NameResolver();
110
		$traverser->addVisitor( $nameResolver );
111
112
		// Resolve names
113
		$ast = $traverser->traverse( $ast );
114
115
		$traverser         = new NodeTraverser();
116
		$invocations       = new Invocations();
0 ignored issues
show
Unused Code introduced by
$invocations 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...
117
		$invocation_finder = new Invocations\Visitor( $file_path, $this );
118
		$traverser->addVisitor( $invocation_finder );
119
		$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...
120
121
		// print_r($this);
122
		// $this->print();
123
		// return $invocations;
124
125
		// $dumper = new NodeDumper;
126
		// echo $dumper->dump($ast) . "\n";
127
128
		// TODO: return a list of warnings and errors
129
130
		/**
131
		 * Scan every invocation to see if it depends on a Difference
132
		 */
133
		$warnings = new Warnings();
134
		foreach( $this->get() as $invocation ) {
135
			foreach( $differences->get() as $difference ) {
136
				// $warning = $
137
				$difference->find_invocation_warnings( $invocation, $warnings );
138
			}
139
		}
140
141
		return $warnings;
142
	}
143
}