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

Dependencies   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 77
Duplicated Lines 67.53 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 52
loc 77
rs 10
c 0
b 0
f 0
wmc 17
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generate() 0 16 5
A slashit() 0 4 2
A declaration_summary() 26 26 5
A external_file_summary() 26 26 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Automattic\Jetpack\Analyzer;
4
5
/**
6
 * This collects dependencies of invocations in Codebase A to declarations in codebase B
7
 */
8
class Dependencies extends PersistentList {
9
	function generate( $invocations, $declarations, $invocation_root = null ) {
10
		if ( $invocation_root ) {
11
			$invocation_root = $this->slashit( $invocation_root );
12
		}
13
14
		/**
15
		 * Scan every invocation to see if it depends on a declaration
16
		 */
17
		foreach( $invocations->get() as $invocation ) {
18
			foreach( $declarations->get() as $declaration ) {
19
				if ( $invocation->depends_on( $declaration ) ) {
20
					$this->add( new Dependencies\Dependency( $invocation, $declaration, $invocation_root ) );
21
				}
22
			}
23
		}
24
	}
25
26
	private function slashit( $path ) {
27
		$path .= ( substr( $path, -1 ) == '/' ? '' : '/' );
28
		return $path;
29
	}
30
31 View Code Duplication
	public function declaration_summary() {
32
		if ( $this->count() === 0 ) {
33
			return '';
34
		}
35
36
		// assoc array of declarations and counts
37
		$summary = array();
38
		foreach( $this->get() as $dependency ) {
39
			$unique_issue_key = $dependency->declaration->display_name();
40
41
			if ( ! isset( $summary[$unique_issue_key] ) ) {
42
				$summary[$unique_issue_key] = 0;
43
			}
44
45
			$summary[$unique_issue_key] += 1;
46
		}
47
48
		arsort( $summary );
49
50
		$summary_string = '';
51
		foreach( $summary as $issue => $count ) {
52
			$summary_string .= "$issue,$count\n";
53
		}
54
55
		return $summary_string;
56
	}
57
58 View Code Duplication
	public function external_file_summary() {
59
		if ( $this->count() === 0 ) {
60
			return '';
61
		}
62
63
		// assoc array of files and counts
64
		$summary = array();
65
		foreach( $this->get() as $dependency ) {
66
			$unique_issue_key = $dependency->full_path();
67
68
			if ( ! isset( $summary[$unique_issue_key] ) ) {
69
				$summary[$unique_issue_key] = 0;
70
			}
71
72
			$summary[$unique_issue_key] += 1;
73
		}
74
75
		arsort( $summary );
76
77
		$summary_string = '';
78
		foreach( $summary as $issue => $count ) {
79
			$summary_string .= "$issue,$count\n";
80
		}
81
82
		return $summary_string;
83
	}
84
}
85