Completed
Push — try/code-signature-diff ( 313026...01fa4f )
by
unknown
08:13
created

Declarations::slashit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
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\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( 'input_error', "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
		$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 );
59
			}
60
		}
61
	}
62
63 View Code Duplication
	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 ) {
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...
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 );
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...
89
	}
90
91
	public function load( $file_path ) {
92
		$row = 1;
93
		if ( ( $handle = fopen( $file_path , "r" ) ) !== FALSE ) {
94
			while ( ( $data = fgetcsv( $handle, 1000, "," ) ) !== FALSE ) {
95
				$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...
96
				list( $type, $file, $line, $class_name, $name, $static, $params_json ) = $data;
97
98
				switch( $type ) {
99
					case 'class':
100
						$this->add( new Declarations\Class_( $file, $line, $class_name ) );
101
						break;
102
103
					case 'property':
104
						$this->add( new Declarations\Class_Property( $file, $line, $class_name, $name, $static ) );
105
						break;
106
107 View Code Duplication
					case 'method':
108
						$params = json_decode( $params_json );
109
						$declaration = new Declarations\Class_Method( $file, $line, $class_name, $name, $static );
110
						if ( is_array( $params ) ) {
111
							foreach( $params as $param ) {
112
								$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic );
113
							}
114
						}
115
116
						$this->add( $declaration );
117
118
						break;
119
120 View Code Duplication
					case 'function':
121
						$params = json_decode( $params_json );
122
						$declaration = new Declarations\Function_( $file, $line, $name );
123
						if ( is_array( $params ) ) {
124
							foreach( $params as $param ) {
125
								$declaration->add_param( $param->name, $param->default, $param->type, $param->byRef, $param->variadic );
126
							}
127
						}
128
129
						$this->add( $declaration );
130
131
						break;
132
				}
133
				$row++;
134
			}
135
			fclose( $handle );
136
		}
137
	}
138
}