ReflectionIterator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
c 3
b 1
f 0
lcom 1
cbo 0
dl 0
loc 87
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 4 1
A next() 0 4 1
A rewind() 0 4 1
A valid() 0 4 1
A __construct() 0 6 1
A current() 0 7 1
A getClassFullNameFromFile() 0 14 1
A createIterator() 0 13 1
1
<?php
2
3
/**
4
 * This file is part of expect package.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
namespace expect\package;
12
13
use FilesystemIterator;
14
use Iterator;
15
use RecursiveDirectoryIterator;
16
use RecursiveIteratorIterator;
17
use ReflectionClass;
18
use SplFileInfo;
19
20
class ReflectionIterator implements Iterator
21
{
22
    private $iterator;
23
    private $namespace;
24
    private $namespaceDirectory;
25
26
    /**
27
     * @param string $namespace
28
     * @param string $namespaceDirectory
29
     */
30
    public function __construct($namespace, $namespaceDirectory)
31
    {
32
        $this->namespace = $namespace;
33
        $this->namespaceDirectory = $namespaceDirectory;
34
        $this->iterator = $this->createIterator($this->namespaceDirectory);
35
    }
36
37
    /**
38
     * @return \expect\package\MatcherClass
39
     */
40
    public function current()
41
    {
42
        $classFile = $this->iterator->current();
43
        $className = $this->getClassFullNameFromFile($classFile);
44
45
        return new ReflectionClass($className);
46
    }
47
48
    public function key()
49
    {
50
        return realpath($this->iterator->key());
51
    }
52
53
    public function next()
54
    {
55
        $this->iterator->next();
56
    }
57
58
    public function rewind()
59
    {
60
        $this->iterator->rewind();
61
    }
62
63
    public function valid()
64
    {
65
        return $this->iterator->valid();
66
    }
67
68
    /**
69
     * @param SplFileInfo $file
70
     *
71
     * @return mixed
72
     */
73
    private function getClassFullNameFromFile(SplFileInfo $file)
74
    {
75
        $targets = [
76
            realpath($this->namespaceDirectory).'/',
77
            '.php',
78
        ];
79
80
        $replaceValues = ['', ''];
81
82
        $className = str_replace($targets, $replaceValues, realpath($file->getPathname()));
83
        $className = str_replace('/', "\\", $className);
84
85
        return $this->namespace."\\".$className;
86
    }
87
88
    /**
89
     * @param string $directory
90
     *
91
     * @return RecursiveIteratorIterator
92
     */
93
    private function createIterator($directory)
94
    {
95
        $directoryIterator = new RecursiveDirectoryIterator($directory,
96
            FilesystemIterator::CURRENT_AS_FILEINFO |
97
            FilesystemIterator::KEY_AS_PATHNAME |
98
            FilesystemIterator::SKIP_DOTS
99
        );
100
101
        $filterIterator = new RecursiveIteratorIterator($directoryIterator,
102
            RecursiveIteratorIterator::LEAVES_ONLY);
103
104
        return $filterIterator;
105
    }
106
}
107