Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
25 / 30 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| InterfaceReflection | |
83.33% |
25 / 30 |
|
0.00% |
0 / 1 |
17.19 | |
0.00% |
0 / 1 |
| parse | |
83.33% |
25 / 30 |
|
0.00% |
0 / 1 |
17.19 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Pop PHP Framework (https://www.popphp.org/) |
| 4 | * |
| 5 | * @link https://github.com/popphp/popphp-framework |
| 6 | * @author Nick Sagona, III <dev@noladev.com> |
| 7 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 8 | * @license https://www.popphp.org/license New BSD License |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * @namespace |
| 13 | */ |
| 14 | namespace Pop\Code\Reflection; |
| 15 | |
| 16 | use Pop\Code\Generator; |
| 17 | use ReflectionException; |
| 18 | |
| 19 | /** |
| 20 | * Interface reflection code class |
| 21 | * |
| 22 | * @category Pop |
| 23 | * @package Pop\Code |
| 24 | * @author Nick Sagona, III <dev@noladev.com> |
| 25 | * @copyright Copyright (c) 2009-2026 NOLA Interactive, LLC. |
| 26 | * @license https://www.popphp.org/license New BSD License |
| 27 | * @version 5.0.5 |
| 28 | */ |
| 29 | class InterfaceReflection extends AbstractReflection |
| 30 | { |
| 31 | |
| 32 | /** |
| 33 | * Method to parse an interface |
| 34 | * |
| 35 | * @param mixed $code |
| 36 | * @param ?string $name |
| 37 | * @throws Exception|ReflectionException |
| 38 | * @return Generator\InterfaceGenerator |
| 39 | */ |
| 40 | public static function parse(mixed $code, ?string $name = null): Generator\InterfaceGenerator |
| 41 | { |
| 42 | $reflection = new \ReflectionClass($code); |
| 43 | $reflectionName = $reflection->getShortName(); |
| 44 | |
| 45 | if (($name === null) && !empty($reflectionName)) { |
| 46 | $name = $reflectionName; |
| 47 | } |
| 48 | |
| 49 | if (!$reflection->isInterface()) { |
| 50 | throw new Exception('Error: The code is not an interface.'); |
| 51 | } |
| 52 | |
| 53 | $interface = new Generator\InterfaceGenerator($name); |
| 54 | |
| 55 | // Detect and set namespace |
| 56 | if ($reflection->inNamespace()) { |
| 57 | $file = $reflection->getFileName(); |
| 58 | if (!empty($file) && file_exists($file)) { |
| 59 | $interface->setNamespace(NamespaceReflection::parse(file_get_contents($file), $reflection->getNamespaceName())); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Detect and set the class doc block |
| 64 | $interfaceDocBlock = $reflection->getDocComment(); |
| 65 | if (!empty($interfaceDocBlock) && (str_contains($interfaceDocBlock, '/*'))) { |
| 66 | $interface->setDocblock(DocblockReflection::parse($interfaceDocBlock)); |
| 67 | } |
| 68 | |
| 69 | // Detect parent class |
| 70 | $parent = $reflection->getParentClass(); |
| 71 | if ($parent !== false) { |
| 72 | if ($parent->inNamespace()) { |
| 73 | if (!$interface->hasNamespace()) { |
| 74 | $interface->setNamespace(new Generator\NamespaceGenerator()); |
| 75 | } |
| 76 | $interface->getNamespace()->addUse($parent->getNamespaceName() . '\\' . $parent->getShortName()); |
| 77 | } |
| 78 | $interface->setParent($parent->getShortName()); |
| 79 | } |
| 80 | |
| 81 | // Detect constants |
| 82 | $constants = $reflection->getConstants(); |
| 83 | if (count($constants) > 0) { |
| 84 | foreach ($constants as $key => $value) { |
| 85 | $interface->addConstant(new Generator\ConstantGenerator($key, gettype($value), $value)); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // Detect methods |
| 90 | $methods = $reflection->getMethods(); |
| 91 | if (count($methods) > 0) { |
| 92 | foreach ($methods as $method) { |
| 93 | $interface->addMethod(MethodReflection::parse($method, $method->name)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return $interface; |
| 98 | } |
| 99 | |
| 100 | } |