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

Differences   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 17
lcom 0
cbo 9

1 Method

Rating   Name   Duplication   Size   Complexity  
D find() 0 64 17
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
			$moved = false;
19
			foreach ( $new_declarations->get() as $new_declaration ) {
20
				if ( $prev_declaration->match( $new_declaration ) ) {
21
					// echo "Comparing " . $prev_declaration->path . " to " . $new_declaration->path . "\n";
22
					if ( $prev_declaration->path !== $new_declaration->path ) {
23
						$moved = true;
24
					}
25
					$matched = true;
26
					break;
27
				} elseif ( $prev_declaration->partial_match( $new_declaration ) ) {
28
					// TODO this is to catch things like function args changed, method the same
29
				}
30
			}
31
32
			if ( $matched && $moved ) {
33
				switch ( $prev_declaration->type() ) {
34
					case 'class':
35
						$this->add( new Differences\Class_Moved( $prev_declaration, $new_declaration ) );
0 ignored issues
show
Bug introduced by
The variable $new_declaration seems to be defined by a foreach iteration on line 19. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
36
						break;
37
					case 'method':
38
						$this->add( new Differences\Class_Method_Moved( $prev_declaration, $new_declaration ) );
39
						break;
40
					case 'property':
41
						$this->add( new Differences\Class_Property_Moved( $prev_declaration, $new_declaration ) );
42
						break;
43
					case 'function':
44
						$this->add( new Differences\Function_Moved( $prev_declaration, $new_declaration ) );
45
						break;
46
					default:
47
						echo 'Unknown moved type ' . $prev_declaration->type() . "\n";
48
				}
49
			}
50
51
			if ( ! $matched ) {
52
				switch ( $prev_declaration->type() ) {
53
					case 'class':
54
						$this->add( new Differences\Class_Missing( $prev_declaration ) );
55
						break;
56
					case 'method':
57
						$this->add( new Differences\Class_Method_Missing( $prev_declaration ) );
58
						break;
59
					case 'property':
60
						$this->add( new Differences\Class_Property_Missing( $prev_declaration ) );
61
						break;
62
					case 'function':
63
						$this->add( new Differences\Function_Missing( $prev_declaration ) );
64
						break;
65
					default:
66
						echo 'Unknown unmatched type ' . $prev_declaration->type() . "\n";
67
				}
68
			}
69
70
			$total += 1;
71
		}
72
73
		echo "Total: $total\n";
74
		echo 'Missing: ' . count( $this->get() ) . "\n";
75
	}
76
}
77