Passed
Push — develop ( 45600c...ff69f6 )
by Paul
03:16
created

DirectoryParser::parse()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
rs 8.2222
cc 7
eloc 11
nc 4
nop 2
1
<?php
2
3
namespace PhpUnitGen\Parser;
4
5
use League\Flysystem\FilesystemInterface;
6
use PhpUnitGen\Configuration\ConsoleConfigInterface;
7
use PhpUnitGen\Exception\InvalidArgumentException;
8
use PhpUnitGen\Exception\ParsingException;
9
use PhpUnitGen\Model\DirectoryModel;
10
use PhpUnitGen\Model\ModelInterface\DirectoryModelInterface;
11
use PhpUnitGen\Parser\ParserInterface\DirectoryParserInterface;
12
use PhpUnitGen\Parser\ParserInterface\PhpFileParserInterface;
13
14
/**
15
 * Class DirectoryParser.
16
 *
17
 * @author     Paul Thébaud <[email protected]>.
18
 * @copyright  2017-2018 Paul Thébaud <[email protected]>.
19
 * @license    https://opensource.org/licenses/MIT The MIT license.
20
 * @link       https://github.com/paul-thebaud/phpunit-generator
21
 * @since      Class available since Release 2.0.0.
22
 */
23
class DirectoryParser implements DirectoryParserInterface
24
{
25
    /**
26
     * @var ConsoleConfigInterface $config The configuration to use.
27
     */
28
    private $config;
29
30
    /**
31
     * @var FilesystemInterface $fileSystem The file system to use.
32
     */
33
    private $fileSystem;
34
35
    /**
36
     * @var PhpFileParserInterface $phpFileParser The php file parser to use.
37
     */
38
    private $phpFileParser;
39
40
    /**
41
     * DirectoryParser constructor.
42
     *
43
     * @param ConsoleConfigInterface $config        A config instance.
44
     * @param FilesystemInterface    $fileSystem    A file system instance.
45
     * @param PhpFileParserInterface $phpFileParser A php file parser instance.
46
     */
47
    public function __construct(
48
        ConsoleConfigInterface $config,
49
        FilesystemInterface $fileSystem,
50
        PhpFileParserInterface $phpFileParser
51
    ) {
52
        $this->config        = $config;
53
        $this->fileSystem    = $fileSystem;
54
        $this->phpFileParser = $phpFileParser;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function parse(string $sourceDirectory, string $targetDirectory): DirectoryModelInterface
61
    {
62
        if (! $this->fileSystem->has($sourceDirectory) || ! $this->fileSystem->getMimetype($sourceDirectory) === 'directory') {
63
            throw new ParsingException(sprintf('The source directory "%s" does not exist.', $sourceDirectory));
64
        }
65
66
        $directoryModel = new DirectoryModel();
67
        $directoryModel->setSourceDirectory($sourceDirectory);
68
        $directoryModel->setTargetDirectory($targetDirectory);
69
70
        foreach ($this->fileSystem->listContents($sourceDirectory) as $file) {
71
            if ($file['type'] === 'file'
72
                && @preg_match($this->config->getIncludeRegex(), $file['path']) === 1
73
                && @preg_match($this->config->getExcludeRegex(), $file['path']) === 0
74
            ) {
75
                $directoryModel->addPhpFile($this->phpFileParser->parse($this->fileSystem->read($file['path'])));
0 ignored issues
show
Bug introduced by
It seems like $this->fileSystem->read($file['path']) can also be of type false; however, parameter $code of PhpUnitGen\Parser\Parser...arserInterface::parse() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
                $directoryModel->addPhpFile($this->phpFileParser->parse(/** @scrutinizer ignore-type */ $this->fileSystem->read($file['path'])));
Loading history...
76
            }
77
        }
78
79
        return $directoryModel;
80
    }
81
}