ClassFinder::getIterator()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.6111
cc 5
nc 5
nop 0
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Borodulin\Finder;
6
7
use Borodulin\Finder\Exception\ParseException;
8
9
class ClassFinder implements FinderInterface
10
{
11
    /**
12
     * @var FinderInterface
13
     */
14
    private $fileFinder;
15
    /**
16
     * @var callable|null
17
     */
18
    private $classExtractor;
19
20 1
    public function __construct(FinderInterface $fileFinder = null, callable $classExtractor = null)
21
    {
22 1
        $this->fileFinder = $fileFinder ?: new FileFinder();
23 1
        $this->classExtractor = $classExtractor ?: new ClassExtractor();
24 1
    }
25
26 1
    public function addPath(string $path): FinderInterface
27
    {
28 1
        $this->fileFinder->addPath($path);
29
30 1
        return $this;
31
    }
32
33 1
    public function getIterator(): \Traversable
34
    {
35 1
        foreach ($this->fileFinder as $fileName) {
36 1
            if ('.php' === substr($fileName, -4)) {
37
                try {
38 1
                    $class = \call_user_func($this->classExtractor, $fileName);
39 1
                    if ($class) {
40 1
                        yield $class;
41
                    }
42 1
                } catch (ParseException $exception) {
43
                    // skip files with parse error
44
                }
45
            }
46
        }
47 1
    }
48
}
49