Compare commits

..

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

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

@ -5,6 +5,7 @@ namespace jrosset\Reflection;
use FilesystemIterator;
use ReflectionClass;
use ReflectionEnum;
use ReflectionException;
/**
@ -41,7 +42,7 @@ class ReflectionNamespace {
* @param string $name The namespace name
* @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);
if (!is_array($directories)) {
@ -60,7 +61,7 @@ class ReflectionNamespace {
*
* @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, []);
//region Normalize the mapping
@ -118,7 +119,7 @@ class ReflectionNamespace {
*
* @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';
if (!file_exists($composerDirectory) || !is_dir($vendorDirectoryPath) || !is_readable($vendorDirectoryPath)) {
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
*/
public function getParent (): ?self {
public function getParent (): ?static {
if (($parentNamespaceName = $this->getParentName()) === null) {
return null;
}
@ -215,7 +216,7 @@ class ReflectionNamespace {
*
* @throws ReflectionException If the sub namespace doesn't exist
*/
public function getSubNamespace (string $subNamespaceName): self {
public function getSubNamespace (string $subNamespaceName): static {
$this->checkValidDirectories();
$subNamespaceDirectories = [];
@ -335,12 +336,12 @@ class ReflectionNamespace {
try {
$classReflection = new ReflectionClass($this->getName() . '\\' . $element);
}
catch (ReflectionException $exception) {
catch (ReflectionException) {
continue;
}
//endregion
//region Check it is a class (ignore interfaces and traits)
if ($classReflection->isInterface() || $classReflection->isTrait()) {
if ($classReflection->isInterface() || $classReflection->isTrait() || $classReflection->isEnum()) {
continue;
}
//endregion
@ -394,7 +395,7 @@ class ReflectionNamespace {
try {
$interfaceReflection = new ReflectionClass($this->getName() . '\\' . $element);
}
catch (ReflectionException $exception) {
catch (ReflectionException) {
continue;
}
//endregion
@ -445,7 +446,7 @@ class ReflectionNamespace {
try {
$traitReflection = new ReflectionClass($this->getName() . '\\' . $element);
}
catch (ReflectionException $exception) {
catch (ReflectionException) {
continue;
}
//endregion
@ -462,6 +463,48 @@ class ReflectionNamespace {
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
*
@ -475,7 +518,7 @@ class ReflectionNamespace {
*
* @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));
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) {
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