Completed
Push — add/analyzer-class-const ( 1ca9f2 )
by
unknown
170:16 queued 161:57
created

Declarations::scan()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 9
loc 9
rs 9.9666
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 View Code Duplication
	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( "Expected $root to be a file or directory" );
33
		}
34
	}
35
36
	public function scan_dir( $root, $exclude = array() ) {
37
38
		if ( is_null( $exclude ) || ! is_array( $exclude ) ) {
39
			throw new Exception( 'Exclude must be an array' );
40
		}
41
42 View Code Duplication
		$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 View Code Duplication
		foreach ( $iterator as $file ) {
57
			$parts             = explode( '.', $file );
58
			$current_extension = strtolower( array_pop( $parts ) );
59
60
			if ( in_array( $current_extension, $valid_extensions, true ) ) {
61
				$this->scan_file( $root, $file );
62
			}
63
		}
64
	}
65
66 View Code Duplication
	public function scan_file( $root, $file_path ) {
67
		$file_path_relative = str_replace( $root, '', $file_path );
68
69
		$source = file_get_contents( $file_path );
70
		try {
71
			$ast = $this->parser->parse( $source );
72
		} catch ( \Error $error ) {
0 ignored issues
show
Bug introduced by
The class 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...
73
			echo "Parse error: {$error->getMessage()}\n";
74
			return;
75
		} catch ( \RuntimeException $error ) {
76
			echo "Parse error: {$error->getMessage()}\n";
77
			return;
78
		}
79
80
		// $dumper = new NodeDumper;
81
		// echo $dumper->dump($ast) . "\n";
82
83
		$traverser    = new NodeTraverser();
84
		$nameResolver = new NameResolver();
85
		$traverser->addVisitor( $nameResolver );
86
87
		// Resolve names
88
		$ast = $traverser->traverse( $ast );
89
90
		// now scan for public methods etc
91
		$traverser           = new NodeTraverser();
92
		$declaration_visitor = new Declarations\Visitor( $file_path_relative, $this );
93
		$traverser->addVisitor( $declaration_visitor );
94
		$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...
95
	}
96
97
	public function load( $file_path ) {
98
		$row = 1;
99
		if ( ( $handle = fopen( $file_path, 'r' ) ) !== false ) {
100
			while ( ( $data = fgetcsv( $handle, 1000, ',' ) ) !== false ) {
101
				$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...
102
				@list( $type, $file, $line, $class_name, $name, $static, $params_json ) = $data;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
103
				switch ( $type ) {
104
					case 'class':
105
						$this->add( new Declarations\Class_( $file, $line, $class_name ) );
106
						break;
107
108
					case 'property':
109
						$this->add( new Declarations\Class_Property( $file, $line, $class_name, $name, $static ) );
110
						break;
111
112
					case 'class_const':
113
						echo "Loading class const declaration\n";
114
						$this->add( new Declarations\Class_Const( $file, $line, $class_name, $name ) );
115
						break;
116
117 View Code Duplication
					case 'method':
118
						$params      = json_decode( $params_json );
119
						$declaration = new Declarations\Class_Method( $file, $line, $class_name, $name, $static );
120
						if ( is_array( $params ) ) {
121
							foreach ( $params as $param ) {
122
								$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic );
123
							}
124
						}
125
126
						$this->add( $declaration );
127
128
						break;
129
130 View Code Duplication
					case 'function':
131
						$params      = json_decode( $params_json );
132
						$declaration = new Declarations\Function_( $file, $line, $name );
133
						if ( is_array( $params ) ) {
134
							foreach ( $params as $param ) {
135
								$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic );
136
							}
137
						}
138
139
						$this->add( $declaration );
140
141
						break;
142
				}
143
				$row++;
144
			}
145
			fclose( $handle );
146
		}
147
	}
148
}
149