RoboFile::test()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2 View Code Duplication
if ( ! function_exists('glob_recursive'))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
3
{
4
	// Does not support flag GLOB_BRACE
5
	function glob_recursive($pattern, $flags = 0)
0 ignored issues
show
Best Practice introduced by
The function glob_recursive() has been defined more than once; this definition is ignored, only the first definition in build/update_header_comments.php (L14-24) is considered.

This check looks for functions that have already been defined in other files.

Some Codebases, like WordPress, make a practice of defining functions multiple times. This may lead to problems with the detection of function parameters and types. If you really need to do this, you can mark the duplicate definition with the @ignore annotation.

/**
 * @ignore
 */
function getUser() {

}

function getUser($id, $realm) {

}

See also the PhpDoc documentation for @ignore.

Loading history...
6
	{
7
		$files = glob($pattern, $flags);
8
9
		foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
10
		{
11
			$files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
12
		}
13
14
		return $files;
15
	}
16
}
17
18
/**
19
 * This is project's console commands configuration for Robo task runner.
20
 *
21
 * @see http://robo.li/
22
 */
23
class RoboFile extends \Robo\Tasks {
24
25
	/**
26
	 * Directories used by analysis tools
27
	 *
28
	 * @var array
29
	 */
30
	protected $taskDirs = [
31
		'build/logs',
32
		'build/pdepend',
33
		'build/phpdox',
34
	];
35
36
	/**
37
	 * Directories to remove with the clean task
38
	 *
39
	 * @var array
40
	 */
41
	protected $cleanDirs = [
42
		'coverage',
43
		'apiDocumentation',
44
		'phpdoc',
45
		'build/logs',
46
		'build/phpdox',
47
		'build/pdepend'
48
	];
49
50
51
	/**
52
	 * Do static analysis tasks
53
	 */
54
	public function analyze()
55
	{
56
		$this->prepare();
57
		$this->lint();
58
		$this->phploc(TRUE);
59
		$this->phpcs(TRUE);
60
		$this->dependencyReport();
61
		$this->phpcpdReport();
62
	}
63
64
	/**
65
	 * Run all tests, generate coverage, generate docs, generate code statistics
66
	 */
67
	public function build()
68
	{
69
		$this->analyze();
70
		$this->coverage();
71
		$this->docs();
72
	}
73
74
	/**
75
	 * Cleanup temporary files
76
	 */
77
	public function clean()
78
	{
79
		// So the task doesn't complain,
80
		// make any 'missing' dirs to cleanup
81
		array_map(function ($dir) {
82
			if ( ! is_dir($dir))
83
			{
84
				`mkdir -p {$dir}`;
85
			}
86
		}, $this->cleanDirs);
87
88
		$this->_cleanDir($this->cleanDirs);
89
		$this->_deleteDir($this->cleanDirs);
90
	}
91
92
	/**
93
	 * Run unit tests and generate coverage reports
94
	 */
95
	public function coverage()
96
	{
97
		$this->_run(['phpdbg -qrr -- vendor/bin/phpunit -c build']);
98
	}
99
100
	/**
101
	 * Generate documentation with phpdox
102
	 */
103
	public function docs()
104
	{
105
		$this->_run(['vendor/bin/phpdox']);
106
	}
107
108
	/**
109
	 * Verify that source files are valid
110
	 */
111
	public function lint()
112
	{
113
		$files = $this->getAllSourceFiles();
114
115
		$chunks = array_chunk($files, (int)`getconf _NPROCESSORS_ONLN`);
116
117
		foreach($chunks as $chunk)
118
		{
119
			$this->parallelLint($chunk);
120
		}
121
	}
122
123
	/**
124
	 * Run the phpcs tool
125
	 *
126
	 * @param bool $report - if true, generates reports instead of direct output
127
	 */
128
	public function phpcs($report = FALSE)
129
	{
130
		$dir = __DIR__;
0 ignored issues
show
Unused Code introduced by
$dir is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
132
		$report_cmd_parts = [
133
			'vendor/bin/phpcs',
134
			"--standard=./build/CodeIgniter",
135
			"--report-checkstyle=./build/logs/phpcs.xml",
136
		];
137
138
		$normal_cmd_parts = [
139
			'vendor/bin/phpcs',
140
			"--standard=./build/CodeIgniter",
141
		];
142
143
		$cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts;
144
145
		$this->_run($cmd_parts);
146
	}
147
148
	/**
149
	 * Run the phploc tool
150
	 *
151
	 * @param bool $report - if true, generates reports instead of direct output
152
	 */
153
	public function phploc($report = FALSE)
154
	{
155
		// Command for generating reports
156
		$report_cmd_parts = [
157
			'vendor/bin/phploc',
158
			'--count-tests',
159
			'--log-csv=build/logs/phploc.csv',
160
			'--log-xml=build/logs/phploc.xml',
161
			'src',
162
			'tests'
163
		];
164
165
		// Command for generating direct output
166
		$normal_cmd_parts = [
167
			'vendor/bin/phploc',
168
			'--count-tests',
169
			'src',
170
			'tests'
171
		];
172
173
		$cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts;
174
175
		$this->_run($cmd_parts);
176
	}
177
178
	/**
179
	 * Create temporary directories
180
	 */
181
	public function prepare()
182
	{
183
		array_map([$this, '_mkdir'], $this->taskDirs);
184
	}
185
186
	/**
187
	 * Lint php files and run unit tests
188
	 */
189
	public function test()
190
	{
191
		$this->lint();
192
		$this->taskPhpUnit()
0 ignored issues
show
Bug introduced by
The method configFile does only exist in Robo\Task\Testing\PHPUnit, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
193
			->configFile('build/phpunit.xml')
194
			->run();
195
		$this->_run(["php tests/index.php"]);
196
	}
197
198
	/**
199
	 * Watches for file updates, and automatically runs appropriate actions
200
	 */
201
	public function watch()
202
	{
203
		$this->taskWatch()
0 ignored issues
show
Bug introduced by
The method monitor does only exist in Robo\Task\Base\Watch, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
204
			->monitor('composer.json', function() {
205
				$this->taskComposerUpdate()->run();
206
			})
207
			->monitor('src', function () {
208
				$this->taskExec('test')->run();
209
			})
210
			->monitor('tests', function () {
211
				$this->taskExec('test')->run();
212
			})
213
			->run();
214
	}
215
216
	/**
217
	 * Create pdepend reports
218
	 */
219
	protected function dependencyReport()
220
	{
221
		$cmd_parts = [
222
			'vendor/bin/pdepend',
223
			'--jdepend-xml=build/logs/jdepend.xml',
224
			'--jdepend-chart=build/pdepend/dependencies.svg',
225
			'--overview-pyramid=build/pdepend/overview-pyramid.svg',
226
			'src'
227
		];
228
		$this->_run($cmd_parts);
229
	}
230
231
	/**
232
	 * Get the total list of source files, including tests
233
	 *
234
	 * @return array
235
	 */
236
	protected function getAllSourceFiles()
237
	{
238
		$files = array_merge(
239
			glob_recursive('build/*.php'),
240
			glob_recursive('src/*.php'),
241
			glob_recursive('tests/*.php'),
242
			glob('*.php')
243
		);
244
245
		sort($files);
246
247
		return $files;
248
	}
249
250
	/**
251
	 * Run php's linter in one parallel task for the passed chunk
252
	 *
253
	 * @param array $chunk
254
	 */
255
	protected function parallelLint(array $chunk)
256
	{
257
		$task = $this->taskParallelExec()
0 ignored issues
show
Bug introduced by
The method timeout does only exist in Robo\Task\Base\ParallelExec, but not in Robo\Collection\CollectionBuilder.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
258
			->timeout(5)
259
			->printed(FALSE);
260
261
		foreach($chunk as $file)
262
		{
263
			$task = $task->process("php -l {$file}");
264
		}
265
266
		$task->run();
267
	}
268
269
	/**
270
	 * Generate copy paste detector report
271
	 */
272
	protected function phpcpdReport()
273
	{
274
		$cmd_parts = [
275
			'vendor/bin/phpcpd',
276
			'--log-pmd build/logs/pmd-cpd.xml',
277
			'src'
278
		];
279
		$this->_run($cmd_parts);
280
	}
281
282
	/**
283
	 * Shortcut for joining an array of command arguments
284
	 * and then running it
285
	 *
286
	 * @param array $cmd_parts - command arguments
287
	 * @param string $join_on - what to join the command arguments with
288
	 */
289
	protected function _run(array $cmd_parts, $join_on = ' ')
290
	{
291
		$this->taskExec(implode($join_on, $cmd_parts))->run();
292
	}
293
}
294