Completed
Push — try/code-signature-diff ( d519f8...76da74 )
by
unknown
270:19 queued 262:09
created

Declarations::scan_dir()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 2
dl 0
loc 22
rs 9.2568
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 Declarations extends PersistentList {
10
11
	private $parser;
12
13
	function __construct() {
14
		$this->parser    = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 );
15
		parent::__construct();
16
	}
17
18
	private function slashit( $path ) {
19
		$path .= ( substr( $path, -1 ) == '/' ? '' : '/' );
20
		return $path;
21
	}
22
23
	/**
24
	 * Scan every PHP in the root
25
	 */
26
	public function scan( $root, $exclude = array() ) {
27
		if ( is_dir( $root ) ) {
28
			return $this->scan_dir( $this->slashit( $root ), $exclude );
29
		} elseif( is_file( $root ) ) {
30
			return $this->scan_file( $this->slashit( dirname( $root ) ), $root );
31
		} else {
32
			throw new \Exception( 'input_error', "Expected $root to be a file or directory" );
33
		}
34
	}
35
36
	public function scan_dir( $root, $exclude = array() ) {
37
38
		$filter  = function ( $file, $key, $iterator ) use ( $exclude ) {
39
			if ( $iterator->hasChildren() && ! in_array( $file->getFilename(), $exclude ) ) {
40
				return true;
41
			}
42
			return $file->isFile();
43
		};
44
45
		$inner_iterator = new \RecursiveDirectoryIterator( $root, \RecursiveDirectoryIterator::SKIP_DOTS );
46
47
		$iterator = new \RecursiveIteratorIterator(
48
			new \RecursiveCallbackFilterIterator( $inner_iterator, $filter )
49
		);
50
51
		$valid_extensions = array( 'php' );
52
		foreach ( $iterator as $file ) {
53
			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...
54
				$this->scan_file( $root, $file );
55
			}
56
		}
57
	}
58
59
	public function scan_file( $root, $file_path ) {
60
		$file_path_relative = str_replace( $root, '', $file_path );
61
62
		$source = file_get_contents( $file_path );
63
		try {
64
			$ast = $this->parser->parse( $source );
65
		} 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...
66
			echo "Parse error: {$error->getMessage()}\n";
67
			return;
68
		}
69
70
		// $dumper = new NodeDumper;
71
		// echo $dumper->dump($ast) . "\n";
72
73
		$traverser    = new NodeTraverser();
74
		$nameResolver = new NameResolver();
75
		$traverser->addVisitor( $nameResolver );
76
77
		// Resolve names
78
		$ast = $traverser->traverse( $ast );
79
80
		// now scan for public methods etc
81
		$traverser           = new NodeTraverser();
82
		$declaration_visitor = new Declarations\Visitor( $file_path_relative, $this );
83
		$traverser->addVisitor( $declaration_visitor );
84
		$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...
85
	}
86
87
	public function load( $file_path ) {
88
		$row = 1;
89
		if ( ( $handle = fopen( $file_path , "r" ) ) !== FALSE ) {
90
			while ( ( $data = fgetcsv( $handle, 1000, "," ) ) !== FALSE ) {
91
				$num = count( $data );
0 ignored issues
show
Unused Code introduced by
$num 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...
92
				list( $type, $file, $line, $class_name, $name, $static, $params_json ) = $data;
93
94
				switch( $type ) {
95
					case 'class':
96
						$this->add( new Declarations\Class_( $file, $line, $class_name ) );
97
						break;
98
99
					case 'property':
100
						$this->add( new Declarations\Class_Property( $file, $line, $class_name, $name, $static ) );
101
						break;
102
103 View Code Duplication
					case 'method':
104
						$params = json_decode( $params_json, TRUE );
105
						$declaration = new Declarations\Class_Method( $file, $line, $class_name, $name, $static );
106
						if ( is_array( $params ) ) {
107
							foreach( $params as $param ) {
108
								$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic );
109
							}
110
						}
111
112
						$this->add( $declaration );
113
114
						break;
115
116 View Code Duplication
					case 'function':
117
						$params = json_decode( $params_json, TRUE );
118
						$declaration = new Declarations\Function_( $file, $line, $name );
119
						if ( is_array( $params ) ) {
120
							foreach( $params as $param ) {
121
								$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic );
122
							}
123
						}
124
125
						$this->add( $declaration );
126
127
						break;
128
				}
129
				$row++;
130
			}
131
			fclose( $handle );
132
		}
133
	}
134
}