Failed Conditions
Push — master ( 17cdc3...604e06 )
by Alexander
05:14
created

MissingTestsCommand::getPath()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.2
cc 4
eloc 6
nc 2
nop 1
crap 20
1
<?php
2
/**
3
 * This file is part of the Code-Insight library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/code-insight
9
 */
10
11
namespace ConsoleHelpers\CodeInsight\Command;
12
13
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Finder\Finder;
18
19
class MissingTestsCommand extends AbstractCommand
20
{
21
22
	/**
23
	 * {@inheritdoc}
24
	 */
25
	protected function configure()
26
	{
27
		$this
28
			->setName('missing-tests')
29
			->setDescription('Shows classes without tests')
30
			->addArgument(
31
				'src-path',
32
				InputArgument::OPTIONAL,
33
				'Path to folder with source code',
34
				'src'
35
			)
36
			->addArgument(
37
				'tests-path',
38
				InputArgument::OPTIONAL,
39
				'Path to folder with tests',
40
				'tests'
41
			);
42
	}
43
44
	/**
45
	 * {@inheritdoc}
46
	 */
47
	protected function execute(InputInterface $input, OutputInterface $output)
48
	{
49
		$src_path = $this->getPath('src-path');
50
		$tests_path = $this->getPath('tests-path');
51
52
		$finder = new Finder();
53
		$finder->files()->name('*.php')->notName('/^(I[A-Z]|Abstract[A-Z])/')->in($src_path);
54
55
		$missing_tests = array();
56
		$src_path_parent_path = dirname($src_path) . '/';
57
58
		foreach ( $finder as $source_file ) {
59
			$source_file_path = $source_file->getPathname();
60
			$test_file_path = preg_replace(
61
				'/^' . preg_quote($src_path, '/') . '(.*)\.php$/',
62
				$tests_path . '$1Test.php',
63
				$source_file_path
64
			);
65
66
			if ( !file_exists($test_file_path) ) {
67
				$missing_tests[] = str_replace($src_path_parent_path, '', $source_file_path);
68
			}
69
		}
70
71
		if ( !$missing_tests ) {
72
			$this->io->writeln('<info>Tests for all source files are found.</info>');
73
		}
74
		else {
75
			$this->io->writeln('<error>Tests for following source files are missing:</error>');
76
77
			foreach ( $missing_tests as $missing_test ) {
78
				$this->io->writeln(' * ' . $missing_test);
79
			}
80
		}
81
	}
82
83
}
84