Completed
Push — add/legacy-files ( 4cc789...bef25d )
by
unknown
157:43 queued 148:20
created

Differences::find()   F

Complexity

Conditions 22
Paths 866

Size

Total Lines 92

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
nc 866
nop 3
dl 0
loc 92
rs 0.186
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
	private function slashit( $path ) {
13
		$path .= ( substr( $path, -1 ) == '/' ? '' : '/' );
14
		return $path;
15
	}
16
17
	public function find( $new_declarations, $prev_declarations, $new_root = null ) {
18
		if ( $new_root ) {
19
			$new_root = $this->slashit( $new_root );
20
		} else {
21
			echo "Warning: calling find() without \$new_root means we can't detect if files are stubbed in the new release\n";
22
		}
23
		$total = 0;
24
		$missing_total = 0;
25
		$moved_total = 0;
26
		$moved_with_empty_file_total = 0;
27
		// for each declaration, see if it exists in the current analyzer's declarations
28
		// if not, add it to the list of differences - either as missing or different
29
		foreach ( $prev_declarations->get() as $prev_declaration ) {
30
			$matched = false;
31
			$moved = false;
32
			$moved_with_empty_file = false;
33
			foreach ( $new_declarations->get() as $new_declaration ) {
34
				if ( $prev_declaration->match( $new_declaration ) ) {
35
					// echo "Comparing " . $prev_declaration->path . " to " . $new_declaration->path . "\n";
36
					if ( $prev_declaration->path !== $new_declaration->path ) {
37
38
						// if a file exists at the old location, and the new method is (we assume) autoloaded,
39
						// do not warn.
40
						// TODO: since functions are not autoloaded, we should probably still warn for them?
41
						if ( $new_root && file_exists( $new_root . $prev_declaration->path ) ) {
42
							$moved_with_empty_file = true;
43
							$moved_with_empty_file_total += 1;
44
						} else {
45
							$moved = true;
46
						}
47
					}
48
					$matched = true;
49
					break;
50
				} elseif ( $prev_declaration->partial_match( $new_declaration ) ) {
51
					// TODO this is to catch things like function args changed, method the same
52
				}
53
			}
54
55
			// do not add warnings for $moved_with_empty_file
56
			if ( $matched && $moved_with_empty_file ) {
57
				echo "Declaration " . $prev_declaration->display_name() . " moved from " . $prev_declaration->path . " to " . $new_declaration->path . " with matching empty file at original location\n";
0 ignored issues
show
Bug introduced by
The variable $new_declaration seems to be defined by a foreach iteration on line 33. 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...
58
			}
59
60
			if ( $matched && $moved ) {
61
				switch ( $prev_declaration->type() ) {
62
					case 'class':
63
						$this->add( new Differences\Class_Moved( $prev_declaration, $new_declaration ) );
64
						break;
65
					case 'method':
66
						$this->add( new Differences\Class_Method_Moved( $prev_declaration, $new_declaration ) );
67
						break;
68
					case 'property':
69
						$this->add( new Differences\Class_Property_Moved( $prev_declaration, $new_declaration ) );
70
						break;
71
					case 'function':
72
						$this->add( new Differences\Function_Moved( $prev_declaration, $new_declaration ) );
73
						break;
74
					default:
75
						echo 'Unknown moved type ' . $prev_declaration->type() . "\n";
76
				}
77
				$moved_total += 1;
78
			}
79
80
			if ( ! $matched ) {
81
				switch ( $prev_declaration->type() ) {
82
					case 'class':
83
						$this->add( new Differences\Class_Missing( $prev_declaration ) );
84
						break;
85
					case 'method':
86
						$this->add( new Differences\Class_Method_Missing( $prev_declaration ) );
87
						break;
88
					case 'property':
89
						$this->add( new Differences\Class_Property_Missing( $prev_declaration ) );
90
						break;
91
					case 'function':
92
						$this->add( new Differences\Function_Missing( $prev_declaration ) );
93
						break;
94
					default:
95
						echo 'Unknown unmatched type ' . $prev_declaration->type() . "\n";
96
				}
97
				$missing_total += 1;
98
			}
99
100
			$total += 1;
101
		}
102
103
		echo "Total Declarations: $total\n";
104
		echo 'Total Differences: ' . count( $this->get() ) . "\n";
105
		echo 'Moved: ' . $moved_total . "\n";
106
		echo 'Moved with stubbed file: ' . $moved_with_empty_file_total . "\n";
107
		echo 'Missing: ' . $missing_total . "\n";
108
	}
109
}
110