Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_classes_ClassIterator | |
0.00% |
0 / 20 |
|
0.00% |
0 / 6 |
240 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
30 | |||
| rewind | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| current | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| next | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
| valid | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | /** |
| 24 | * Iterates over a class(es) and its subclasses |
| 25 | * |
| 26 | * @author Joel Bout <joel@taotesting.com> |
| 27 | */ |
| 28 | class core_kernel_classes_ClassIterator implements \Iterator |
| 29 | { |
| 30 | /** |
| 31 | * List of classes whose subclasses have not yet been exploded |
| 32 | * |
| 33 | * @var array |
| 34 | */ |
| 35 | private $todoClasses = []; |
| 36 | |
| 37 | private $classes = []; |
| 38 | |
| 39 | private $currentId = -1; |
| 40 | |
| 41 | /** |
| 42 | * Constructor of the iterator expecting a class or classes as argument |
| 43 | * |
| 44 | * @param mixed $classes array/instance of class(es) to iterate over |
| 45 | */ |
| 46 | public function __construct($classes) |
| 47 | { |
| 48 | $classes = is_array($classes) ? $classes : [$classes]; |
| 49 | foreach ($classes as $class) { |
| 50 | $this->todoClasses[] = (is_object($class) && $class instanceof core_kernel_classes_Class) |
| 51 | ? $class->getUri() |
| 52 | : $class; |
| 53 | } |
| 54 | $this->rewind(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * (non-PHPdoc) |
| 59 | * @see Iterator::rewind() |
| 60 | */ |
| 61 | public function rewind() |
| 62 | { |
| 63 | $this->currentId = -1; |
| 64 | $this->next(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * (non-PHPdoc) |
| 69 | * @see Iterator::current() |
| 70 | */ |
| 71 | public function current() |
| 72 | { |
| 73 | return new \core_kernel_classes_Class($this->classes[$this->currentId]); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * (non-PHPdoc) |
| 78 | * @see Iterator::key() |
| 79 | */ |
| 80 | public function key() |
| 81 | { |
| 82 | return $this->currentId; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * (non-PHPdoc) |
| 87 | * @see Iterator::next() |
| 88 | */ |
| 89 | public function next() |
| 90 | { |
| 91 | $this->currentId++; |
| 92 | if (!isset($this->classes[$this->currentId]) && !empty($this->todoClasses)) { |
| 93 | $newUri = array_shift($this->todoClasses); |
| 94 | $this->classes[] = $newUri; |
| 95 | $class = new \core_kernel_classes_Class($newUri); |
| 96 | foreach ($class->getSubClasses(false) as $subClass) { |
| 97 | if ( |
| 98 | !in_array($subClass->getUri(), $this->classes) |
| 99 | && !in_array($subClass->getUri(), $this->todoClasses) |
| 100 | ) { |
| 101 | $this->todoClasses[] = $subClass->getUri(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * (non-PHPdoc) |
| 109 | * @see Iterator::valid() |
| 110 | */ |
| 111 | public function valid() |
| 112 | { |
| 113 | return isset($this->classes[$this->currentId]); |
| 114 | } |
| 115 | } |