Completed
Push — renovate/mocha-8.x ( 07030e...e8e64c )
by
unknown
28:17 queued 19:09
created

AutoloadProcessor::processClassmap()   C

Complexity

Conditions 13
Paths 18

Size

Total Lines 49

Duplication

Lines 20
Ratio 40.82 %

Importance

Changes 0
Metric Value
cc 13
nc 18
nop 2
dl 20
loc 49
rs 6.6166
c 0
b 0
f 0

How to fix   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 // phpcs:ignore WordPress.Files.FileName
2
/**
3
 * Autoload Processor.
4
 *
5
 * @package automattic/jetpack-autoloader
6
 */
7
8
// phpcs:disable WordPress.Files.FileName.InvalidClassFileName
9
// phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
10
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
11
// phpcs:disable WordPress.NamingConventions.ValidVariableName.InterpolatedVariableNotSnakeCase
12
// phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
13
// phpcs:disable WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
14
15
namespace Automattic\Jetpack\Autoloader;
16
17
use Composer\Util\Filesystem;
18
19
/**
20
 * Class AutoloadProcessor.
21
 */
22
class AutoloadProcessor {
23
24
	/**
25
	 * A callable for scanning a directory for all of its classes.
26
	 *
27
	 * @var callable
28
	 */
29
	private $classmapScanner;
30
31
	/**
32
	 * A callable for transforming a path into one to be used in code.
33
	 *
34
	 * @var callable
35
	 */
36
	private $pathCodeTransformer;
37
38
	/**
39
	 * The constructor.
40
	 *
41
	 * @param callable $classmapScanner A callable for scanning a directory for all of its classes.
42
	 * @param callable $pathCodeTransformer A callable for transforming a path into one to be used in code.
43
	 */
44
	public function __construct( $classmapScanner, $pathCodeTransformer ) {
45
		$this->classmapScanner     = $classmapScanner;
46
		$this->pathCodeTransformer = $pathCodeTransformer;
47
	}
48
49
	/**
50
	 * Processes the classmap autoloads into a relative path format including the version for each file.
51
	 *
52
	 * @param array $autoloads The autoloads we are processing.
53
	 * @param bool  $scanPsrPackages Whether or not PSR packages should be converted to a classmap.
54
	 *
55
	 * @return array $processed
56
	 */
57
	public function processClassmap( $autoloads, $scanPsrPackages ) {
58
		// We can't scan PSR packages if we don't actually have any.
59
		if ( empty( $autoloads['psr-4'] ) ) {
60
			$scanPsrPackages = false;
61
		}
62
63
		if ( empty( $autoloads['classmap'] ) && ! $scanPsrPackages ) {
64
			return null;
65
		}
66
67
		$excludedClasses = null;
68
		if ( ! empty( $autoloads['exclude-from-classmap'] ) ) {
69
			$excludedClasses = '{(' . implode( '|', $autoloads['exclude-from-classmap'] ) . ')}';
70
		}
71
72
		$processed = array();
73
74
		if ( $scanPsrPackages ) {
75
			foreach ( $autoloads['psr-4'] as $namespace => $sources ) {
76
				$namespace = empty( $namespace ) ? null : $namespace;
77
78 View Code Duplication
				foreach ( $sources as $source ) {
79
					$classmap = call_user_func( $this->classmapScanner, $source['path'], $excludedClasses, $namespace );
80
81
					foreach ( $classmap as $class => $path ) {
82
						$processed[ $class ] = array(
83
							'version' => $source['version'],
84
							'path'    => call_user_func( $this->pathCodeTransformer, $path ),
85
						);
86
					}
87
				}
88
			}
89
		}
90
91
		if ( ! empty( $autoloads['classmap'] ) ) {
92 View Code Duplication
			foreach ( $autoloads['classmap'] as $package ) {
93
				$classmap = call_user_func( $this->classmapScanner, $package['path'], $excludedClasses, null );
94
95
				foreach ( $classmap as $class => $path ) {
96
					$processed[ $class ] = array(
97
						'version' => $package['version'],
98
						'path'    => call_user_func( $this->pathCodeTransformer, $path ),
99
					);
100
				}
101
			}
102
		}
103
104
		return $processed;
105
	}
106
107
	/**
108
	 * Processes the PSR-4 autoloads into a relative path format including the version for each file.
109
	 *
110
	 * @param array $autoloads The autoloads we are processing.
111
	 * @param bool  $scanPsrPackages Whether or not PSR packages should be converted to a classmap.
112
	 *
113
	 * @return array $processed
114
	 */
115
	public function processPsr4Packages( $autoloads, $scanPsrPackages ) {
116
		if ( $scanPsrPackages || empty( $autoloads['psr-4'] ) ) {
117
			return null;
118
		}
119
120
		$processed = array();
121
122
		foreach ( $autoloads['psr-4'] as $namespace => $packages ) {
123
			$namespace = empty( $namespace ) ? null : $namespace;
124
			$paths     = array();
125
126
			foreach ( $packages as $package ) {
127
				$paths[] = call_user_func( $this->pathCodeTransformer, $package['path'] );
128
			}
129
130
			$processed[ $namespace ] = array(
131
				'version' => $package['version'],
0 ignored issues
show
Bug introduced by
The variable $package seems to be defined by a foreach iteration on line 126. 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...
132
				'path'    => $paths,
133
			);
134
		}
135
136
		return $processed;
137
	}
138
139
	/**
140
	 * Processes the file autoloads into a relative format including the version for each file.
141
	 *
142
	 * @param array $autoloads The autoloads we are processing.
143
	 *
144
	 * @return array|null $processed
145
	 */
146
	public function processFiles( $autoloads ) {
147
		if ( empty( $autoloads['files'] ) ) {
148
			return null;
149
		}
150
151
		$processed = array();
152
153
		foreach ( $autoloads['files'] as $file_id => $package ) {
154
			$processed[ $file_id ] = array(
155
				'version' => $package['version'],
156
				'path'    => call_user_func( $this->pathCodeTransformer, $package['path'] ),
157
			);
158
		}
159
160
		return $processed;
161
	}
162
}
163