TypeFilter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B getIterator() 0 21 7
1
<?php
2
/**
3
 * This program is free software. It comes without any warranty, to
4
 * the extent permitted by applicable law. You can redistribute it
5
 * and/or modify it under the terms of the Do What The Fuck You Want
6
 * To Public License, Version 2, as published by Sam Hocevar. See
7
 * http://www.wtfpl.net/ for more details.
8
 */
9
10
declare(strict_types = 1);
11
12
namespace hanneskod\classtools\Iterator\Filter;
13
14
use hanneskod\classtools\Iterator\ClassIterator;
15
use hanneskod\classtools\Iterator\Filter;
16
use ReflectionException;
17
18
/**
19
 * Filter classes of a spefcified type
20
 *
21
 * @author Hannes Forsgård <[email protected]>
22
 */
23
final class TypeFilter extends ClassIterator implements Filter
24
{
25
    use FilterTrait;
26
27
    /**
28
     * @var string
29
     */
30
    private $typename;
31
32
    public function __construct(string $typename)
33
    {
34
        parent::__construct();
35
        $this->typename = $typename;
36
    }
37
38
    public function getIterator(): iterable
39
    {
40
        foreach ($this->getBoundIterator() as $className => $reflectedClass) {
41
            try {
42
                if ($reflectedClass->implementsInterface($this->typename)) {
43
                    yield $className => $reflectedClass;
44
                }
45
            } catch (\ReflectionException $e) {
46
                try {
47
                    if (
48
                        $reflectedClass->isSubclassOf($this->typename)
49
                        || $reflectedClass->getName() == $this->typename
50
                    ) {
51
                        yield $className => $reflectedClass;
52
                    }
53
                } catch (\ReflectionException $e) {
54
                    // Nope
55
                }
56
            }
57
        }
58
    }
59
}
60