Completed
Push — renovate/glob-7.x ( 697d78...f7fc07 )
by
unknown
18:21 queued 12:01
created

Declarations::scan_dir()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 29

Duplication

Lines 14
Ratio 48.28 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 2
dl 14
loc 29
rs 8.5226
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
						$this->add( new Declarations\Class_Const( $file, $line, $class_name, $name ) );
114
						break;
115
116 View Code Duplication
					case 'method':
117
						$params      = json_decode( $params_json );
118
						$declaration = new Declarations\Class_Method( $file, $line, $class_name, $name, $static );
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 View Code Duplication
					case 'function':
130
						$params      = json_decode( $params_json );
131
						$declaration = new Declarations\Function_( $file, $line, $name );
132
						if ( is_array( $params ) ) {
133
							foreach ( $params as $param ) {
134
								$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic );
135
							}
136
						}
137
138
						$this->add( $declaration );
139
140
						break;
141
				}
142
				$row++;
143
			}
144
			fclose( $handle );
145
		}
146
	}
147
}
148