pax_global_header00006660000000000000000000000064137657626360014537gustar00rootroot0000000000000052 comment=7ba5bb0211e1e846189108cd8b4b35d59f06ad3f phpunit-code-unit-1.0.8/000077500000000000000000000000001376576263600151215ustar00rootroot00000000000000phpunit-code-unit-1.0.8/.github/000077500000000000000000000000001376576263600164615ustar00rootroot00000000000000phpunit-code-unit-1.0.8/.github/FUNDING.yml000066400000000000000000000000321376576263600202710ustar00rootroot00000000000000github: sebastianbergmann phpunit-code-unit-1.0.8/.github/workflows/000077500000000000000000000000001376576263600205165ustar00rootroot00000000000000phpunit-code-unit-1.0.8/.github/workflows/ci.yml000066400000000000000000000052441376576263600216410ustar00rootroot00000000000000# https://help.github.com/en/categories/automating-your-workflow-with-github-actions on: - "pull_request" - "push" name: "CI" jobs: coding-guidelines: name: "Coding Guidelines" runs-on: "ubuntu-latest" steps: - name: "Checkout" uses: "actions/checkout@v2" - name: "Run friendsofphp/php-cs-fixer" run: "php7.4 ./tools/php-cs-fixer fix --diff-format=udiff --dry-run --show-progress=dots --using-cache=no --verbose" type-checker: name: "Type Checker" runs-on: "ubuntu-latest" steps: - name: "Checkout" uses: "actions/checkout@v2" - name: "Update dependencies with composer" run: "php7.4 ./tools/composer update --no-ansi --no-interaction --no-progress" - name: "Run vimeo/psalm" run: "php7.4 ./tools/psalm --config=.psalm/config.xml --no-progress --shepherd --show-info=false --stats" backward-compatibility: name: Backward Compatibility runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 with: fetch-depth: 0 - name: Fetch tags run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* - name: Install PHP with extensions uses: shivammathur/setup-php@v2 with: php-version: 7.4 coverage: none extensions: intl - name: Run roave/backward-compatibility-check run: ./tools/roave-backward-compatibility-check --from=1.0.8 tests: name: "Tests" runs-on: "ubuntu-latest" strategy: fail-fast: false matrix: php-version: - "7.3" - "7.4" - "8.0" - "8.1" steps: - name: "Checkout" uses: "actions/checkout@v2" - name: "Install PHP with extensions" uses: "shivammathur/setup-php@v2" with: php-version: "${{ matrix.php-version }}" coverage: "pcov" - name: "Cache dependencies installed with composer" uses: "actions/cache@v1" with: path: "~/.composer/cache" key: "php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('**/composer.json') }}" restore-keys: "php${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-" - name: "Install dependencies with composer" run: "./tools/composer update --no-ansi --no-interaction --no-progress" - name: "Run tests with phpunit/phpunit" run: "vendor/bin/phpunit --coverage-clover=coverage.xml" - name: "Send code coverage report to Codecov.io" env: CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}" run: "bash <(curl -s https://codecov.io/bash) || true" phpunit-code-unit-1.0.8/.gitignore000066400000000000000000000001151376576263600171060ustar00rootroot00000000000000/.idea /.php_cs /.php_cs.cache /.phpunit.result.cache /composer.lock /vendor phpunit-code-unit-1.0.8/.phive/000077500000000000000000000000001376576263600163125ustar00rootroot00000000000000phpunit-code-unit-1.0.8/.phive/phars.xml000066400000000000000000000010401376576263600201440ustar00rootroot00000000000000 phpunit-code-unit-1.0.8/.php_cs.dist000066400000000000000000000173731376576263600173530ustar00rootroot00000000000000 For the full copyright and license information, please view the LICENSE file that was distributed with this source code. EOF; $finder = PhpCsFixer\Finder::create() ->files() ->in(__DIR__ . '/src') ->in(__DIR__ . '/tests'); return PhpCsFixer\Config::create() ->setFinder($finder) ->setRiskyAllowed(true) ->setRules([ 'align_multiline_comment' => true, 'array_indentation' => true, 'array_syntax' => ['syntax' => 'short'], 'binary_operator_spaces' => [ 'operators' => [ '=' => 'align', '=>' => 'align', ], ], 'blank_line_after_namespace' => true, 'blank_line_before_statement' => [ 'statements' => [ 'break', 'continue', 'declare', 'do', 'for', 'foreach', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', 'yield', ], ], 'braces' => true, 'cast_spaces' => true, 'class_attributes_separation' => ['elements' => ['const', 'method', 'property']], 'combine_consecutive_issets' => true, 'combine_consecutive_unsets' => true, 'compact_nullable_typehint' => true, 'concat_space' => ['spacing' => 'one'], 'declare_equal_normalize' => ['space' => 'none'], 'declare_strict_types' => true, 'dir_constant' => true, 'elseif' => true, 'encoding' => true, 'full_opening_tag' => true, 'function_declaration' => true, 'global_namespace_import' => [ 'import_classes' => true, 'import_constants' => true, 'import_functions' => true, ], 'header_comment' => ['header' => $header, 'separate' => 'none'], 'indentation_type' => true, 'is_null' => true, 'line_ending' => true, 'list_syntax' => ['syntax' => 'short'], 'logical_operators' => true, 'lowercase_cast' => true, 'lowercase_constants' => true, 'lowercase_keywords' => true, 'lowercase_static_reference' => true, 'magic_constant_casing' => true, 'method_argument_space' => ['ensure_fully_multiline' => true], 'modernize_types_casting' => true, 'multiline_comment_opening_closing' => true, 'multiline_whitespace_before_semicolons' => true, 'native_constant_invocation' => false, 'native_function_casing' => false, 'native_function_invocation' => false, 'new_with_braces' => false, 'no_alias_functions' => true, 'no_alternative_syntax' => true, 'no_blank_lines_after_class_opening' => true, 'no_blank_lines_after_phpdoc' => true, 'no_blank_lines_before_namespace' => true, 'no_closing_tag' => true, 'no_empty_comment' => true, 'no_empty_phpdoc' => true, 'no_empty_statement' => true, 'no_extra_blank_lines' => true, 'no_homoglyph_names' => true, 'no_leading_import_slash' => true, 'no_leading_namespace_whitespace' => true, 'no_mixed_echo_print' => ['use' => 'print'], 'no_multiline_whitespace_around_double_arrow' => true, 'no_null_property_initialization' => true, 'no_php4_constructor' => true, 'no_short_bool_cast' => true, 'no_short_echo_tag' => true, 'no_singleline_whitespace_before_semicolons' => true, 'no_spaces_after_function_name' => true, 'no_spaces_inside_parenthesis' => true, 'no_superfluous_elseif' => true, 'no_superfluous_phpdoc_tags' => [ 'allow_mixed' => true, ], 'no_trailing_comma_in_list_call' => true, 'no_trailing_comma_in_singleline_array' => true, 'no_trailing_whitespace' => true, 'no_trailing_whitespace_in_comment' => true, 'no_unneeded_control_parentheses' => true, 'no_unneeded_curly_braces' => true, 'no_unneeded_final_method' => true, 'no_unreachable_default_argument_value' => true, 'no_unset_on_property' => true, 'no_unused_imports' => true, 'no_useless_else' => true, 'no_useless_return' => true, 'no_whitespace_before_comma_in_array' => true, 'no_whitespace_in_blank_line' => true, 'non_printable_character' => true, 'normalize_index_brace' => true, 'object_operator_without_whitespace' => true, 'ordered_class_elements' => [ 'order' => [ 'use_trait', 'constant_public', 'constant_protected', 'constant_private', 'property_public_static', 'property_protected_static', 'property_private_static', 'property_public', 'property_protected', 'property_private', 'method_public_static', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private', 'method_protected_static', 'method_private_static', ], ], 'ordered_imports' => [ 'imports_order' => [ PhpCsFixer\Fixer\Import\OrderedImportsFixer::IMPORT_TYPE_CONST, PhpCsFixer\Fixer\Import\OrderedImportsFixer::IMPORT_TYPE_FUNCTION, PhpCsFixer\Fixer\Import\OrderedImportsFixer::IMPORT_TYPE_CLASS, ] ], 'ordered_interfaces' => [ 'direction' => 'ascend', 'order' => 'alpha', ], 'phpdoc_add_missing_param_annotation' => false, 'phpdoc_align' => true, 'phpdoc_annotation_without_dot' => true, 'phpdoc_indent' => true, 'phpdoc_no_access' => true, 'phpdoc_no_empty_return' => true, 'phpdoc_no_package' => true, 'phpdoc_order' => true, 'phpdoc_return_self_reference' => true, 'phpdoc_scalar' => true, 'phpdoc_separation' => true, 'phpdoc_single_line_var_spacing' => true, 'phpdoc_summary' => true, 'phpdoc_to_comment' => true, 'phpdoc_trim' => true, 'phpdoc_trim_consecutive_blank_line_separation' => true, 'phpdoc_types' => ['groups' => ['simple', 'meta']], 'phpdoc_types_order' => true, 'phpdoc_var_without_name' => true, 'pow_to_exponentiation' => true, 'protected_to_private' => true, 'return_assignment' => true, 'return_type_declaration' => ['space_before' => 'none'], 'self_accessor' => true, 'semicolon_after_instruction' => true, 'set_type_to_cast' => true, 'short_scalar_cast' => true, 'simplified_null_return' => false, 'single_blank_line_at_eof' => true, 'single_import_per_statement' => true, 'single_line_after_imports' => true, 'single_quote' => true, 'standardize_not_equals' => true, 'ternary_to_null_coalescing' => true, 'trailing_comma_in_multiline_array' => true, 'trim_array_spaces' => true, 'unary_operator_spaces' => true, 'visibility_required' => [ 'elements' => [ 'const', 'method', 'property', ], ], 'void_return' => true, 'whitespace_after_comma_in_array' => true, ]); phpunit-code-unit-1.0.8/.psalm/000077500000000000000000000000001376576263600163135ustar00rootroot00000000000000phpunit-code-unit-1.0.8/.psalm/baseline.xml000066400000000000000000000012521376576263600206170ustar00rootroot00000000000000 $firstPart $firstPart $firstPart $firstPart $firstPart $firstPart $firstPart $firstPart $firstPart $secondPart $unit $unit $unit $unit $unit $unit phpunit-code-unit-1.0.8/.psalm/config.xml000066400000000000000000000007521376576263600203060ustar00rootroot00000000000000 phpunit-code-unit-1.0.8/ChangeLog.md000066400000000000000000000037621376576263600173020ustar00rootroot00000000000000# ChangeLog All notable changes are documented in this file using the [Keep a CHANGELOG](http://keepachangelog.com/) principles. ## [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` is used for class that extends built-in class ## [1.0.0] - 2020-03-30 * Initial release [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 phpunit-code-unit-1.0.8/LICENSE000066400000000000000000000030151376576263600161250ustar00rootroot00000000000000sebastian/code-unit Copyright (c) 2020, 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: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 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. * Neither the name of Sebastian Bergmann nor the names of his 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 OWNER 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. phpunit-code-unit-1.0.8/README.md000066400000000000000000000007351376576263600164050ustar00rootroot00000000000000# 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 ``` phpunit-code-unit-1.0.8/build.xml000066400000000000000000000020231376576263600167370ustar00rootroot00000000000000 phpunit-code-unit-1.0.8/composer.json000066400000000000000000000022341376576263600176440ustar00rootroot00000000000000{ "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": ">=7.3" }, "require-dev": { "phpunit/phpunit": "^9.3" }, "config": { "platform": { "php": "7.3.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-master": "1.0-dev" } } } phpunit-code-unit-1.0.8/infection.json000066400000000000000000000007471376576263600200020ustar00rootroot00000000000000{ "source": { "directories": [ "src" ] }, "mutators": { "@default": true, "Break_": { "ignore": [ "SebastianBergmann\\CodeUnit\\Mapper::classAndParentClassesAndTraits" ] }, "UnwrapArrayValues": { "ignore": [ "SebastianBergmann\\CodeUnit\\Mapper::codeUnitsToSourceLines" ] } }, "minMsi": 100, "minCoveredMsi": 100 } phpunit-code-unit-1.0.8/phpunit.xml000066400000000000000000000015121376576263600173310ustar00rootroot00000000000000 tests src phpunit-code-unit-1.0.8/src/000077500000000000000000000000001376576263600157105ustar00rootroot00000000000000phpunit-code-unit-1.0.8/src/ClassMethodUnit.php000066400000000000000000000010131376576263600214620ustar00rootroot00000000000000 * * 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; } } phpunit-code-unit-1.0.8/src/ClassUnit.php000066400000000000000000000007711376576263600203330ustar00rootroot00000000000000 * * 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; } } phpunit-code-unit-1.0.8/src/CodeUnit.php000066400000000000000000000263271376576263600201450ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use function range; use function sprintf; use ReflectionClass; use ReflectionFunction; use ReflectionMethod; /** * @psalm-immutable */ abstract class CodeUnit { /** * @var string */ private $name; /** * @var string */ private $sourceFileName; /** * @var array * @psalm-var list */ private $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() ) ); } /** * @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 $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 */ 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; } /** * @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(), (int) $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(), (int) $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(), (int) $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(), (int) $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(), (int) $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(), (int) $e->getCode(), $e ); } // @codeCoverageIgnoreEnd } } phpunit-code-unit-1.0.8/src/CodeUnitCollection.php000066400000000000000000000032751376576263600221560ustar00rootroot00000000000000 * * 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; final class CodeUnitCollection implements Countable, IteratorAggregate { /** * @psalm-var list */ private $codeUnits = []; /** * @psalm-param list $items */ public static function fromArray(array $items): self { $collection = new self; foreach ($items as $item) { $collection->add($item); } return $collection; } public static function fromList(CodeUnit ...$items): self { return self::fromArray($items); } private function __construct() { } /** * @psalm-return list */ 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 self::fromArray( array_merge( $this->asArray(), $other->asArray() ) ); } private function add(CodeUnit $item): void { $this->codeUnits[] = $item; } } phpunit-code-unit-1.0.8/src/CodeUnitCollectionIterator.php000066400000000000000000000020641376576263600236630ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use Iterator; final class CodeUnitCollectionIterator implements Iterator { /** * @psalm-var list */ private $codeUnits; /** * @var int */ private $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++; } } phpunit-code-unit-1.0.8/src/FunctionUnit.php000066400000000000000000000010021376576263600210370ustar00rootroot00000000000000 * * 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; } } phpunit-code-unit-1.0.8/src/InterfaceMethodUnit.php000066400000000000000000000010231376576263600223160ustar00rootroot00000000000000 * * 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; } } phpunit-code-unit-1.0.8/src/InterfaceUnit.php000066400000000000000000000010051376576263600211550ustar00rootroot00000000000000 * * 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; } } phpunit-code-unit-1.0.8/src/Mapper.php000066400000000000000000000276461376576263600176640ustar00rootroot00000000000000 * * 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_replace; use function strpos; use function trait_exists; use ReflectionClass; use ReflectionFunction; use ReflectionMethod; final class Mapper { /** * @psalm-return array> */ 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 (strpos($unit, '::') !== false) { [$firstPart, $secondPart] = explode('::', $unit); if (empty($firstPart) && $this->isUserDefinedFunction($secondPart)) { return CodeUnitCollection::fromList(CodeUnit::forFunction($secondPart)); } if ($this->isUserDefinedClass($firstPart)) { if ($secondPart === '') { return $this->publicMethodsOfClass($firstPart); } if ($secondPart === '') { return $this->protectedAndPrivateMethodsOfClass($firstPart); } if ($secondPart === '') { return $this->protectedMethodsOfClass($firstPart); } if ($secondPart === '') { return $this->publicAndPrivateMethodsOfClass($firstPart); } if ($secondPart === '') { return $this->privateMethodsOfClass($firstPart); } if ($secondPart === '') { return $this->publicAndProtectedMethodsOfClass($firstPart); } 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::fromArray($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)); } $unit = str_replace('', '', $unit); if ($this->isUserDefinedClass($unit)) { return $this->classAndParentClassesAndTraits($unit); } } throw new InvalidCodeUnitException( sprintf( '"%s" is not a valid code unit', $unit ) ); } /** * @psalm-param class-string $className * * @throws ReflectionException */ private function publicMethodsOfClass(string $className): CodeUnitCollection { return $this->methodsOfClass($className, ReflectionMethod::IS_PUBLIC); } /** * @psalm-param class-string $className * * @throws ReflectionException */ private function publicAndProtectedMethodsOfClass(string $className): CodeUnitCollection { return $this->methodsOfClass($className, ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED); } /** * @psalm-param class-string $className * * @throws ReflectionException */ private function publicAndPrivateMethodsOfClass(string $className): CodeUnitCollection { return $this->methodsOfClass($className, ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PRIVATE); } /** * @psalm-param class-string $className * * @throws ReflectionException */ private function protectedMethodsOfClass(string $className): CodeUnitCollection { return $this->methodsOfClass($className, ReflectionMethod::IS_PROTECTED); } /** * @psalm-param class-string $className * * @throws ReflectionException */ private function protectedAndPrivateMethodsOfClass(string $className): CodeUnitCollection { return $this->methodsOfClass($className, ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_PRIVATE); } /** * @psalm-param class-string $className * * @throws ReflectionException */ private function privateMethodsOfClass(string $className): CodeUnitCollection { return $this->methodsOfClass($className, ReflectionMethod::IS_PRIVATE); } /** * @psalm-param class-string $className * * @throws ReflectionException */ private function methodsOfClass(string $className, int $filter): CodeUnitCollection { $units = []; foreach ($this->reflectorForClass($className)->getMethods($filter) as $method) { if (!$method->isUserDefined()) { continue; } $units[] = CodeUnit::forClassMethod($className, $method->getName()); } return CodeUnitCollection::fromArray($units); } /** * @psalm-param class-string $className * * @throws ReflectionException */ private function classAndParentClassesAndTraits(string $className): CodeUnitCollection { $units = [CodeUnit::forClass($className)]; $reflector = $this->reflectorForClass($className); foreach ($this->reflectorForClass($className)->getTraits() as $trait) { if (!$trait->isUserDefined()) { // @codeCoverageIgnoreStart continue; // @codeCoverageIgnoreEnd } $units[] = CodeUnit::forTrait($trait->getName()); } while ($reflector = $reflector->getParentClass()) { if (!$reflector->isUserDefined()) { break; } $units[] = CodeUnit::forClass($reflector->getName()); foreach ($reflector->getTraits() as $trait) { if (!$trait->isUserDefined()) { // @codeCoverageIgnoreStart continue; // @codeCoverageIgnoreEnd } $units[] = CodeUnit::forTrait($trait->getName()); } } return CodeUnitCollection::fromArray($units); } /** * @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(), (int) $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(), (int) $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(), (int) $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(), (int) $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(), (int) $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(), (int) $e->getCode(), $e ); } // @codeCoverageIgnoreEnd } } phpunit-code-unit-1.0.8/src/TraitMethodUnit.php000066400000000000000000000010131376576263600215000ustar00rootroot00000000000000 * * 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; } } phpunit-code-unit-1.0.8/src/TraitUnit.php000066400000000000000000000007711376576263600203510ustar00rootroot00000000000000 * * 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; } } phpunit-code-unit-1.0.8/src/exceptions/000077500000000000000000000000001376576263600200715ustar00rootroot00000000000000phpunit-code-unit-1.0.8/src/exceptions/Exception.php000066400000000000000000000005521376576263600225420ustar00rootroot00000000000000 * * 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 { } phpunit-code-unit-1.0.8/src/exceptions/InvalidCodeUnitException.php000066400000000000000000000006361376576263600255070ustar00rootroot00000000000000 * * 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 { } phpunit-code-unit-1.0.8/src/exceptions/NoTraitException.php000066400000000000000000000006261376576263600240450ustar00rootroot00000000000000 * * 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 { } phpunit-code-unit-1.0.8/src/exceptions/ReflectionException.php000066400000000000000000000006311376576263600245530ustar00rootroot00000000000000 * * 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 { } phpunit-code-unit-1.0.8/tests/000077500000000000000000000000001376576263600162635ustar00rootroot00000000000000phpunit-code-unit-1.0.8/tests/_fixture/000077500000000000000000000000001376576263600201105ustar00rootroot00000000000000phpunit-code-unit-1.0.8/tests/_fixture/FixtureAnotherTrait.php000066400000000000000000000006331376576263600245760ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; trait FixtureAnotherTrait { public function anotherMethod(): void { // ... } } phpunit-code-unit-1.0.8/tests/_fixture/FixtureChildClassWithTrait.php000066400000000000000000000007511376576263600260440ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; final class FixtureChildClassWithTrait extends FixtureParentClassWithTrait { use FixtureAnotherTrait; public function publicMethod(): void { // ... } } phpunit-code-unit-1.0.8/tests/_fixture/FixtureClass.php000066400000000000000000000010451376576263600232350ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; class FixtureClass { public function publicMethod(): void { // ... } protected function protectedMethod(): void { // ... } private function privateMethod(): void { // ... } } phpunit-code-unit-1.0.8/tests/_fixture/FixtureClassWithTrait.php000066400000000000000000000006771376576263600251070ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; final class FixtureClassWithTrait { use FixtureTrait; protected function protectedMethod(): void { // ... } } phpunit-code-unit-1.0.8/tests/_fixture/FixtureInterface.php000066400000000000000000000005731376576263600240750ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; interface FixtureInterface { public function method(): void; } phpunit-code-unit-1.0.8/tests/_fixture/FixtureParentClassWithTrait.php000066400000000000000000000007311376576263600262500ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; use stdClass; class FixtureParentClassWithTrait extends stdClass { use FixtureTrait; public function publicMethod(): void { // ... } } phpunit-code-unit-1.0.8/tests/_fixture/FixtureTrait.php000066400000000000000000000006151376576263600232550ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; trait FixtureTrait { public function method(): void { // ... } } phpunit-code-unit-1.0.8/tests/_fixture/file_with_multiple_code_units.php000066400000000000000000000007301376576263600267220ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; abstract class FixtureAnotherParentClass { } final class FixtureAnotherChildClass extends FixtureAnotherParentClass { } function another_function(): void { } phpunit-code-unit-1.0.8/tests/_fixture/function.php000066400000000000000000000006601376576263600224500ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture { function f(): int { return 1; } } namespace { function f(): int { return 1; } } phpunit-code-unit-1.0.8/tests/_fixture/issue_3.php000066400000000000000000000007271376576263600222010ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; final class Getopt { public static function getopt(array $args, string $short_options, array $long_options = null): array { return []; } } phpunit-code-unit-1.0.8/tests/_fixture/issue_5.php000066400000000000000000000005641376576263600222020ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit\Fixture; use Exception; class Issue5Exception extends Exception { } phpunit-code-unit-1.0.8/tests/unit/000077500000000000000000000000001376576263600172425ustar00rootroot00000000000000phpunit-code-unit-1.0.8/tests/unit/ClassMethodUnitTest.php000066400000000000000000000044371376576263600236710ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use function range; use function realpath; use Exception; use PHPUnit\Framework\TestCase; use SebastianBergmann\CodeUnit\Fixture\FixtureClass; use SebastianBergmann\CodeUnit\Fixture\FixtureInterface; use SebastianBergmann\CodeUnit\Fixture\FixtureTrait; /** * @covers \SebastianBergmann\CodeUnit\ClassMethodUnit * @covers \SebastianBergmann\CodeUnit\CodeUnit * * @uses \SebastianBergmann\CodeUnit\CodeUnitCollection * @uses \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator * @uses \SebastianBergmann\CodeUnit\Mapper * * @testdox ClassMethodUnit */ final class ClassMethodUnitTest extends TestCase { public function testCanBeCreatedFromNameOfUserDefinedClassAndMethodName(): void { $unit = CodeUnit::forClassMethod(FixtureClass::class, 'publicMethod'); $this->assertFalse($unit->isClass()); $this->assertTrue($unit->isClassMethod()); $this->assertFalse($unit->isInterface()); $this->assertFalse($unit->isInterfaceMethod()); $this->assertFalse($unit->isTrait()); $this->assertFalse($unit->isTraitMethod()); $this->assertFalse($unit->isFunction()); $this->assertSame(FixtureClass::class . '::publicMethod', $unit->name()); $this->assertSame(realpath(__DIR__ . '/../_fixture/FixtureClass.php'), $unit->sourceFileName()); $this->assertSame(range(14, 17), $unit->sourceLines()); } public function testCannotBeCreatedForMethodOfInternalClass(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forClassMethod(Exception::class, 'getMessage'); } public function testCannotBeCreatedForInterfaceMethod(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forClassMethod(FixtureInterface::class, 'method'); } public function testCannotBeCreatedForTraitMethod(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forClassMethod(FixtureTrait::class, 'method'); } } phpunit-code-unit-1.0.8/tests/unit/ClassUnitTest.php000066400000000000000000000042171376576263600225240ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use function range; use function realpath; use Exception; use PHPUnit\Framework\TestCase; use SebastianBergmann\CodeUnit\Fixture\FixtureClass; use SebastianBergmann\CodeUnit\Fixture\FixtureInterface; use SebastianBergmann\CodeUnit\Fixture\FixtureTrait; /** * @covers \SebastianBergmann\CodeUnit\ClassUnit * @covers \SebastianBergmann\CodeUnit\CodeUnit * * @uses \SebastianBergmann\CodeUnit\CodeUnitCollection * @uses \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator * @uses \SebastianBergmann\CodeUnit\Mapper * * @testdox ClassUnit */ final class ClassUnitTest extends TestCase { public function testCanBeCreatedFromNameOfUserDefinedClass(): void { $unit = CodeUnit::forClass(FixtureClass::class); $this->assertTrue($unit->isClass()); $this->assertFalse($unit->isClassMethod()); $this->assertFalse($unit->isInterface()); $this->assertFalse($unit->isInterfaceMethod()); $this->assertFalse($unit->isTrait()); $this->assertFalse($unit->isTraitMethod()); $this->assertFalse($unit->isFunction()); $this->assertSame(FixtureClass::class, $unit->name()); $this->assertSame(realpath(__DIR__ . '/../_fixture/FixtureClass.php'), $unit->sourceFileName()); $this->assertSame(range(12, 28), $unit->sourceLines()); } public function testCannotBeCreatedForInternalClass(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forClass(Exception::class); } public function testCannotBeCreatedForInterface(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forClass(FixtureInterface::class); } public function testCannotBeCreatedForTrait(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forClass(FixtureTrait::class); } } phpunit-code-unit-1.0.8/tests/unit/CodeUnitCollectionTest.php000066400000000000000000000053771376576263600243550ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use PHPUnit\Framework\TestCase; use SebastianBergmann\CodeUnit\Fixture\FixtureClass; use SebastianBergmann\CodeUnit\Fixture\FixtureInterface; /** * @covers \SebastianBergmann\CodeUnit\CodeUnitCollection * @covers \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator * * @uses \SebastianBergmann\CodeUnit\CodeUnit * @uses \SebastianBergmann\CodeUnit\Mapper * * @testdox CodeUnitCollection */ final class CodeUnitCollectionTest extends TestCase { /** * @var InterfaceUnit */ private $interface; /** * @var ClassUnit */ private $class; protected function setUp(): void { $this->interface = CodeUnit::forInterface(FixtureInterface::class); $this->class = CodeUnit::forClass(FixtureClass::class); } /** * @testdox Can be created from array of CodeUnit objects */ public function testCanBeCreatedFromArrayOfObjects(): void { $collection = CodeUnitCollection::fromArray([$this->interface, $this->class]); $this->assertSame([$this->interface, $this->class], $collection->asArray()); } /** * @testdox Can be created from list of CodeUnit objects */ public function testCanBeCreatedFromListOfObjects(): void { $collection = CodeUnitCollection::fromList($this->interface, $this->class); $this->assertSame([$this->interface, $this->class], $collection->asArray()); } public function testCanBeCounted(): void { $collection = CodeUnitCollection::fromList($this->interface, $this->class); $this->assertCount(2, $collection); $this->assertFalse($collection->isEmpty()); } public function testCanBeIterated(): void { $array = []; foreach (CodeUnitCollection::fromList($this->interface, $this->class) as $key => $value) { $array[$key] = $value; } $this->assertCount(2, $array); $this->assertArrayHasKey(0, $array); $this->assertSame($this->interface, $array[0]); $this->assertArrayHasKey(1, $array); $this->assertSame($this->class, $array[1]); } public function testCanBeMergedWithAnotherCollectionOfCodeUnitObjects(): void { $this->assertSame( [ $this->class, $this->interface, ], CodeUnitCollection::fromList($this->class)->mergeWith( CodeUnitCollection::fromList($this->interface) )->asArray() ); } } phpunit-code-unit-1.0.8/tests/unit/FunctionUnitTest.php000066400000000000000000000032121376576263600232360ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use function range; use function realpath; use PHPUnit\Framework\TestCase; /** * @covers \SebastianBergmann\CodeUnit\FunctionUnit * @covers \SebastianBergmann\CodeUnit\CodeUnit * * @uses \SebastianBergmann\CodeUnit\CodeUnitCollection * @uses \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator * @uses \SebastianBergmann\CodeUnit\Mapper * * @testdox FunctionUnit */ final class FunctionUnitTest extends TestCase { public function testCanBeCreatedFromNameOfUserFunction(): void { $unit = CodeUnit::forFunction('SebastianBergmann\CodeUnit\Fixture\f'); $this->assertFalse($unit->isClass()); $this->assertFalse($unit->isClassMethod()); $this->assertFalse($unit->isInterface()); $this->assertFalse($unit->isInterfaceMethod()); $this->assertFalse($unit->isTrait()); $this->assertFalse($unit->isTraitMethod()); $this->assertTrue($unit->isFunction()); $this->assertSame('SebastianBergmann\CodeUnit\Fixture\f', $unit->name()); $this->assertSame(realpath(__DIR__ . '/../_fixture/function.php'), $unit->sourceFileName()); $this->assertSame(range(12, 15), $unit->sourceLines()); } public function testCannotBeCreatedForInternalFunction(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forFunction('abs'); } } phpunit-code-unit-1.0.8/tests/unit/InterfaceMethodUnitTest.php000066400000000000000000000044371376576263600245240ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use function realpath; use Iterator; use PHPUnit\Framework\TestCase; use SebastianBergmann\CodeUnit\Fixture\FixtureClass; use SebastianBergmann\CodeUnit\Fixture\FixtureInterface; use SebastianBergmann\CodeUnit\Fixture\FixtureTrait; /** * @covers \SebastianBergmann\CodeUnit\InterfaceMethodUnit * @covers \SebastianBergmann\CodeUnit\CodeUnit * * @uses \SebastianBergmann\CodeUnit\CodeUnitCollection * @uses \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator * @uses \SebastianBergmann\CodeUnit\Mapper * * @testdox InterfaceMethodUnit */ final class InterfaceMethodUnitTest extends TestCase { public function testCanBeCreatedFromNameOfUserDefinedInterfaceAndMethodName(): void { $unit = CodeUnit::forInterfaceMethod(FixtureInterface::class, 'method'); $this->assertFalse($unit->isClass()); $this->assertFalse($unit->isClassMethod()); $this->assertFalse($unit->isInterface()); $this->assertTrue($unit->isInterfaceMethod()); $this->assertFalse($unit->isTrait()); $this->assertFalse($unit->isTraitMethod()); $this->assertFalse($unit->isFunction()); $this->assertSame(FixtureInterface::class . '::method', $unit->name()); $this->assertSame(realpath(__DIR__ . '/../_fixture/FixtureInterface.php'), $unit->sourceFileName()); $this->assertSame([14], $unit->sourceLines()); } public function testCannotBeCreatedForMethodOfInternalInterface(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forInterfaceMethod(Iterator::class, 'current'); } public function testCannotBeCreatedForClassMethod(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forInterfaceMethod(FixtureClass::class, 'publicMethod'); } public function testCannotBeCreatedForTraitMethod(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forInterfaceMethod(FixtureTrait::class, 'method'); } } phpunit-code-unit-1.0.8/tests/unit/InterfaceUnitTest.php000066400000000000000000000042651376576263600233620ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use function range; use function realpath; use Iterator; use PHPUnit\Framework\TestCase; use SebastianBergmann\CodeUnit\Fixture\FixtureClass; use SebastianBergmann\CodeUnit\Fixture\FixtureInterface; use SebastianBergmann\CodeUnit\Fixture\FixtureTrait; /** * @covers \SebastianBergmann\CodeUnit\InterfaceUnit * @covers \SebastianBergmann\CodeUnit\CodeUnit * * @uses \SebastianBergmann\CodeUnit\CodeUnitCollection * @uses \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator * @uses \SebastianBergmann\CodeUnit\Mapper * * @testdox InterfaceUnit */ final class InterfaceUnitTest extends TestCase { public function testCanBeCreatedFromNameOfUserDefinedInterface(): void { $unit = CodeUnit::forInterface(FixtureInterface::class); $this->assertFalse($unit->isClass()); $this->assertFalse($unit->isClassMethod()); $this->assertTrue($unit->isInterface()); $this->assertFalse($unit->isInterfaceMethod()); $this->assertFalse($unit->isTrait()); $this->assertFalse($unit->isTraitMethod()); $this->assertFalse($unit->isFunction()); $this->assertSame(FixtureInterface::class, $unit->name()); $this->assertSame(realpath(__DIR__ . '/../_fixture/FixtureInterface.php'), $unit->sourceFileName()); $this->assertSame(range(12, 15), $unit->sourceLines()); } public function testCannotBeCreatedForInternalInterface(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forInterface(Iterator::class); } public function testCannotBeCreatedForClass(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forInterface(FixtureClass::class); } public function testCannotBeCreatedForTrait(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forInterface(FixtureTrait::class); } } phpunit-code-unit-1.0.8/tests/unit/MapperTest.php000066400000000000000000000255151376576263600220470ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use function range; use function realpath; use PHPUnit\Framework\TestCase; use SebastianBergmann\CodeUnit\Fixture\FixtureAnotherChildClass; use SebastianBergmann\CodeUnit\Fixture\FixtureAnotherParentClass; use SebastianBergmann\CodeUnit\Fixture\FixtureAnotherTrait; use SebastianBergmann\CodeUnit\Fixture\FixtureChildClassWithTrait; use SebastianBergmann\CodeUnit\Fixture\FixtureClass; use SebastianBergmann\CodeUnit\Fixture\FixtureClassWithTrait; use SebastianBergmann\CodeUnit\Fixture\FixtureInterface; use SebastianBergmann\CodeUnit\Fixture\FixtureParentClassWithTrait; use SebastianBergmann\CodeUnit\Fixture\FixtureTrait; use SebastianBergmann\CodeUnit\Fixture\Getopt; use SebastianBergmann\CodeUnit\Fixture\Issue5Exception; /** * @covers \SebastianBergmann\CodeUnit\Mapper * * @uses \SebastianBergmann\CodeUnit\CodeUnit * @uses \SebastianBergmann\CodeUnit\CodeUnitCollection * @uses \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator */ final class MapperTest extends TestCase { /** * @testdox Can map 'function_name' string to code unit objects */ public function testCanMapStringWithFunctionNameToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits('SebastianBergmann\CodeUnit\Fixture\f'); $this->assertSame('SebastianBergmann\CodeUnit\Fixture\f', $units->asArray()[0]->name()); } /** * @testdox Can map '::function_name' string to code unit objects */ public function testCanMapStringWithFunctionNamePrefixedWithDoubleColonsToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits('::f'); $this->assertSame('f', $units->asArray()[0]->name()); } /** * @testdox Can map 'ClassName' string to code unit objects */ public function testCanMapStringWithClassNameToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureClass::class); $this->assertCount(1, $units); $this->assertSame(FixtureClass::class, $units->asArray()[0]->name()); } /** * @testdox Can map 'ClassName' string to code unit objects */ public function testMapClassesAndTheTraitsTheyUseToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureClassWithTrait::class); $this->assertCount(2, $units); $this->assertSame(FixtureClassWithTrait::class, $units->asArray()[0]->name()); $this->assertSame(FixtureTrait::class, $units->asArray()[1]->name()); } /** * @testdox Can map 'ClassName' string to code unit objects */ public function testCanMapStringWithClassNameAndSelectorForParentClassesToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureChildClassWithTrait::class . ''); $this->assertCount(4, $units); $this->assertSame(FixtureChildClassWithTrait::class, $units->asArray()[0]->name()); $this->assertSame(FixtureAnotherTrait::class, $units->asArray()[1]->name()); $this->assertSame(FixtureParentClassWithTrait::class, $units->asArray()[2]->name()); $this->assertSame(FixtureTrait::class, $units->asArray()[3]->name()); } /** * @testdox Can map 'ClassName::methodName' string to code unit objects */ public function testCanMapStringWithClassNameAndMethodNameToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureClass::class . '::publicMethod'); $this->assertSame(FixtureClass::class . '::publicMethod', $units->asArray()[0]->name()); } /** * @testdox Can map 'ClassName::' string to code unit objects */ public function testCanMapStringWithClassNameAndPublicMethodSelectorToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureClass::class . '::'); $this->assertCount(1, $units); $this->assertSame(FixtureClass::class . '::publicMethod', $units->asArray()[0]->name()); } /** * @testdox Can map 'ClassName::' string to code unit objects */ public function testCanMapStringWithClassNameAndNoPublicMethodSelectorToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureClass::class . '::'); $this->assertCount(2, $units); $this->assertSame(FixtureClass::class . '::protectedMethod', $units->asArray()[0]->name()); $this->assertSame(FixtureClass::class . '::privateMethod', $units->asArray()[1]->name()); } /** * @testdox Can map 'ClassName::' string to code unit objects */ public function testCanMapStringWithClassNameAndProtectedMethodSelectorToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureClass::class . '::'); $this->assertCount(1, $units); $this->assertSame(FixtureClass::class . '::protectedMethod', $units->asArray()[0]->name()); } /** * @testdox Can map 'ClassName::' string to code unit objects */ public function testCanMapStringWithClassNameAndNoProtectedMethodSelectorToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureClass::class . '::'); $this->assertCount(2, $units); $this->assertSame(FixtureClass::class . '::publicMethod', $units->asArray()[0]->name()); $this->assertSame(FixtureClass::class . '::privateMethod', $units->asArray()[1]->name()); } /** * @testdox Can map 'ClassName::' string to code unit objects */ public function testCanMapStringWithClassNameAndPrivateMethodSelectorToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureClass::class . '::'); $this->assertCount(1, $units); $this->assertSame(FixtureClass::class . '::privateMethod', $units->asArray()[0]->name()); } /** * @testdox Can map 'ClassName::' string to code unit objects */ public function testCanMapStringWithClassNameAndNoPrivateMethodSelectorToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureClass::class . '::'); $this->assertCount(2, $units); $this->assertSame(FixtureClass::class . '::publicMethod', $units->asArray()[0]->name()); $this->assertSame(FixtureClass::class . '::protectedMethod', $units->asArray()[1]->name()); } /** * @testdox Can map 'InterfaceName' string to code unit objects */ public function testCanMapStringWithInterfaceNameToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureInterface::class); $this->assertSame(FixtureInterface::class, $units->asArray()[0]->name()); } /** * @testdox Can map 'InterfaceName::methodName' string to code unit objects */ public function testCanMapStringWithInterfaceNameAndMethodNameToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureInterface::class . '::method'); $this->assertSame(FixtureInterface::class . '::method', $units->asArray()[0]->name()); } /** * @testdox Can map 'TraitName' string to code unit objects */ public function testCanMapStringWithTraitNameToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureTrait::class); $this->assertSame(FixtureTrait::class, $units->asArray()[0]->name()); } /** * @testdox Can map 'TraitName::methodName' string to code unit objects */ public function testCanMapStringWithTraitNameAndMethodNameToCodeUnitObjects(): void { $units = (new Mapper)->stringToCodeUnits(FixtureTrait::class . '::method'); $this->assertSame(FixtureTrait::class . '::method', $units->asArray()[0]->name()); } public function testCannotMapInvalidStringToCodeUnitObjects(): void { $this->expectException(InvalidCodeUnitException::class); (new Mapper)->stringToCodeUnits('invalid'); } public function testCanMapCodeUnitObjectsToArrayWithSourceLinesInSingleFile(): void { $codeUnits = CodeUnitCollection::fromList( CodeUnit::forFunction('SebastianBergmann\CodeUnit\Fixture\another_function'), CodeUnit::forClass(FixtureAnotherParentClass::class), CodeUnit::forClass(FixtureAnotherChildClass::class), ); $this->assertSame( [ realpath(__DIR__ . '/../_fixture/file_with_multiple_code_units.php') => [ 12, 13, 14, 16, 17, 18, 20, 21, 22, ], ], (new Mapper)->codeUnitsToSourceLines($codeUnits) ); } public function testCanMapCodeUnitObjectsToArrayWithSourceLinesInMultipleFiles(): void { $codeUnits = CodeUnitCollection::fromList( CodeUnit::forInterface(FixtureInterface::class), CodeUnit::forClass(FixtureClass::class), CodeUnit::forClass(FixtureClass::class) ); $this->assertSame( [ realpath(__DIR__ . '/../_fixture/FixtureClass.php') => range(12, 28), realpath(__DIR__ . '/../_fixture/FixtureInterface.php') => range(12, 15), ], (new Mapper)->codeUnitsToSourceLines($codeUnits) ); } public function testCanMapOverlappingCodeUnitObjectsToArrayWithSourceLines(): void { $codeUnits = CodeUnitCollection::fromList( CodeUnit::forClass(FixtureClass::class), CodeUnit::forClassMethod(FixtureClass::class, 'publicMethod'), CodeUnit::forClassMethod(FixtureClass::class, 'protectedMethod'), CodeUnit::forClassMethod(FixtureClass::class, 'privateMethod'), ); $this->assertSame( [ realpath(__DIR__ . '/../_fixture/FixtureClass.php') => range(12, 28), ], (new Mapper)->codeUnitsToSourceLines($codeUnits) ); } /** * @ticket https://github.com/sebastianbergmann/code-unit/issues/3 */ public function testIssue3(): void { $units = (new Mapper)->stringToCodeUnits(Getopt::class . '::getopt'); $this->assertSame(Getopt::class . '::getopt', $units->asArray()[0]->name()); } /** * @ticket https://github.com/sebastianbergmann/code-unit/issues/5 */ public function testIssue5(): void { $units = (new Mapper)->stringToCodeUnits(Issue5Exception::class . '::'); $this->assertCount(0, $units->asArray()); } } phpunit-code-unit-1.0.8/tests/unit/TraitMethodUnitTest.php000066400000000000000000000040611376576263600237000ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use function range; use function realpath; use PHPUnit\Framework\TestCase; use SebastianBergmann\CodeUnit\Fixture\FixtureClass; use SebastianBergmann\CodeUnit\Fixture\FixtureInterface; use SebastianBergmann\CodeUnit\Fixture\FixtureTrait; /** * @covers \SebastianBergmann\CodeUnit\TraitMethodUnit * @covers \SebastianBergmann\CodeUnit\CodeUnit * * @uses \SebastianBergmann\CodeUnit\CodeUnitCollection * @uses \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator * @uses \SebastianBergmann\CodeUnit\Mapper * * @testdox TraitMethodUnit */ final class TraitMethodUnitTest extends TestCase { public function testCanBeCreatedFromNameOfUserDefinedTraitAndMethodName(): void { $unit = CodeUnit::forTraitMethod(FixtureTrait::class, 'method'); $this->assertFalse($unit->isClass()); $this->assertFalse($unit->isClassMethod()); $this->assertFalse($unit->isInterface()); $this->assertFalse($unit->isInterfaceMethod()); $this->assertFalse($unit->isTrait()); $this->assertTrue($unit->isTraitMethod()); $this->assertFalse($unit->isFunction()); $this->assertSame(FixtureTrait::class . '::method', $unit->name()); $this->assertSame(realpath(__DIR__ . '/../_fixture/FixtureTrait.php'), $unit->sourceFileName()); $this->assertSame(range(14, 17), $unit->sourceLines()); } public function testCannotBeCreatedForClassMethod(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forTraitMethod(FixtureClass::class, 'publicMethod'); } public function testCannotBeCreatedForInterfaceMethod(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forTraitMethod(FixtureInterface::class, 'method'); } } phpunit-code-unit-1.0.8/tests/unit/TraitUnitTest.php000066400000000000000000000037031376576263600225410ustar00rootroot00000000000000 * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace SebastianBergmann\CodeUnit; use function range; use function realpath; use PHPUnit\Framework\TestCase; use SebastianBergmann\CodeUnit\Fixture\FixtureClass; use SebastianBergmann\CodeUnit\Fixture\FixtureInterface; use SebastianBergmann\CodeUnit\Fixture\FixtureTrait; /** * @covers \SebastianBergmann\CodeUnit\TraitUnit * @covers \SebastianBergmann\CodeUnit\CodeUnit * * @uses \SebastianBergmann\CodeUnit\CodeUnitCollection * @uses \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator * @uses \SebastianBergmann\CodeUnit\Mapper * * @testdox TraitUnit */ final class TraitUnitTest extends TestCase { public function testCanBeCreatedFromNameOfUserDefinedTrait(): void { $unit = CodeUnit::forTrait(FixtureTrait::class); $this->assertFalse($unit->isClass()); $this->assertFalse($unit->isClassMethod()); $this->assertFalse($unit->isInterface()); $this->assertFalse($unit->isInterfaceMethod()); $this->assertTrue($unit->isTrait()); $this->assertFalse($unit->isTraitMethod()); $this->assertFalse($unit->isFunction()); $this->assertSame(FixtureTrait::class, $unit->name()); $this->assertSame(realpath(__DIR__ . '/../_fixture/FixtureTrait.php'), $unit->sourceFileName()); $this->assertSame(range(12, 18), $unit->sourceLines()); } public function testCannotBeCreatedForClass(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forTrait(FixtureClass::class); } public function testCannotBeCreatedForInterface(): void { $this->expectException(InvalidCodeUnitException::class); CodeUnit::forTrait(FixtureInterface::class); } }