Passed
Pull Request — master (#5)
by mark
07:42
created

ServiceTask   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 3
dl 0
loc 103
ccs 0
cts 40
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A clearBuffer() 0 3 1
A collect() 0 15 3
B collectServices() 0 24 5
B addServices() 0 23 5
1
<?php
2
namespace Burzum\Cake\Generator\Task;
3
4
use Cake\Core\App;
5
use Cake\Core\Plugin;
6
use Cake\Filesystem\Folder;
7
use IdeHelper\Generator\Task\TaskInterface;
8
9
class ServiceTask implements TaskInterface {
10
11
	/**
12
	 * @var array
13
	 */
14
	protected $aliases = [
15
		'\Burzum\Cake\Service\ServiceAwareTrait::loadService(0)',
16
		'\ServiceAwareTrait::loadService(0)',
17
	];
18
19
	/**
20
	 * Buffer
21
	 *
22
	 * @var array|null
23
	 */
24
	protected static $services;
25
26
	/**
27
	 * @return void
28
	 */
29
	public static function clearBuffer() {
30
		static::$services = null;
31
	}
32
33
	/**
34
	 * @return array
35
	 */
36
	public function collect() {
37
		$map = [];
38
39
		$services = $this->collectServices();
40
		foreach ($services as $service => $className) {
41
			$map[$service] = '\\' . $className . '::class';
42
		}
43
44
		$result = [];
45
		foreach ($this->aliases as $alias) {
46
			$result[$alias] = $map;
47
		}
48
49
		return $result;
50
	}
51
52
	/**
53
	 * @return string[]
54
	 */
55
	protected function collectServices() {
56
		if (static::$services !== null) {
57
			return static::$services;
58
		}
59
60
		$services = [];
61
62
		$folders = App::path('Service');
63
		foreach ($folders as $folder) {
64
			$services = $this->addServices($services, $folder);
65
		}
66
67
		$plugins = Plugin::loaded();
68
		foreach ($plugins as $plugin) {
0 ignored issues
show
Bug introduced by
The expression $plugins of type boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
69
			$folders = App::path('Service', $plugin);
70
			foreach ($folders as $folder) {
71
				$services = $this->addServices($services, $folder, $plugin);
72
			}
73
		}
74
75
		static::$services = $services;
76
77
		return $services;
78
	}
79
80
	/**
81
	 * @param array $services
82
	 * @param string $folder
83
	 * @param string|null $plugin
84
	 *
85
	 * @return string[]
86
	 */
87
	protected function addServices(array $services, $folder, $plugin = null) {
88
		$folderContent = (new Folder($folder))->read(Folder::SORT_NAME, true);
89
90
		foreach ($folderContent[1] as $file) {
91
			preg_match('/^(.+)Service\.php$/', $file, $matches);
92
			if (!$matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
93
				continue;
94
			}
95
			$service = $matches[1];
96
			if ($plugin) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $plugin of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
97
				$service = $plugin . '.' . $service;
98
			}
99
100
			$className = App::className($service, 'Service', 'Service');
101
			if (!$className) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $className of type string|false is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
102
				continue;
103
			}
104
105
			$services[$service] = $className;
106
		}
107
108
		return $services;
109
	}
110
111
}
112