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

Differences   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B find() 0 30 7
A check_file_compatibility() 0 49 4
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
class Differences extends PersistentList {
11
12
	public function find( $new_declarations, $prev_declarations ) {
13
		$total = 0;
14
		// for each declaration, see if it exists in the current analyzer's declarations
15
		// if not, add it to the list of differences - either as missing or different
16
		foreach( $prev_declarations->get() as $prev_declaration ) {
17
			$matched = false;
18
			foreach( $new_declarations->get() as $new_declaration ) {
19
				if ( $prev_declaration->match( $new_declaration ) ) {
20
					$matched = true;
21
					break;
22
				}
23
			}
24
			if ( ! $matched ) {
25
				switch( $prev_declaration->type() ) {
26
					case 'class':
27
						$this->add( new Differences\Class_Missing( $prev_declaration ) );
28
						break;
29
					case 'method':
30
						$this->add( new Differences\Class_Method_Missing( $prev_declaration ) );
31
						break;
32
					default:
33
						echo "Unknown unmatched type " . $prev_declaration->type() . "\n";
34
				}
35
			}
36
			$total += 1;
37
		}
38
39
		echo "Total: $total\n";
40
		echo "Missing: " . count( $this->get() ) . "\n";
41
	}
42
43
	/**
44
	 * Scans the file for any invocations that depend on missing or different classes, methods, properties and functions
45
	 */
46
	public function check_file_compatibility( $file_path ) {
47
		$source = file_get_contents( $file_path );
48
		$parser = ( new ParserFactory() )->create( ParserFactory::PREFER_PHP7 );
49
		try {
50
			$ast    = $parser->parse( $source );
51
		} 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...
52
			echo "Parse error: {$error->getMessage()}\n";
53
			return;
54
		}
55
56
		// $dumper = new NodeDumper;
57
		// echo $dumper->dump($ast) . "\n";
58
59
		// before parsing, make sure we try to resolve class names
60
		$traverser    = new NodeTraverser();
61
		$nameResolver = new NameResolver();
62
		$traverser->addVisitor( $nameResolver );
63
64
		// Resolve names
65
		$ast = $traverser->traverse( $ast );
66
67
		$traverser         = new NodeTraverser();
68
		$invocations       = new Invocations();
69
		$invocation_finder = new Invocations\Visitor( $file_path, $invocations );
70
		$traverser->addVisitor( $invocation_finder );
71
		$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...
72
73
		print_r($invocations);
74
		$invocations->print();
75
		// return $invocations;
76
77
		// $dumper = new NodeDumper;
78
		// echo $dumper->dump($ast) . "\n";
79
80
		// TODO: return a list of warnings and errors
81
82
		/**
83
		 * Scan every invocation to see if it depends on a Difference
84
		 */
85
		$warnings = new Warnings();
86
		foreach( $invocations->get() as $invocation ) {
87
			foreach( $this->get() as $difference ) {
88
				// $warning = $
89
				$difference->find_invocation_warnings( $invocation, $warnings );
90
			}
91
		}
92
93
		return $warnings;
94
	}
95
}
96