Code and test

1.x 1.0.0
Julien Rosset 2 years ago
parent 8215fd4987
commit eb0ffad4a0

@ -15,7 +15,7 @@
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {
"php": "^8.1" "php": "^7.4 || ^8.0.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

@ -5,7 +5,6 @@ namespace jrosset\Reflection;
use FilesystemIterator; use FilesystemIterator;
use ReflectionClass; use ReflectionClass;
use ReflectionEnum;
use ReflectionException; use ReflectionException;
/** /**
@ -42,7 +41,7 @@ class ReflectionNamespace {
* @param string $name The namespace name * @param string $name The namespace name
* @param string[]|string $directories The list of directories that maps the namespace * @param string[]|string $directories The list of directories that maps the namespace
*/ */
public function __construct (string $name, array|string $directories) { public function __construct (string $name, $directories) {
$this->name = self::normalizeNamespaceName($name); $this->name = self::normalizeNamespaceName($name);
if (!is_array($directories)) { if (!is_array($directories)) {
@ -61,7 +60,7 @@ class ReflectionNamespace {
* *
* @throws ReflectionException If the namespace can not be found with the mapping * @throws ReflectionException If the namespace can not be found with the mapping
*/ */
public static function createFromMapping (string $name, array $mapping): static { public static function createFromMapping (string $name, array $mapping): self {
$nsReflection = new static ($name, []); $nsReflection = new static ($name, []);
//region Normalize the mapping //region Normalize the mapping
@ -119,7 +118,7 @@ class ReflectionNamespace {
* *
* @throws ReflectionException If the namespace can not be found * @throws ReflectionException If the namespace can not be found
*/ */
public static function createFromComposerMapping (string $name, string $vendorDirectoryPath): static { public static function createFromComposerMapping (string $name, string $vendorDirectoryPath): self {
$composerDirectory = static::normalizeDirectoryPath($vendorDirectoryPath) . DIRECTORY_SEPARATOR . 'composer'; $composerDirectory = static::normalizeDirectoryPath($vendorDirectoryPath) . DIRECTORY_SEPARATOR . 'composer';
if (!file_exists($composerDirectory) || !is_dir($vendorDirectoryPath) || !is_readable($vendorDirectoryPath)) { if (!file_exists($composerDirectory) || !is_dir($vendorDirectoryPath) || !is_readable($vendorDirectoryPath)) {
throw new ReflectionException('Unable to find composer directory: ' . $composerDirectory); throw new ReflectionException('Unable to find composer directory: ' . $composerDirectory);
@ -188,7 +187,7 @@ class ReflectionNamespace {
* *
* @return static|null The parent namespace or Null if there is no parent * @return static|null The parent namespace or Null if there is no parent
*/ */
public function getParent (): ?static { public function getParent (): ?self {
if (($parentNamespaceName = $this->getParentName()) === null) { if (($parentNamespaceName = $this->getParentName()) === null) {
return null; return null;
} }
@ -216,7 +215,7 @@ class ReflectionNamespace {
* *
* @throws ReflectionException If the sub namespace doesn't exist * @throws ReflectionException If the sub namespace doesn't exist
*/ */
public function getSubNamespace (string $subNamespaceName): static { public function getSubNamespace (string $subNamespaceName): self {
$this->checkValidDirectories(); $this->checkValidDirectories();
$subNamespaceDirectories = []; $subNamespaceDirectories = [];
@ -336,12 +335,12 @@ class ReflectionNamespace {
try { try {
$classReflection = new ReflectionClass($this->getName() . '\\' . $element); $classReflection = new ReflectionClass($this->getName() . '\\' . $element);
} }
catch (ReflectionException) { catch (ReflectionException $exception) {
continue; continue;
} }
//endregion //endregion
//region Check it is a class (ignore interfaces and traits) //region Check it is a class (ignore interfaces and traits)
if ($classReflection->isInterface() || $classReflection->isTrait() || $classReflection->isEnum()) { if ($classReflection->isInterface() || $classReflection->isTrait()) {
continue; continue;
} }
//endregion //endregion
@ -395,7 +394,7 @@ class ReflectionNamespace {
try { try {
$interfaceReflection = new ReflectionClass($this->getName() . '\\' . $element); $interfaceReflection = new ReflectionClass($this->getName() . '\\' . $element);
} }
catch (ReflectionException) { catch (ReflectionException $exception) {
continue; continue;
} }
//endregion //endregion
@ -446,7 +445,7 @@ class ReflectionNamespace {
try { try {
$traitReflection = new ReflectionClass($this->getName() . '\\' . $element); $traitReflection = new ReflectionClass($this->getName() . '\\' . $element);
} }
catch (ReflectionException) { catch (ReflectionException $exception) {
continue; continue;
} }
//endregion //endregion
@ -463,48 +462,6 @@ class ReflectionNamespace {
return $traits; return $traits;
} }
/**
* Get an enum of the namespace
*
* @param string $enumName The enum name
*
* @return ReflectionEnum The reflection enum
*
* @throws ReflectionException If the enum doesn't exist
*/
public function getEnum (string $enumName): ReflectionEnum {
return new ReflectionEnum($this->getName() . '\\' . $enumName);
}
/**
* Get the enums of the namespace
*
* @param bool $recursive Search also in sub namespaces
*
* @return ReflectionEnum[] The list of enums
*
* @throws ReflectionException If the namespace has no configured directories
*/
public function getEnums (bool $recursive = false): array {
$traits = [];
//region For each valid element of the namespace
foreach ($this->getValidElements($recursive) as $element) {
//region Try to get the reflection class
try {
$enumReflection = new ReflectionEnum($this->getName() . '\\' . $element);
}
catch (ReflectionException) {
continue;
}
//endregion
$traits[] = $enumReflection;
}
//endregion
return $traits;
}
/** /**
* The list of directories that maps the namespace * The list of directories that maps the namespace
* *
@ -518,7 +475,7 @@ class ReflectionNamespace {
* *
* @param string[] $directories The list of directories that maps the namespace * @param string[] $directories The list of directories that maps the namespace
*/ */
public function setDirectories (array $directories): static { public function setDirectories (array $directories): self {
$this->directories = array_unique(static::normalizeDirectoriesPath($directories)); $this->directories = array_unique(static::normalizeDirectoriesPath($directories));
return $this; return $this;
} }

@ -1,8 +0,0 @@
<?php
namespace Tests\SubTests1;
enum Enum1 {
case One;
case Two;
}

@ -1,8 +0,0 @@
<?php
namespace Tests\SubTests1\SubSubTests11;
enum Enum11: int {
case One = 1;
case Two = 2;
}

@ -27,8 +27,3 @@ echo '===== TRAITS =====' . PHP_EOL;
foreach ($tests->getTraits(true) as $trait) { foreach ($tests->getTraits(true) as $trait) {
echo "\t - " . $trait->getName() . PHP_EOL; echo "\t - " . $trait->getName() . PHP_EOL;
} }
echo '===== ENUMS =====' . PHP_EOL;
foreach ($tests->getEnums(true) as $enum) {
echo "\t - " . $enum->getName() . ($enum->isBacked() ? ' (backed)' : '') . PHP_EOL;
}
Loading…
Cancel
Save