first commit

This commit is contained in:
Sampanna Rimal
2024-08-27 17:48:06 +05:45
commit 53c0140f58
10839 changed files with 1125847 additions and 0 deletions

78
vendor/sebastian/code-unit/ChangeLog.md vendored Normal file
View File

@ -0,0 +1,78 @@
# ChangeLog
All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles.
## [2.0.0] - 2023-02-03
### Added
* Added `SebastianBergmann\CodeUnit\FileUnit` value object that represents a sourcecode file
### Removed
* `SebastianBergmann\CodeUnit\CodeUnitCollection::fromArray()` has been removed
* `SebastianBergmann\CodeUnit\Mapper::stringToCodeUnits()` no longer supports `ClassName<*>`
* This component is no longer supported on PHP 7.3, PHP 7.4, and PHP 8.0
## [1.0.8] - 2020-10-26
### Fixed
* `SebastianBergmann\CodeUnit\Exception` now correctly extends `\Throwable`
## [1.0.7] - 2020-10-02
### Fixed
* `SebastianBergmann\CodeUnit\Mapper::stringToCodeUnits()` no longer attempts to create `CodeUnit` objects for code units that are not declared in userland
## [1.0.6] - 2020-09-28
### Changed
* Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3`
## [1.0.5] - 2020-06-26
### Fixed
* [#3](https://github.com/sebastianbergmann/code-unit/issues/3): Regression in 1.0.4
## [1.0.4] - 2020-06-26
### Added
* This component is now supported on PHP 8
## [1.0.3] - 2020-06-15
### Changed
* Tests etc. are now ignored for archive exports
## [1.0.2] - 2020-04-30
### Fixed
* `Mapper::stringToCodeUnits()` raised the wrong exception for `Class::method` when a class named `Class` exists but does not have a method named `method`
## [1.0.1] - 2020-04-27
### Fixed
* [#2](https://github.com/sebastianbergmann/code-unit/issues/2): `Mapper::stringToCodeUnits()` breaks when `ClassName<extended>` is used for class that extends built-in class
## [1.0.0] - 2020-03-30
* Initial release
[2.0.0]: https://github.com/sebastianbergmann/code-unit/compare/1.0.8...2.0.0
[1.0.8]: https://github.com/sebastianbergmann/code-unit/compare/1.0.7...1.0.8
[1.0.7]: https://github.com/sebastianbergmann/code-unit/compare/1.0.6...1.0.7
[1.0.6]: https://github.com/sebastianbergmann/code-unit/compare/1.0.5...1.0.6
[1.0.5]: https://github.com/sebastianbergmann/code-unit/compare/1.0.4...1.0.5
[1.0.4]: https://github.com/sebastianbergmann/code-unit/compare/1.0.3...1.0.4
[1.0.3]: https://github.com/sebastianbergmann/code-unit/compare/1.0.2...1.0.3
[1.0.2]: https://github.com/sebastianbergmann/code-unit/compare/1.0.1...1.0.2
[1.0.1]: https://github.com/sebastianbergmann/code-unit/compare/1.0.0...1.0.1
[1.0.0]: https://github.com/sebastianbergmann/code-unit/compare/530c3900e5db9bcb8516da545bef0d62536cedaa...1.0.0

29
vendor/sebastian/code-unit/LICENSE vendored Normal file
View File

@ -0,0 +1,29 @@
BSD 3-Clause License
Copyright (c) 2020-2023, Sebastian Bergmann
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

22
vendor/sebastian/code-unit/README.md vendored Normal file
View File

@ -0,0 +1,22 @@
[![Latest Stable Version](https://poser.pugx.org/sebastian/code-unit/v/stable.png)](https://packagist.org/packages/sebastian/code-unit)
[![CI Status](https://github.com/sebastianbergmann/code-unit/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/code-unit/actions)
[![Type Coverage](https://shepherd.dev/github/sebastianbergmann/code-unit/coverage.svg)](https://shepherd.dev/github/sebastianbergmann/code-unit)
[![codecov](https://codecov.io/gh/sebastianbergmann/code-unit/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/code-unit)
# sebastian/code-unit
Collection of value objects that represent the PHP code units.
## Installation
You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/):
```
composer require sebastian/code-unit
```
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
```
composer require --dev sebastian/code-unit
```

View File

@ -0,0 +1,9 @@
# Security Policy
This library is intended to be used in development environments only. For instance, it is used by the testing framework PHPUnit. There is no reason why this library should be installed on a webserver.
**If you upload this library to a webserver then your deployment process is broken. On a more general note, if your `vendor` directory is publicly accessible on your webserver then your deployment process is also broken.**
## Security Contact Information
After the above, if you still would like to report a security vulnerability, please email `sebastian@phpunit.de`.

View File

@ -0,0 +1,50 @@
{
"name": "sebastian/code-unit",
"description": "Collection of value objects that represent the PHP code units",
"type": "library",
"homepage": "https://github.com/sebastianbergmann/code-unit",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Sebastian Bergmann",
"email": "sebastian@phpunit.de",
"role": "lead"
}
],
"support": {
"issues": "https://github.com/sebastianbergmann/code-unit/issues"
},
"prefer-stable": true,
"require": {
"php": ">=8.1"
},
"require-dev": {
"phpunit/phpunit": "^10.0"
},
"config": {
"platform": {
"php": "8.1.0"
},
"optimize-autoloader": true,
"sort-packages": true
},
"autoload": {
"classmap": [
"src/"
]
},
"autoload-dev": {
"classmap": [
"tests/_fixture"
],
"files": [
"tests/_fixture/file_with_multiple_code_units.php",
"tests/_fixture/function.php"
]
},
"extra": {
"branch-alias": {
"dev-main": "2.0-dev"
}
}
}

View File

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
/**
* @psalm-immutable
*/
final class ClassMethodUnit extends CodeUnit
{
/**
* @psalm-assert-if-true ClassMethodUnit $this
*/
public function isClassMethod(): bool
{
return true;
}
}

View File

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
/**
* @psalm-immutable
*/
final class ClassUnit extends CodeUnit
{
/**
* @psalm-assert-if-true ClassUnit $this
*/
public function isClass(): bool
{
return true;
}
}

View File

@ -0,0 +1,478 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
use function count;
use function file;
use function file_exists;
use function is_readable;
use function range;
use function sprintf;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
/**
* @psalm-immutable
*/
abstract class CodeUnit
{
private readonly string $name;
private readonly string $sourceFileName;
/**
* @psalm-var list<int>
*/
private readonly array $sourceLines;
/**
* @psalm-param class-string $className
*
* @throws InvalidCodeUnitException
* @throws ReflectionException
*/
public static function forClass(string $className): ClassUnit
{
self::ensureUserDefinedClass($className);
$reflector = self::reflectorForClass($className);
return new ClassUnit(
$className,
$reflector->getFileName(),
range(
$reflector->getStartLine(),
$reflector->getEndLine()
)
);
}
/**
* @psalm-param class-string $className
*
* @throws InvalidCodeUnitException
* @throws ReflectionException
*/
public static function forClassMethod(string $className, string $methodName): ClassMethodUnit
{
self::ensureUserDefinedClass($className);
$reflector = self::reflectorForClassMethod($className, $methodName);
return new ClassMethodUnit(
$className . '::' . $methodName,
$reflector->getFileName(),
range(
$reflector->getStartLine(),
$reflector->getEndLine()
)
);
}
/**
* @throws InvalidCodeUnitException
*/
public static function forFileWithAbsolutePath(string $path): FileUnit
{
self::ensureFileExistsAndIsReadable($path);
return new FileUnit(
$path,
$path,
range(
1,
count(file($path))
)
);
}
/**
* @psalm-param class-string $interfaceName
*
* @throws InvalidCodeUnitException
* @throws ReflectionException
*/
public static function forInterface(string $interfaceName): InterfaceUnit
{
self::ensureUserDefinedInterface($interfaceName);
$reflector = self::reflectorForClass($interfaceName);
return new InterfaceUnit(
$interfaceName,
$reflector->getFileName(),
range(
$reflector->getStartLine(),
$reflector->getEndLine()
)
);
}
/**
* @psalm-param class-string $interfaceName
*
* @throws InvalidCodeUnitException
* @throws ReflectionException
*/
public static function forInterfaceMethod(string $interfaceName, string $methodName): InterfaceMethodUnit
{
self::ensureUserDefinedInterface($interfaceName);
$reflector = self::reflectorForClassMethod($interfaceName, $methodName);
return new InterfaceMethodUnit(
$interfaceName . '::' . $methodName,
$reflector->getFileName(),
range(
$reflector->getStartLine(),
$reflector->getEndLine()
)
);
}
/**
* @psalm-param class-string $traitName
*
* @throws InvalidCodeUnitException
* @throws ReflectionException
*/
public static function forTrait(string $traitName): TraitUnit
{
self::ensureUserDefinedTrait($traitName);
$reflector = self::reflectorForClass($traitName);
return new TraitUnit(
$traitName,
$reflector->getFileName(),
range(
$reflector->getStartLine(),
$reflector->getEndLine()
)
);
}
/**
* @psalm-param class-string $traitName
*
* @throws InvalidCodeUnitException
* @throws ReflectionException
*/
public static function forTraitMethod(string $traitName, string $methodName): TraitMethodUnit
{
self::ensureUserDefinedTrait($traitName);
$reflector = self::reflectorForClassMethod($traitName, $methodName);
return new TraitMethodUnit(
$traitName . '::' . $methodName,
$reflector->getFileName(),
range(
$reflector->getStartLine(),
$reflector->getEndLine()
)
);
}
/**
* @psalm-param callable-string $functionName
*
* @throws InvalidCodeUnitException
* @throws ReflectionException
*/
public static function forFunction(string $functionName): FunctionUnit
{
$reflector = self::reflectorForFunction($functionName);
if (!$reflector->isUserDefined()) {
throw new InvalidCodeUnitException(
sprintf(
'"%s" is not a user-defined function',
$functionName
)
);
}
return new FunctionUnit(
$functionName,
$reflector->getFileName(),
range(
$reflector->getStartLine(),
$reflector->getEndLine()
)
);
}
/**
* @psalm-param list<int> $sourceLines
*/
private function __construct(string $name, string $sourceFileName, array $sourceLines)
{
$this->name = $name;
$this->sourceFileName = $sourceFileName;
$this->sourceLines = $sourceLines;
}
public function name(): string
{
return $this->name;
}
public function sourceFileName(): string
{
return $this->sourceFileName;
}
/**
* @psalm-return list<int>
*/
public function sourceLines(): array
{
return $this->sourceLines;
}
public function isClass(): bool
{
return false;
}
public function isClassMethod(): bool
{
return false;
}
public function isInterface(): bool
{
return false;
}
public function isInterfaceMethod(): bool
{
return false;
}
public function isTrait(): bool
{
return false;
}
public function isTraitMethod(): bool
{
return false;
}
public function isFunction(): bool
{
return false;
}
public function isFile(): bool
{
return false;
}
/**
* @throws InvalidCodeUnitException
*/
private static function ensureFileExistsAndIsReadable(string $path): void
{
if (!(file_exists($path) && is_readable($path))) {
throw new InvalidCodeUnitException(
sprintf(
'File "%s" does not exist or is not readable',
$path
)
);
}
}
/**
* @psalm-param class-string $className
*
* @throws InvalidCodeUnitException
*/
private static function ensureUserDefinedClass(string $className): void
{
try {
$reflector = new ReflectionClass($className);
if ($reflector->isInterface()) {
throw new InvalidCodeUnitException(
sprintf(
'"%s" is an interface and not a class',
$className
)
);
}
if ($reflector->isTrait()) {
throw new InvalidCodeUnitException(
sprintf(
'"%s" is a trait and not a class',
$className
)
);
}
if (!$reflector->isUserDefined()) {
throw new InvalidCodeUnitException(
sprintf(
'"%s" is not a user-defined class',
$className
)
);
}
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @psalm-param class-string $interfaceName
*
* @throws InvalidCodeUnitException
*/
private static function ensureUserDefinedInterface(string $interfaceName): void
{
try {
$reflector = new ReflectionClass($interfaceName);
if (!$reflector->isInterface()) {
throw new InvalidCodeUnitException(
sprintf(
'"%s" is not an interface',
$interfaceName
)
);
}
if (!$reflector->isUserDefined()) {
throw new InvalidCodeUnitException(
sprintf(
'"%s" is not a user-defined interface',
$interfaceName
)
);
}
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @psalm-param class-string $traitName
*
* @throws InvalidCodeUnitException
*/
private static function ensureUserDefinedTrait(string $traitName): void
{
try {
$reflector = new ReflectionClass($traitName);
if (!$reflector->isTrait()) {
throw new InvalidCodeUnitException(
sprintf(
'"%s" is not a trait',
$traitName
)
);
}
// @codeCoverageIgnoreStart
if (!$reflector->isUserDefined()) {
throw new InvalidCodeUnitException(
sprintf(
'"%s" is not a user-defined trait',
$traitName
)
);
}
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @psalm-param class-string $className
*
* @throws ReflectionException
*/
private static function reflectorForClass(string $className): ReflectionClass
{
try {
return new ReflectionClass($className);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @psalm-param class-string $className
*
* @throws ReflectionException
*/
private static function reflectorForClassMethod(string $className, string $methodName): ReflectionMethod
{
try {
return new ReflectionMethod($className, $methodName);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @psalm-param callable-string $functionName
*
* @throws ReflectionException
*/
private static function reflectorForFunction(string $functionName): ReflectionFunction
{
try {
return new ReflectionFunction($functionName);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
}

View File

@ -0,0 +1,74 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
use function array_merge;
use function count;
use Countable;
use IteratorAggregate;
/**
* @template-implements IteratorAggregate<int, CodeUnit>
*
* @psalm-immutable
*/
final class CodeUnitCollection implements Countable, IteratorAggregate
{
/**
* @psalm-var list<CodeUnit>
*/
private readonly array $codeUnits;
public static function fromList(CodeUnit ...$codeUnits): self
{
return new self($codeUnits);
}
/**
* @psalm-param list<CodeUnit> $codeUnits
*/
private function __construct(array $codeUnits)
{
$this->codeUnits = $codeUnits;
}
/**
* @psalm-return list<CodeUnit>
*/
public function asArray(): array
{
return $this->codeUnits;
}
public function getIterator(): CodeUnitCollectionIterator
{
return new CodeUnitCollectionIterator($this);
}
public function count(): int
{
return count($this->codeUnits);
}
public function isEmpty(): bool
{
return empty($this->codeUnits);
}
public function mergeWith(self $other): self
{
return new self(
array_merge(
$this->asArray(),
$other->asArray()
)
);
}
}

View File

@ -0,0 +1,54 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
use Iterator;
/**
* @template-implements Iterator<int, CodeUnit>
*/
final class CodeUnitCollectionIterator implements Iterator
{
/**
* @psalm-var list<CodeUnit>
*/
private array $codeUnits;
private int $position = 0;
public function __construct(CodeUnitCollection $collection)
{
$this->codeUnits = $collection->asArray();
}
public function rewind(): void
{
$this->position = 0;
}
public function valid(): bool
{
return isset($this->codeUnits[$this->position]);
}
public function key(): int
{
return $this->position;
}
public function current(): CodeUnit
{
return $this->codeUnits[$this->position];
}
public function next(): void
{
$this->position++;
}
}

View File

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
/**
* @psalm-immutable
*/
final class FileUnit extends CodeUnit
{
/**
* @psalm-assert-if-true FileUnit $this
*/
public function isFile(): bool
{
return true;
}
}

View File

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
/**
* @psalm-immutable
*/
final class FunctionUnit extends CodeUnit
{
/**
* @psalm-assert-if-true FunctionUnit $this
*/
public function isFunction(): bool
{
return true;
}
}

View File

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
/**
* @psalm-immutable
*/
final class InterfaceMethodUnit extends CodeUnit
{
/**
* @psalm-assert-if-true InterfaceMethod $this
*/
public function isInterfaceMethod(): bool
{
return true;
}
}

View File

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
/**
* @psalm-immutable
*/
final class InterfaceUnit extends CodeUnit
{
/**
* @psalm-assert-if-true InterfaceUnit $this
*/
public function isInterface(): bool
{
return true;
}
}

View File

@ -0,0 +1,259 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
use function array_keys;
use function array_merge;
use function array_unique;
use function array_values;
use function class_exists;
use function explode;
use function function_exists;
use function interface_exists;
use function ksort;
use function method_exists;
use function sort;
use function sprintf;
use function str_contains;
use function trait_exists;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
final class Mapper
{
/**
* @psalm-return array<string,list<int>>
*/
public function codeUnitsToSourceLines(CodeUnitCollection $codeUnits): array
{
$result = [];
foreach ($codeUnits as $codeUnit) {
$sourceFileName = $codeUnit->sourceFileName();
if (!isset($result[$sourceFileName])) {
$result[$sourceFileName] = [];
}
$result[$sourceFileName] = array_merge($result[$sourceFileName], $codeUnit->sourceLines());
}
foreach (array_keys($result) as $sourceFileName) {
$result[$sourceFileName] = array_values(array_unique($result[$sourceFileName]));
sort($result[$sourceFileName]);
}
ksort($result);
return $result;
}
/**
* @throws InvalidCodeUnitException
* @throws ReflectionException
*/
public function stringToCodeUnits(string $unit): CodeUnitCollection
{
if (str_contains($unit, '::')) {
[$firstPart, $secondPart] = explode('::', $unit);
if ($this->isUserDefinedFunction($secondPart)) {
return CodeUnitCollection::fromList(CodeUnit::forFunction($secondPart));
}
if ($this->isUserDefinedMethod($firstPart, $secondPart)) {
return CodeUnitCollection::fromList(CodeUnit::forClassMethod($firstPart, $secondPart));
}
if ($this->isUserDefinedInterface($firstPart)) {
return CodeUnitCollection::fromList(CodeUnit::forInterfaceMethod($firstPart, $secondPart));
}
if ($this->isUserDefinedTrait($firstPart)) {
return CodeUnitCollection::fromList(CodeUnit::forTraitMethod($firstPart, $secondPart));
}
} else {
if ($this->isUserDefinedClass($unit)) {
$units = [CodeUnit::forClass($unit)];
foreach ($this->reflectorForClass($unit)->getTraits() as $trait) {
if (!$trait->isUserDefined()) {
// @codeCoverageIgnoreStart
continue;
// @codeCoverageIgnoreEnd
}
$units[] = CodeUnit::forTrait($trait->getName());
}
return CodeUnitCollection::fromList(...$units);
}
if ($this->isUserDefinedInterface($unit)) {
return CodeUnitCollection::fromList(CodeUnit::forInterface($unit));
}
if ($this->isUserDefinedTrait($unit)) {
return CodeUnitCollection::fromList(CodeUnit::forTrait($unit));
}
if ($this->isUserDefinedFunction($unit)) {
return CodeUnitCollection::fromList(CodeUnit::forFunction($unit));
}
}
throw new InvalidCodeUnitException(
sprintf(
'"%s" is not a valid code unit',
$unit
)
);
}
/**
* @psalm-param class-string $className
*
* @throws ReflectionException
*/
private function reflectorForClass(string $className): ReflectionClass
{
try {
return new ReflectionClass($className);
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @throws ReflectionException
*/
private function isUserDefinedFunction(string $functionName): bool
{
if (!function_exists($functionName)) {
return false;
}
try {
return (new ReflectionFunction($functionName))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @throws ReflectionException
*/
private function isUserDefinedClass(string $className): bool
{
if (!class_exists($className)) {
return false;
}
try {
return (new ReflectionClass($className))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @throws ReflectionException
*/
private function isUserDefinedInterface(string $interfaceName): bool
{
if (!interface_exists($interfaceName)) {
return false;
}
try {
return (new ReflectionClass($interfaceName))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @throws ReflectionException
*/
private function isUserDefinedTrait(string $traitName): bool
{
if (!trait_exists($traitName)) {
return false;
}
try {
return (new ReflectionClass($traitName))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
/**
* @throws ReflectionException
*/
private function isUserDefinedMethod(string $className, string $methodName): bool
{
if (!class_exists($className)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}
if (!method_exists($className, $methodName)) {
// @codeCoverageIgnoreStart
return false;
// @codeCoverageIgnoreEnd
}
try {
return (new ReflectionMethod($className, $methodName))->isUserDefined();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd
}
}

View File

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
/**
* @psalm-immutable
*/
final class TraitMethodUnit extends CodeUnit
{
/**
* @psalm-assert-if-true TraitMethodUnit $this
*/
public function isTraitMethod(): bool
{
return true;
}
}

View File

@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
/**
* @psalm-immutable
*/
final class TraitUnit extends CodeUnit
{
/**
* @psalm-assert-if-true TraitUnit $this
*/
public function isTrait(): bool
{
return true;
}
}

View File

@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
use Throwable;
interface Exception extends Throwable
{
}

View File

@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
use RuntimeException;
final class InvalidCodeUnitException extends RuntimeException implements Exception
{
}

View File

@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
use RuntimeException;
final class NoTraitException extends RuntimeException implements Exception
{
}

View File

@ -0,0 +1,16 @@
<?php declare(strict_types=1);
/*
* This file is part of sebastian/code-unit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\CodeUnit;
use RuntimeException;
final class ReflectionException extends RuntimeException implements Exception
{
}