Compare commits

..

No commits in common. '1.x' and 'master' have entirely different histories.
1.x ... master

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

@ -5,6 +5,7 @@ namespace jrosset\Reflection;
use FilesystemIterator; use FilesystemIterator;
use ReflectionClass; use ReflectionClass;
use ReflectionEnum;
use ReflectionException; use ReflectionException;
/** /**
@ -41,7 +42,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, $directories) { public function __construct (string $name, array|string $directories) {
$this->name = self::normalizeNamespaceName($name); $this->name = self::normalizeNamespaceName($name);
if (!is_array($directories)) { if (!is_array($directories)) {
@ -60,7 +61,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): self { public static function createFromMapping (string $name, array $mapping): static {
$nsReflection = new static ($name, []); $nsReflection = new static ($name, []);
//region Normalize the mapping //region Normalize the mapping
@ -118,7 +119,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): self { public static function createFromComposerMapping (string $name, string $vendorDirectoryPath): static {
$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);
@ -187,7 +188,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 (): ?self { public function getParent (): ?static {
if (($parentNamespaceName = $this->getParentName()) === null) { if (($parentNamespaceName = $this->getParentName()) === null) {
return null; return null;
} }
@ -215,7 +216,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): self { public function getSubNamespace (string $subNamespaceName): static {
$this->checkValidDirectories(); $this->checkValidDirectories();
$subNamespaceDirectories = []; $subNamespaceDirectories = [];
@ -335,12 +336,12 @@ class ReflectionNamespace {
try { try {
$classReflection = new ReflectionClass($this->getName() . '\\' . $element); $classReflection = new ReflectionClass($this->getName() . '\\' . $element);
} }
catch (ReflectionException $exception) { catch (ReflectionException) {
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()) { if ($classReflection->isInterface() || $classReflection->isTrait() || $classReflection->isEnum()) {
continue; continue;
} }
//endregion //endregion
@ -394,7 +395,7 @@ class ReflectionNamespace {
try { try {
$interfaceReflection = new ReflectionClass($this->getName() . '\\' . $element); $interfaceReflection = new ReflectionClass($this->getName() . '\\' . $element);
} }
catch (ReflectionException $exception) { catch (ReflectionException) {
continue; continue;
} }
//endregion //endregion
@ -445,7 +446,7 @@ class ReflectionNamespace {
try { try {
$traitReflection = new ReflectionClass($this->getName() . '\\' . $element); $traitReflection = new ReflectionClass($this->getName() . '\\' . $element);
} }
catch (ReflectionException $exception) { catch (ReflectionException) {
continue; continue;
} }
//endregion //endregion
@ -462,6 +463,48 @@ 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
* *
@ -475,7 +518,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): self { public function setDirectories (array $directories): static {
$this->directories = array_unique(static::normalizeDirectoriesPath($directories)); $this->directories = array_unique(static::normalizeDirectoriesPath($directories));
return $this; return $this;
} }

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

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

@ -27,3 +27,8 @@ 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