Repository: doctrine/annotations Branch: 2.0.x Commit: 17815fb6b2f4 Files: 163 Total size: 392.5 KB Directory structure: gitextract_dxqf29h2/ ├── .doctrine-project.json ├── .gitattributes ├── .github/ │ └── workflows/ │ ├── coding-standards.yml │ ├── composer-lint.yml │ ├── continuous-integration.yml │ ├── documentation.yml │ ├── release-on-milestone-closed.yml │ └── static-analysis.yml ├── .gitignore ├── LICENSE ├── README.md ├── UPGRADE.md ├── composer.json ├── docs/ │ ├── composer.json │ └── en/ │ ├── annotations.rst │ ├── custom.rst │ ├── index.rst │ └── sidebar.rst ├── lib/ │ └── Doctrine/ │ └── Common/ │ └── Annotations/ │ ├── Annotation/ │ │ ├── Attribute.php │ │ ├── Attributes.php │ │ ├── Enum.php │ │ ├── IgnoreAnnotation.php │ │ ├── NamedArgumentConstructor.php │ │ ├── Required.php │ │ └── Target.php │ ├── Annotation.php │ ├── AnnotationException.php │ ├── AnnotationReader.php │ ├── AnnotationRegistry.php │ ├── DocLexer.php │ ├── DocParser.php │ ├── ImplicitlyIgnoredAnnotationNames.php │ ├── IndexedReader.php │ ├── PhpParser.php │ ├── PsrCachedReader.php │ ├── Reader.php │ └── TokenParser.php ├── phpbench.json.dist ├── phpcs.xml.dist ├── phpstan.neon ├── phpunit.xml.dist ├── psalm.xml └── tests/ └── Doctrine/ ├── Performance/ │ └── Common/ │ ├── Annotations/ │ │ ├── CachedReadPerformanceWithInMemoryBench.php │ │ ├── DocLexerPerformanceBench.php │ │ ├── DocParserPerformanceBench.php │ │ ├── MethodInitializer.php │ │ ├── PhpParserPerformanceWithShortCutBench.php │ │ ├── PhpParserPerformanceWithoutShortCutBench.php │ │ └── ReadPerformanceBench.php │ └── bootstrap.php └── Tests/ ├── Common/ │ └── Annotations/ │ ├── AbstractReaderTest.php │ ├── Annotation/ │ │ └── TargetTest.php │ ├── AnnotationReaderTest.php │ ├── AnnotationRegistryTest.php │ ├── AnnotationTest.php │ ├── DocLexerTest.php │ ├── DocParserTest.php │ ├── DummyClass.php │ ├── Fixtures/ │ │ ├── AbstractController.php │ │ ├── Annotation/ │ │ │ ├── AnnotWithDefaultValue.php │ │ │ ├── Autoload.php │ │ │ ├── CanBeAutoLoaded.php │ │ │ ├── LoadedUsingRegisterFile.php │ │ │ ├── Param.php │ │ │ ├── Route.php │ │ │ ├── Secure.php │ │ │ ├── ShouldNeverBeLoaded.php │ │ │ ├── SingleUseAnnotation.php │ │ │ ├── Template.php │ │ │ └── Version.php │ │ ├── AnnotationEnum.php │ │ ├── AnnotationEnumInvalid.php │ │ ├── AnnotationEnumLiteral.php │ │ ├── AnnotationEnumLiteralInvalid.php │ │ ├── AnnotationTargetAll.php │ │ ├── AnnotationTargetAnnotation.php │ │ ├── AnnotationTargetClass.php │ │ ├── AnnotationTargetMethod.php │ │ ├── AnnotationTargetPropertyMethod.php │ │ ├── AnnotationWithAttributes.php │ │ ├── AnnotationWithConstants.php │ │ ├── AnnotationWithEnumProperty.php │ │ ├── AnnotationWithRequiredAttributes.php │ │ ├── AnnotationWithRequiredAttributesWithoutConstructor.php │ │ ├── AnnotationWithTargetSyntaxError.php │ │ ├── AnnotationWithVarType.php │ │ ├── Api.php │ │ ├── ClassDDC1660.php │ │ ├── ClassNoNamespaceNoComment.php │ │ ├── ClassOverwritesTrait.php │ │ ├── ClassThatUsesTraitThatUsesAnotherTrait.php │ │ ├── ClassThatUsesTraitThatUsesAnotherTraitWithMethods.php │ │ ├── ClassUsesTrait.php │ │ ├── ClassWithAnnotationConstantReferenceWithDashes.php │ │ ├── ClassWithAnnotationEnum.php │ │ ├── ClassWithAnnotationWithSelfConstantReference.php │ │ ├── ClassWithAnnotationWithTargetSyntaxError.php │ │ ├── ClassWithAnnotationWithVarType.php │ │ ├── ClassWithAtInDescriptionAndAnnotation.php │ │ ├── ClassWithClassAnnotationOnly.php │ │ ├── ClassWithClosure.php │ │ ├── ClassWithConstants.php │ │ ├── ClassWithEnumAnnotations.php │ │ ├── ClassWithFullPathUseStatement.php │ │ ├── ClassWithFullyQualifiedUseStatements.php │ │ ├── ClassWithIgnoreAnnotation.php │ │ ├── ClassWithImportedIgnoredAnnotation.php │ │ ├── ClassWithInvalidAnnotationContainingDashes.php │ │ ├── ClassWithInvalidAnnotationTargetAtClass.php │ │ ├── ClassWithInvalidAnnotationTargetAtMethod.php │ │ ├── ClassWithInvalidAnnotationTargetAtProperty.php │ │ ├── ClassWithNotRegisteredAnnotationUsed.php │ │ ├── ClassWithPHPCodeSnifferAnnotation.php │ │ ├── ClassWithPHPStanGenericsAnnotations.php │ │ ├── ClassWithPhpCsSuppressAnnotation.php │ │ ├── ClassWithRequire.php │ │ ├── ClassWithValidAnnotationTarget.php │ │ ├── Controller.php │ │ ├── ControllerWithParentClass.php │ │ ├── ControllerWithTrait.php │ │ ├── DifferentNamespacesPerFileWithClassAsFirst.php │ │ ├── DifferentNamespacesPerFileWithClassAsLast.php │ │ ├── EmptyInterface.php │ │ ├── EqualNamespacesPerFileWithClassAsFirst.php │ │ ├── EqualNamespacesPerFileWithClassAsLast.php │ │ ├── GlobalNamespacesPerFileWithClassAsFirst.php │ │ ├── GlobalNamespacesPerFileWithClassAsLast.php │ │ ├── GroupUseStatement.php │ │ ├── HereForTesting.php │ │ ├── IgnoreAnnotationClass.php │ │ ├── IgnoredNamespaces/ │ │ │ ├── AnnotatedAtClassLevel.php │ │ │ ├── AnnotatedAtMethodLevel.php │ │ │ ├── AnnotatedAtPropertyLevel.php │ │ │ └── AnnotatedWithAlias.php │ │ ├── InterfaceThatExtendsAnInterface.php │ │ ├── InterfaceWithConstants.php │ │ ├── InvalidAnnotationUsageButIgnoredClass.php │ │ ├── InvalidAnnotationUsageClass.php │ │ ├── MultipleClassesInFile.php │ │ ├── MultipleImportsInUseStatement.php │ │ ├── NamespaceAndClassCommentedOut.php │ │ ├── NamespaceWithClosureDeclaration.php │ │ ├── NamespacedSingleClassLOC1000.php │ │ ├── NoAnnotation.php │ │ ├── NonNamespacedClass.php │ │ ├── SingleClassLOC1000.php │ │ ├── Suit.php │ │ ├── TraitWithAnnotatedMethod.php │ │ ├── Traits/ │ │ │ ├── EmptyTrait.php │ │ │ ├── SecretRouteTrait.php │ │ │ ├── TraitThatUsesAnotherTrait.php │ │ │ └── TraitWithSelfConstantReferenceTrait.php │ │ └── functions.php │ ├── PhpParserTest.php │ ├── PsrCachedReaderTest.php │ ├── ReservedKeywordsClasses.php │ ├── Ticket/ │ │ ├── DCOM141Test.php │ │ ├── DCOM55Test.php │ │ ├── DCOM58Entity.php │ │ └── DCOM58Test.php │ └── TopLevelAnnotation.php ├── DoctrineTestCase.php └── TestInit.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .doctrine-project.json ================================================ { "active": false, "name": "Annotations", "slug": "annotations", "docsSlug": "doctrine-annotations", "versions": [ { "name": "2.0", "branchName": "2.0.x", "aliases": [ "current", "stable" ], "current": true, "maintained": true }, { "name": "1.14", "branchName": "1.14.x", "maintained": false }, { "name": "1.13", "branchName": "1.13.x", "slug": "1.13", "maintained": false }, { "name": "1.12", "branchName": "1.12.x", "slug": "1.12", "maintained": false }, { "name": "1.11", "branchName": "1.11.x", "slug": "1.11", "maintained": false }, { "name": "1.10", "branchName": "1.10.x", "slug": "1.10", "maintained": false }, { "name": "1.9", "branchName": "1.9.x", "slug": "1.9", "maintained": false }, { "name": "1.8", "branchName": "1.8", "slug": "1.8", "maintained": false }, { "name": "1.7", "branchName": "1.7", "slug": "1.7", "maintained": false }, { "name": "1.6", "branchName": "1.6", "slug": "1.6", "maintained": false } ] } ================================================ FILE: .gitattributes ================================================ /.* export-ignore /tests export-ignore /phpbench.json.dist export-ignore /phpcs.xml.dist export-ignore /phpstan.neon export-ignore /phpunit.xml.dist export-ignore /psalm.xml export-ignore ================================================ FILE: .github/workflows/coding-standards.yml ================================================ name: "Coding Standards" on: pull_request: branches: - "*.x" push: branches: - "*.x" jobs: coding-standards: name: "Coding Standards" uses: "doctrine/.github/.github/workflows/coding-standards.yml@13.1.0" ================================================ FILE: .github/workflows/composer-lint.yml ================================================ name: "Composer Lint" on: pull_request: branches: - "*.x" paths: - "composer.json" push: branches: - "*.x" paths: - "composer.json" jobs: composer-lint: name: "Composer Lint" uses: "doctrine/.github/.github/workflows/composer-lint.yml@13.1.0" ================================================ FILE: .github/workflows/continuous-integration.yml ================================================ name: "Continuous Integration" on: pull_request: branches: - "*.x" push: branches: - "*.x" env: fail-fast: true jobs: phpunit: name: "PHPUnit" uses: "doctrine/.github/.github/workflows/continuous-integration.yml@13.1.0" with: php-versions: '["7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.4", "8.5"]' secrets: CODECOV_TOKEN: "${{ secrets.CODECOV_TOKEN }}" ================================================ FILE: .github/workflows/documentation.yml ================================================ name: "Documentation" on: pull_request: branches: - "*.x" paths: - ".github/workflows/documentation.yml" - "docs/**" push: branches: - "*.x" paths: - ".github/workflows/documentation.yml" - "docs/**" jobs: documentation: name: "Documentation" uses: "doctrine/.github/.github/workflows/documentation.yml@13.1.0" ================================================ FILE: .github/workflows/release-on-milestone-closed.yml ================================================ name: "Automatic Releases" on: milestone: types: - "closed" jobs: release: name: "Git tag, release & create merge-up PR" uses: "doctrine/.github/.github/workflows/release-on-milestone-closed.yml@13.1.0" secrets: GIT_AUTHOR_EMAIL: ${{ secrets.GIT_AUTHOR_EMAIL }} GIT_AUTHOR_NAME: ${{ secrets.GIT_AUTHOR_NAME }} ORGANIZATION_ADMIN_TOKEN: ${{ secrets.ORGANIZATION_ADMIN_TOKEN }} SIGNING_SECRET_KEY: ${{ secrets.SIGNING_SECRET_KEY }} ================================================ FILE: .github/workflows/static-analysis.yml ================================================ name: "Static Analysis" on: pull_request: branches: - "*.x" push: branches: - "*.x" jobs: phpstan: name: "Static Analysis" uses: "doctrine/.github/.github/workflows/phpstan.yml@13.1.0" psalm: name: "Psalm (PHP: 8.2)" runs-on: "ubuntu-22.04" steps: - name: "Checkout code" uses: "actions/checkout@v6" - name: "Install PHP" uses: "shivammathur/setup-php@v2" with: coverage: "none" php-version: "8.2" - name: "Install dependencies with Composer" uses: "ramsey/composer-install@v3" with: dependency-versions: "highest" - name: "Run a static analysis with vimeo/psalm" run: "vendor/bin/psalm --show-info=false --stats --output-format=github --threads=$(nproc)" ================================================ FILE: .gitignore ================================================ /vendor/ /composer.lock /composer.phar /phpbench.json /phpunit.xml /.phpcs-cache /.phpunit.result.cache ================================================ FILE: LICENSE ================================================ Copyright (c) 2006-2013 Doctrine Project Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ ⚠️ PHP 8 introduced [attributes](https://www.php.net/manual/en/language.attributes.overview.php), which are a native replacement for annotations. As such, this library is considered feature complete, and should receive exclusively bugfixes and security fixes. We do not recommend using this library in new projects and encourage authors of downstream libraries to offer support for attributes as an alternative to Doctrine Annotations. Have a look at [our blog](https://www.doctrine-project.org/2022/11/04/annotations-to-attributes.html) to learn more. # Doctrine Annotations [![Build Status](https://github.com/doctrine/annotations/workflows/Continuous%20Integration/badge.svg?label=build)](https://github.com/doctrine/persistence/actions) [![Dependency Status](https://www.versioneye.com/package/php--doctrine--annotations/badge.png)](https://www.versioneye.com/package/php--doctrine--annotations) [![Reference Status](https://www.versioneye.com/php/doctrine:annotations/reference_badge.svg)](https://www.versioneye.com/php/doctrine:annotations/references) [![Total Downloads](https://poser.pugx.org/doctrine/annotations/downloads.png)](https://packagist.org/packages/doctrine/annotations) [![Latest Stable Version](https://img.shields.io/packagist/v/doctrine/annotations.svg?label=stable)](https://packagist.org/packages/doctrine/annotations) Docblock Annotations Parser library (extracted from [Doctrine Common](https://github.com/doctrine/common)). ## Documentation See the [doctrine-project website](https://www.doctrine-project.org/projects/doctrine-annotations/en/stable/index.html). ## Contributing When making a pull request, make sure your changes follow the [Coding Standard Guidelines](https://www.doctrine-project.org/projects/doctrine-coding-standard/en/current/reference/index.html#introduction). ================================================ FILE: UPGRADE.md ================================================ # Upgrade from 1.0.x to 2.0.x - The `NamedArgumentConstructorAnnotation` has been removed. Use the `@NamedArgumentConstructor` annotation instead. - `SimpleAnnotationReader` has been removed. - `DocLexer::peek()` and `DocLexer::glimpse` now return `Doctrine\Common\Lexer\Token` objects. When using `doctrine/lexer` 2, these implement `ArrayAccess` as a way for you to still be able to treat them as arrays in some ways. - `CachedReader` and `FileCacheReader` have been removed use `PsrCachedReader` instead. - `AnnotationRegistry` methods related to registering annotations instead of using autoloading have been removed. - Parameter type declarations have been added to all methods of all classes. If you have classes inheriting from classes inside this package, you should add parameter and return type declarations. - Support for PHP < 7.2 has been removed - `PhpParser::parseClass()` has been removed. Use `PhpParser::parseUseStatements()` instead. ================================================ FILE: composer.json ================================================ { "name": "doctrine/annotations", "description": "Docblock Annotations Parser", "license": "MIT", "type": "library", "abandoned": true, "keywords": [ "annotations", "docblock", "parser" ], "authors": [ { "name": "Guilherme Blanco", "email": "guilhermeblanco@gmail.com" }, { "name": "Roman Borschel", "email": "roman@code-factory.org" }, { "name": "Benjamin Eberlei", "email": "kontakt@beberlei.de" }, { "name": "Jonathan Wage", "email": "jonwage@gmail.com" }, { "name": "Johannes Schmitt", "email": "schmittjoh@gmail.com" } ], "homepage": "https://www.doctrine-project.org/projects/annotations.html", "require": { "php": "^7.2 || ^8.0", "ext-tokenizer": "*", "doctrine/lexer": "^2 || ^3", "psr/cache": "^1 || ^2 || ^3" }, "require-dev": { "doctrine/coding-standard": "^11.1 || ^14", "phpstan/phpstan": "^1.11 || ^2.1", "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", "symfony/cache": "^5.4 || ^6.4 || ^7.4 || ^8", "vimeo/psalm": "^4.30 || ^5.14" }, "suggest": { "php": "PHP 8.0 or higher comes with attributes, a native replacement for annotations" }, "autoload": { "psr-4": { "Doctrine\\Common\\Annotations\\": "lib/Doctrine/Common/Annotations" } }, "autoload-dev": { "psr-4": { "Doctrine\\Performance\\Common\\Annotations\\": "tests/Doctrine/Performance/Common/Annotations", "Doctrine\\Tests\\Common\\Annotations\\": "tests/Doctrine/Tests/Common/Annotations" }, "files": [ "tests/Doctrine/Tests/Common/Annotations/Fixtures/functions.php", "tests/Doctrine/Tests/Common/Annotations/Fixtures/SingleClassLOC1000.php" ] }, "config": { "allow-plugins": { "dealerdirect/phpcodesniffer-composer-installer": true }, "sort-packages": true } } ================================================ FILE: docs/composer.json ================================================ { "require": { "doctrine/docs-builder": "^1.0" } } ================================================ FILE: docs/en/annotations.rst ================================================ Handling Annotations ==================== There are several different approaches to handling annotations in PHP. Doctrine Annotations maps docblock annotations to PHP classes. Because not all docblock annotations are used for metadata purposes a filter is applied to ignore or skip classes that are not Doctrine annotations. Take a look at the following code snippet: .. code-block:: php namespace MyProject\Entities; use Doctrine\ORM\Mapping AS ORM; use Symfony\Component\Validator\Constraints AS Assert; /** * @author Benjamin Eberlei * @ORM\Entity * @MyProject\Annotations\Foobarable */ class User { /** * @ORM\Id @ORM\Column @ORM\GeneratedValue * @dummy * @var int */ private $id; /** * @ORM\Column(type="string") * @Assert\NotEmpty * @Assert\Email * @var string */ private $email; } In this snippet you can see a variety of different docblock annotations: - Documentation annotations such as ``@var`` and ``@author``. These annotations are ignored and never considered for throwing an exception due to wrongly used annotations. - Annotations imported through use statements. The statement ``use Doctrine\ORM\Mapping AS ORM`` makes all classes under that namespace available as ``@ORM\ClassName``. Same goes for the import of ``@Assert``. - The ``@dummy`` annotation. It is not a documentation annotation and not ignored. For Doctrine Annotations it is not entirely clear how to handle this annotation. Depending on the configuration an exception (unknown annotation) will be thrown when parsing this annotation. - The fully qualified annotation ``@MyProject\Annotations\Foobarable``. This is transformed directly into the given class name. How are these annotations loaded? From looking at the code you could guess that the ORM Mapping, Assert Validation and the fully qualified annotation can just be loaded using the defined PHP autoloaders. This is not the case however: For error handling reasons every check for class existence inside the ``AnnotationReader`` sets the second parameter $autoload of ``class_exists($name, $autoload)`` to false. To work flawlessly the ``AnnotationReader`` requires silent autoloaders which many autoloaders are not. Silent autoloading is NOT part of the `PSR-0 specification `_ for autoloading. This is why Doctrine Annotations uses its own autoloading mechanism through a global registry. If you are wondering about the annotation registry being global, there is no other way to solve the architectural problems of autoloading annotation classes in a straightforward fashion. Additionally if you think about PHP autoloading then you recognize it is a global as well. To anticipate the configuration section, making the above PHP class work with Doctrine Annotations requires this setup: .. code-block:: php use Doctrine\Common\Annotations\AnnotationReader; $reader = new AnnotationReader(); AnnotationReader::addGlobalIgnoredName('dummy'); We create the actual ``AnnotationReader`` instance. Note that we also add ``dummy`` to the global list of ignored annotations for which we do not throw exceptions. Setting this is necessary in our example case, otherwise ``@dummy`` would trigger an exception to be thrown during the parsing of the docblock of ``MyProject\Entities\User#id``. Setup and Configuration ----------------------- To use the annotations library is simple, you just need to create a new ``AnnotationReader`` instance: .. code-block:: php $reader = new \Doctrine\Common\Annotations\AnnotationReader(); This creates a simple annotation reader with no caching other than in memory (in php arrays). Since parsing docblocks can be expensive you should cache this process by using a caching reader. To cache annotations, you can create a ``Doctrine\Common\Annotations\PsrCachedReader``. This reader decorates the original reader and stores all annotations in a PSR-6 cache: .. code-block:: php use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\PsrCachedReader; $cache = ... // instantiate a PSR-6 Cache pool $reader = new PsrCachedReader( new AnnotationReader(), $cache, $debug = true ); The ``debug`` flag is used here as well to invalidate the cache files when the PHP class with annotations changed and should be used during development. .. warning :: The ``AnnotationReader`` works and caches under the assumption that all annotations of a doc-block are processed at once. That means that annotation classes that do not exist and aren't loaded and cannot be autoloaded (using the AnnotationRegistry) would never be visible and not accessible if a cache is used unless the cache is cleared and the annotations requested again, this time with all annotations defined. By default the annotation reader returns a list of annotations with numeric indexes. If you want your annotations to be indexed by their class name you can wrap the reader in an ``IndexedReader``: .. code-block:: php use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\IndexedReader; $reader = new IndexedReader(new AnnotationReader()); .. warning:: You should never wrap the indexed reader inside a cached reader, only the other way around. This way you can re-use the cache with indexed or numeric keys, otherwise your code may experience failures due to caching in a numerical or indexed format. Ignoring missing exceptions ~~~~~~~~~~~~~~~~~~~~~~~~~~~ By default an exception is thrown from the ``AnnotationReader`` if an annotation was found that: - is not part of the list of ignored "documentation annotations"; - was not imported through a use statement; - is not a fully qualified class that exists. You can disable this behavior for specific names if your docblocks do not follow strict requirements: .. code-block:: php $reader = new \Doctrine\Common\Annotations\AnnotationReader(); AnnotationReader::addGlobalIgnoredName('foo'); PHP Imports ~~~~~~~~~~~ By default the annotation reader parses the use-statement of a php file to gain access to the import rules and register them for the annotation processing. Only if you are using PHP Imports can you validate the correct usage of annotations and throw exceptions if you misspelled an annotation. This mechanism is enabled by default. To ease the upgrade path, we still allow you to disable this mechanism. Note however that we will remove this in future versions: .. code-block:: php $reader = new \Doctrine\Common\Annotations\AnnotationReader(); $reader->setEnabledPhpImports(false); ================================================ FILE: docs/en/custom.rst ================================================ Custom Annotation Classes ========================= If you want to define your own annotations, you just have to group them in a namespace. Annotation classes have to contain a class-level docblock with the text ``@Annotation``: .. code-block:: php namespace MyCompany\Annotations; /** @Annotation */ class Bar { // some code } Inject annotation values ------------------------ The annotation parser checks if the annotation constructor has arguments, if so then it will pass the value array, otherwise it will try to inject values into public properties directly: .. code-block:: php namespace MyCompany\Annotations; /** * @Annotation * * Some Annotation using a constructor */ class Bar { private $foo; public function __construct(array $values) { $this->foo = $values['foo']; } } /** * @Annotation * * Some Annotation without a constructor */ class Foo { public $bar; } Optional: Constructors with Named Parameters -------------------------------------------- Starting with Annotations v1.11 a new annotation instantiation strategy is available that aims at compatibility of Annotation classes with the PHP 8 attribute feature. You need to declare a constructor with regular parameter names that match the named arguments in the annotation syntax. To enable this feature, you can tag your annotation class with ``@NamedArgumentConstructor`` (available from v1.12) or implement the ``Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation`` interface (available from v1.11 and deprecated as of v1.12). When using the ``@NamedArgumentConstructor`` tag, the first argument of the constructor is considered as the default one. Usage with the ``@NamedArgumentConstructor`` tag .. code-block:: php namespace MyCompany\Annotations; /** * @Annotation * @NamedArgumentConstructor */ class Bar implements NamedArgumentConstructorAnnotation { private $foo; public function __construct(string $foo) { $this->foo = $foo; } } /** Usable with @Bar(foo="baz") */ /** Usable with @Bar("baz") */ In combination with PHP 8's constructor property promotion feature you can simplify this to: .. code-block:: php namespace MyCompany\Annotations; /** * @Annotation * @NamedArgumentConstructor */ class Bar implements NamedArgumentConstructorAnnotation { public function __construct(private string $foo) {} } Usage with the ``Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation`` interface (v1.11, deprecated as of v1.12): .. code-block:: php namespace MyCompany\Annotations; use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation; /** @Annotation */ class Bar implements NamedArgumentConstructorAnnotation { private $foo; public function __construct(private string $foo) {} } /** Usable with @Bar(foo="baz") */ Annotation Target ----------------- ``@Target`` indicates the kinds of class elements to which an annotation type is applicable. Then you could define one or more targets: - ``CLASS`` Allowed in class docblocks - ``PROPERTY`` Allowed in property docblocks - ``METHOD`` Allowed in the method docblocks - ``FUNCTION`` Allowed in function docblocks - ``ALL`` Allowed in class, property, method and function docblocks - ``ANNOTATION`` Allowed inside other annotations If the annotations is not allowed in the current context, an ``AnnotationException`` is thrown. .. code-block:: php namespace MyCompany\Annotations; /** * @Annotation * @Target({"METHOD","PROPERTY"}) */ class Bar { // some code } /** * @Annotation * @Target("CLASS") */ class Foo { // some code } Attribute types --------------- The annotation parser checks the given parameters using the phpdoc annotation ``@var``, The data type could be validated using the ``@var`` annotation on the annotation properties or using the ``@Attributes`` and ``@Attribute`` annotations. If the data type does not match you get an ``AnnotationException`` .. code-block:: php namespace MyCompany\Annotations; /** * @Annotation * @Target({"METHOD","PROPERTY"}) */ class Bar { /** @var mixed */ public $mixed; /** @var boolean */ public $boolean; /** @var bool */ public $bool; /** @var float */ public $float; /** @var string */ public $string; /** @var integer */ public $integer; /** @var array */ public $array; /** @var SomeAnnotationClass */ public $annotation; /** @var array */ public $arrayOfIntegers; /** @var array */ public $arrayOfAnnotations; } /** * @Annotation * @Target({"METHOD","PROPERTY"}) * @Attributes({ * @Attribute("stringProperty", type = "string"), * @Attribute("annotProperty", type = "SomeAnnotationClass"), * }) */ class Foo { public function __construct(array $values) { $this->stringProperty = $values['stringProperty']; $this->annotProperty = $values['annotProperty']; } // some code } Annotation Required ------------------- ``@Required`` indicates that the field must be specified when the annotation is used. If it is not used you get an ``AnnotationException`` stating that this value can not be null. Declaring a required field: .. code-block:: php /** * @Annotation * @Target("ALL") */ class Foo { /** @Required */ public $requiredField; } Usage: .. code-block:: php /** @Foo(requiredField="value") */ public $direction; // Valid /** @Foo */ public $direction; // Required field missing, throws an AnnotationException Enumerated values ----------------- - An annotation property marked with ``@Enum`` is a field that accepts a fixed set of scalar values. - You should use ``@Enum`` fields any time you need to represent fixed values. - The annotation parser checks the given value and throws an ``AnnotationException`` if the value does not match. Declaring an enumerated property: .. code-block:: php /** * @Annotation * @Target("ALL") */ class Direction { /** * @Enum({"NORTH", "SOUTH", "EAST", "WEST"}) */ public $value; } Annotation usage: .. code-block:: php /** @Direction("NORTH") */ public $direction; // Valid value /** @Direction("NORTHEAST") */ public $direction; // Invalid value, throws an AnnotationException Constants --------- The use of constants and class constants is available on the annotations parser. The following usages are allowed: .. code-block:: php namespace MyCompany\Entity; use MyCompany\Annotations\Foo; use MyCompany\Annotations\Bar; use MyCompany\Entity\SomeClass; /** * @Foo(PHP_EOL) * @Bar(Bar::FOO) * @Foo({SomeClass::FOO, SomeClass::BAR}) * @Bar({SomeClass::FOO_KEY = SomeClass::BAR_VALUE}) */ class User { } Be careful with constants and the cache ! .. note:: The cached reader will not re-evaluate each time an annotation is loaded from cache. When a constant is changed the cache must be cleaned. Usage ----- Using the library API is simple. Using the annotations described in the previous section, you can now annotate other classes with your annotations: .. code-block:: php namespace MyCompany\Entity; use MyCompany\Annotations\Foo; use MyCompany\Annotations\Bar; /** * @Foo(bar="foo") * @Bar(foo="bar") */ class User { } Now we can write a script to get the annotations above: .. code-block:: php $reflClass = new ReflectionClass('MyCompany\Entity\User'); $classAnnotations = $reader->getClassAnnotations($reflClass); foreach ($classAnnotations AS $annot) { if ($annot instanceof \MyCompany\Annotations\Foo) { echo $annot->bar; // prints "foo"; } else if ($annot instanceof \MyCompany\Annotations\Bar) { echo $annot->foo; // prints "bar"; } } You have a complete API for retrieving annotation class instances from a class, property or method docblock: Reader API ~~~~~~~~~~ Access all annotations of a class ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: php public function getClassAnnotations(\ReflectionClass $class); Access one annotation of a class ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: php public function getClassAnnotation(\ReflectionClass $class, $annotationName); Access all annotations of a method ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: php public function getMethodAnnotations(\ReflectionMethod $method); Access one annotation of a method ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: php public function getMethodAnnotation(\ReflectionMethod $method, $annotationName); Access all annotations of a property ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: php public function getPropertyAnnotations(\ReflectionProperty $property); Access one annotation of a property ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: php public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName); Access all annotations of a function ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: php public function getFunctionAnnotations(\ReflectionFunction $property); Access one annotation of a function ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ .. code-block:: php public function getFunctionAnnotation(\ReflectionFunction $property, $annotationName); ================================================ FILE: docs/en/index.rst ================================================ Getting Started =============== Deprecation notice ------------------ PHP 8 introduced `attributes `_, which are a native replacement for annotations. As such, this library is considered feature complete, and should receive exclusively bugfixes and security fixes. We do not recommend using this library in new projects and encourage authors of downstream libraries to offer support for attributes as an alternative to Doctrine Annotations. Have a look at `our blog `_ to learn more. Introduction ------------ Doctrine Annotations allows to implement custom annotation functionality for PHP classes and functions. .. code-block:: php class Foo { /** * @MyAnnotation(myProperty="value") */ private $bar; } Annotations aren't implemented in PHP itself which is why this component offers a way to use the PHP doc-blocks as a place for the well known annotation syntax using the ``@`` char. Annotations in Doctrine are used for the ORM configuration to build the class mapping, but it can be used in other projects for other purposes too. Installation ------------ You can install the Annotation component with composer: .. code-block:: $ composer require doctrine/annotations Create an annotation class -------------------------- An annotation class is a representation of the later used annotation configuration in classes. The annotation class of the previous example looks like this: .. code-block:: php /** * @Annotation */ final class MyAnnotation { public $myProperty; } The annotation class is declared as an annotation by ``@Annotation``. :doc:`Read more about custom annotations. ` Reading annotations ------------------- The access to the annotations happens by reflection of the class or function containing them. There are multiple reader-classes implementing the ``Doctrine\Common\Annotations\Reader`` interface, that can access the annotations of a class. A common one is ``Doctrine\Common\Annotations\AnnotationReader``: .. code-block:: php use Doctrine\Common\Annotations\AnnotationReader; $reflectionClass = new ReflectionClass(Foo::class); $property = $reflectionClass->getProperty('bar'); $reader = new AnnotationReader(); $myAnnotation = $reader->getPropertyAnnotation( $property, MyAnnotation::class ); echo $myAnnotation->myProperty; // result: "value" A reader has multiple methods to access the annotations of a class or function. :doc:`Read more about handling annotations. ` IDE Support ^^^^^^^^^^^ Some IDEs already provide support for annotations: - Eclipse via the `Symfony2 Plugin `_ - PhpStorm via the `PHP Annotations Plugin `_ or the `Symfony Plugin `_ ================================================ FILE: docs/en/sidebar.rst ================================================ :orphan: .. toctree:: :depth: 3 index annotations custom ================================================ FILE: lib/Doctrine/Common/Annotations/Annotation/Attribute.php ================================================ */ public $value; } ================================================ FILE: lib/Doctrine/Common/Annotations/Annotation/Enum.php ================================================ */ public $value; /** * Literal target declaration. * * @var mixed[] */ public $literal; /** * @phpstan-param array{literal?: mixed[], value: list} $values * * @throws InvalidArgumentException */ public function __construct(array $values) { if (! isset($values['literal'])) { $values['literal'] = []; } foreach ($values['value'] as $var) { if (! is_scalar($var)) { throw new InvalidArgumentException(sprintf( '@Enum supports only scalar values "%s" given.', is_object($var) ? get_class($var) : gettype($var) )); } } foreach ($values['literal'] as $key => $var) { if (! in_array($key, $values['value'])) { throw new InvalidArgumentException(sprintf( 'Undefined enumerator value "%s" for literal "%s".', $key, $var )); } } $this->value = $values['value']; $this->literal = $values['literal']; } } ================================================ FILE: lib/Doctrine/Common/Annotations/Annotation/IgnoreAnnotation.php ================================================ */ public $names; /** * @phpstan-param array{value: string|list} $values * * @throws RuntimeException */ public function __construct(array $values) { if (is_string($values['value'])) { $values['value'] = [$values['value']]; } if (! is_array($values['value'])) { throw new RuntimeException(sprintf( '@IgnoreAnnotation expects either a string name, or an array of strings, but got %s.', json_encode($values['value']) )); } $this->names = $values['value']; } } ================================================ FILE: lib/Doctrine/Common/Annotations/Annotation/NamedArgumentConstructor.php ================================================ */ private static $map = [ 'ALL' => self::TARGET_ALL, 'CLASS' => self::TARGET_CLASS, 'METHOD' => self::TARGET_METHOD, 'PROPERTY' => self::TARGET_PROPERTY, 'FUNCTION' => self::TARGET_FUNCTION, 'ANNOTATION' => self::TARGET_ANNOTATION, ]; /** @phpstan-var list */ public $value; /** * Targets as bitmask. * * @var int */ public $targets; /** * Literal target declaration. * * @var string */ public $literal; /** * @phpstan-param array{value?: string|list} $values * * @throws InvalidArgumentException */ public function __construct(array $values) { if (! isset($values['value'])) { $values['value'] = null; } if (is_string($values['value'])) { $values['value'] = [$values['value']]; } if (! is_array($values['value'])) { throw new InvalidArgumentException( sprintf( '@Target expects either a string value, or an array of strings, "%s" given.', is_object($values['value']) ? get_class($values['value']) : gettype($values['value']) ) ); } $bitmask = 0; foreach ($values['value'] as $literal) { if (! isset(self::$map[$literal])) { throw new InvalidArgumentException( sprintf( 'Invalid Target "%s". Available targets: [%s]', $literal, implode(', ', array_keys(self::$map)) ) ); } $bitmask |= self::$map[$literal]; } $this->targets = $bitmask; $this->value = $values['value']; $this->literal = implode(', ', $this->value); } } ================================================ FILE: lib/Doctrine/Common/Annotations/Annotation.php ================================================ $data Key-value for properties to be defined in this class. */ final public function __construct(array $data) { foreach ($data as $key => $value) { $this->$key = $value; } } /** * Error handler for unknown property accessor in Annotation class. * * @throws BadMethodCallException */ public function __get(string $name) { throw new BadMethodCallException( sprintf("Unknown property '%s' on annotation '%s'.", $name, static::class) ); } /** * Error handler for unknown property mutator in Annotation class. * * @param mixed $value Property value. * * @throws BadMethodCallException */ public function __set(string $name, $value) { throw new BadMethodCallException( sprintf("Unknown property '%s' on annotation '%s'.", $name, static::class) ); } } ================================================ FILE: lib/Doctrine/Common/Annotations/AnnotationException.php ================================================ $available * * @return AnnotationException */ public static function enumeratorError( string $attributeName, string $annotationName, string $context, array $available, $given ) { return new self(sprintf( '[Enum Error] Attribute "%s" of @%s declared on %s accepts only [%s], but got %s.', $attributeName, $annotationName, $context, implode(', ', $available), is_object($given) ? get_class($given) : $given )); } /** @return AnnotationException */ public static function optimizerPlusSaveComments() { return new self( 'You have to enable opcache.save_comments=1 or zend_optimizerplus.save_comments=1.' ); } /** @return AnnotationException */ public static function optimizerPlusLoadComments() { return new self( 'You have to enable opcache.load_comments=1 or zend_optimizerplus.load_comments=1.' ); } } ================================================ FILE: lib/Doctrine/Common/Annotations/AnnotationReader.php ================================================ */ private static $globalImports = [ 'ignoreannotation' => Annotation\IgnoreAnnotation::class, ]; /** * A list with annotations that are not causing exceptions when not resolved to an annotation class. * * The names are case sensitive. * * @var array */ private static $globalIgnoredNames = ImplicitlyIgnoredAnnotationNames::LIST; /** * A list with annotations that are not causing exceptions when not resolved to an annotation class. * * The names are case sensitive. * * @var array */ private static $globalIgnoredNamespaces = []; /** * Add a new annotation to the globally ignored annotation names with regard to exception handling. */ public static function addGlobalIgnoredName(string $name) { self::$globalIgnoredNames[$name] = true; } /** * Add a new annotation to the globally ignored annotation namespaces with regard to exception handling. */ public static function addGlobalIgnoredNamespace(string $namespace) { self::$globalIgnoredNamespaces[$namespace] = true; } /** * Annotations parser. * * @var DocParser */ private $parser; /** * Annotations parser used to collect parsing metadata. * * @var DocParser */ private $preParser; /** * PHP parser used to collect imports. * * @var PhpParser */ private $phpParser; /** * In-memory cache mechanism to store imported annotations per class. * * @psalm-var array<'class'|'function', array>> */ private $imports = []; /** * In-memory cache mechanism to store ignored annotations per class. * * @psalm-var array<'class'|'function', array>> */ private $ignoredAnnotationNames = []; /** * Initializes a new AnnotationReader. * * @throws AnnotationException */ public function __construct(?DocParser $parser = null) { if ( extension_loaded('Zend Optimizer+') && (filter_var(ini_get('zend_optimizerplus.save_comments'), FILTER_VALIDATE_BOOLEAN) === false || filter_var(ini_get('opcache.save_comments'), FILTER_VALIDATE_BOOLEAN) === false) ) { throw AnnotationException::optimizerPlusSaveComments(); } if ( extension_loaded('Zend OPcache') && filter_var(ini_get('opcache.save_comments'), FILTER_VALIDATE_BOOLEAN) === false ) { throw AnnotationException::optimizerPlusSaveComments(); } // Make sure that the IgnoreAnnotation annotation is loaded class_exists(IgnoreAnnotation::class); $this->parser = $parser ?: new DocParser(); $this->preParser = new DocParser(); $this->preParser->setImports(self::$globalImports); $this->preParser->setIgnoreNotImportedAnnotations(true); $this->preParser->setIgnoredAnnotationNames(self::$globalIgnoredNames); $this->phpParser = new PhpParser(); } /** * {@inheritDoc} */ public function getClassAnnotations(ReflectionClass $class) { $this->parser->setTarget(Target::TARGET_CLASS); $this->parser->setImports($this->getImports($class)); $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces); return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName()); } /** * {@inheritDoc} */ public function getClassAnnotation(ReflectionClass $class, $annotationName) { $annotations = $this->getClassAnnotations($class); foreach ($annotations as $annotation) { if ($annotation instanceof $annotationName) { return $annotation; } } return null; } /** * {@inheritDoc} */ public function getPropertyAnnotations(ReflectionProperty $property) { $class = $property->getDeclaringClass(); $context = 'property ' . $class->getName() . '::$' . $property->getName(); $this->parser->setTarget(Target::TARGET_PROPERTY); $this->parser->setImports($this->getPropertyImports($property)); $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces); return $this->parser->parse($property->getDocComment(), $context); } /** * {@inheritDoc} */ public function getPropertyAnnotation(ReflectionProperty $property, $annotationName) { $annotations = $this->getPropertyAnnotations($property); foreach ($annotations as $annotation) { if ($annotation instanceof $annotationName) { return $annotation; } } return null; } /** * {@inheritDoc} */ public function getMethodAnnotations(ReflectionMethod $method) { $class = $method->getDeclaringClass(); $context = 'method ' . $class->getName() . '::' . $method->getName() . '()'; $this->parser->setTarget(Target::TARGET_METHOD); $this->parser->setImports($this->getMethodImports($method)); $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class)); $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces); return $this->parser->parse($method->getDocComment(), $context); } /** * {@inheritDoc} */ public function getMethodAnnotation(ReflectionMethod $method, $annotationName) { $annotations = $this->getMethodAnnotations($method); foreach ($annotations as $annotation) { if ($annotation instanceof $annotationName) { return $annotation; } } return null; } /** * Gets the annotations applied to a function. * * @phpstan-return list An array of Annotations. */ public function getFunctionAnnotations(ReflectionFunction $function): array { $context = 'function ' . $function->getName(); $this->parser->setTarget(Target::TARGET_FUNCTION); $this->parser->setImports($this->getImports($function)); $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($function)); $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces); return $this->parser->parse($function->getDocComment(), $context); } /** * Gets a function annotation. * * @return object|null The Annotation or NULL, if the requested annotation does not exist. */ public function getFunctionAnnotation(ReflectionFunction $function, string $annotationName) { $annotations = $this->getFunctionAnnotations($function); foreach ($annotations as $annotation) { if ($annotation instanceof $annotationName) { return $annotation; } } return null; } /** * Returns the ignored annotations for the given class or function. * * @param ReflectionClass|ReflectionFunction $reflection * * @return array */ private function getIgnoredAnnotationNames($reflection): array { $type = $reflection instanceof ReflectionClass ? 'class' : 'function'; $name = $reflection->getName(); if (isset($this->ignoredAnnotationNames[$type][$name])) { return $this->ignoredAnnotationNames[$type][$name]; } $this->collectParsingMetadata($reflection); return $this->ignoredAnnotationNames[$type][$name]; } /** * Retrieves imports for a class or a function. * * @param ReflectionClass|ReflectionFunction $reflection * * @return array */ private function getImports($reflection): array { $type = $reflection instanceof ReflectionClass ? 'class' : 'function'; $name = $reflection->getName(); if (isset($this->imports[$type][$name])) { return $this->imports[$type][$name]; } $this->collectParsingMetadata($reflection); return $this->imports[$type][$name]; } /** * Retrieves imports for methods. * * @return array */ private function getMethodImports(ReflectionMethod $method) { $class = $method->getDeclaringClass(); $classImports = $this->getImports($class); $traitImports = []; foreach ($class->getTraits() as $trait) { if ( ! $trait->hasMethod($method->getName()) || $trait->getFileName() !== $method->getFileName() ) { continue; } $traitImports = array_merge($traitImports, $this->phpParser->parseUseStatements($trait)); } return array_merge($classImports, $traitImports); } /** * Retrieves imports for properties. * * @return array */ private function getPropertyImports(ReflectionProperty $property) { $class = $property->getDeclaringClass(); $classImports = $this->getImports($class); $traitImports = []; foreach ($class->getTraits() as $trait) { if (! $trait->hasProperty($property->getName())) { continue; } $traitImports = array_merge($traitImports, $this->phpParser->parseUseStatements($trait)); } return array_merge($classImports, $traitImports); } /** * Collects parsing metadata for a given class or function. * * @param ReflectionClass|ReflectionFunction $reflection */ private function collectParsingMetadata($reflection): void { $type = $reflection instanceof ReflectionClass ? 'class' : 'function'; $name = $reflection->getName(); $ignoredAnnotationNames = self::$globalIgnoredNames; $annotations = $this->preParser->parse($reflection->getDocComment(), $type . ' ' . $name); foreach ($annotations as $annotation) { if (! ($annotation instanceof IgnoreAnnotation)) { continue; } foreach ($annotation->names as $annot) { $ignoredAnnotationNames[$annot] = true; } } $this->imports[$type][$name] = array_merge( self::$globalImports, $this->phpParser->parseUseStatements($reflection), [ '__NAMESPACE__' => $reflection->getNamespaceName(), 'self' => $name, ] ); $this->ignoredAnnotationNames[$type][$name] = $ignoredAnnotationNames; } } ================================================ FILE: lib/Doctrine/Common/Annotations/AnnotationRegistry.php ================================================ */ final class DocLexer extends AbstractLexer { public const T_NONE = 1; public const T_INTEGER = 2; public const T_STRING = 3; public const T_FLOAT = 4; // All tokens that are also identifiers should be >= 100 public const T_IDENTIFIER = 100; public const T_AT = 101; public const T_CLOSE_CURLY_BRACES = 102; public const T_CLOSE_PARENTHESIS = 103; public const T_COMMA = 104; public const T_EQUALS = 105; public const T_FALSE = 106; public const T_NAMESPACE_SEPARATOR = 107; public const T_OPEN_CURLY_BRACES = 108; public const T_OPEN_PARENTHESIS = 109; public const T_TRUE = 110; public const T_NULL = 111; public const T_COLON = 112; public const T_MINUS = 113; /** @var array */ protected $noCase = [ '@' => self::T_AT, ',' => self::T_COMMA, '(' => self::T_OPEN_PARENTHESIS, ')' => self::T_CLOSE_PARENTHESIS, '{' => self::T_OPEN_CURLY_BRACES, '}' => self::T_CLOSE_CURLY_BRACES, '=' => self::T_EQUALS, ':' => self::T_COLON, '-' => self::T_MINUS, '\\' => self::T_NAMESPACE_SEPARATOR, ]; /** @var array */ protected $withCase = [ 'true' => self::T_TRUE, 'false' => self::T_FALSE, 'null' => self::T_NULL, ]; /** * Whether the next token starts immediately, or if there were * non-captured symbols before that */ public function nextTokenIsAdjacent(): bool { return $this->token === null || ($this->lookahead !== null && ($this->lookahead->position - $this->token->position) === strlen($this->token->value)); } /** * {@inheritDoc} */ protected function getCatchablePatterns() { return [ '[a-z_\\\][a-z0-9_\:\\\]*[a-z_][a-z0-9_]*', '(?:[+-]?[0-9]+(?:[\.][0-9]+)*)(?:[eE][+-]?[0-9]+)?', '"(?:""|[^"])*+"', ]; } /** * {@inheritDoc} */ protected function getNonCatchablePatterns() { return ['\s+', '\*+', '(.)']; } /** * {@inheritDoc} */ protected function getType(&$value) { $type = self::T_NONE; if ($value[0] === '"') { $value = str_replace('""', '"', substr($value, 1, strlen($value) - 2)); return self::T_STRING; } if (isset($this->noCase[$value])) { return $this->noCase[$value]; } if ($value[0] === '_' || $value[0] === '\\' || ctype_alpha($value[0])) { return self::T_IDENTIFIER; } $lowerValue = strtolower($value); if (isset($this->withCase[$lowerValue])) { return $this->withCase[$lowerValue]; } // Checking numeric value if (is_numeric($value)) { return strpos($value, '.') !== false || stripos($value, 'e') !== false ? self::T_FLOAT : self::T_INTEGER; } return $type; } } ================================================ FILE: lib/Doctrine/Common/Annotations/DocParser.php ================================================ , named_arguments?: array} */ final class DocParser { /** * An array of all valid tokens for a class name. * * @phpstan-var list */ private static $classIdentifiers = [ DocLexer::T_IDENTIFIER, DocLexer::T_TRUE, DocLexer::T_FALSE, DocLexer::T_NULL, ]; /** * The lexer. * * @var DocLexer */ private $lexer; /** * Current target context. * * @var int */ private $target; /** * Doc parser used to collect annotation target. * * @var DocParser */ private static $metadataParser; /** * Flag to control if the current annotation is nested or not. * * @var bool */ private $isNestedAnnotation = false; /** * Hashmap containing all use-statements that are to be used when parsing * the given doc block. * * @var array */ private $imports = []; /** * This hashmap is used internally to cache results of class_exists() * look-ups. * * @var array */ private $classExists = []; /** * Whether annotations that have not been imported should be ignored. * * @var bool */ private $ignoreNotImportedAnnotations = false; /** * An array of default namespaces if operating in simple mode. * * @var string[] */ private $namespaces = []; /** * A list with annotations that are not causing exceptions when not resolved to an annotation class. * * The names must be the raw names as used in the class, not the fully qualified * * @var bool[] indexed by annotation name */ private $ignoredAnnotationNames = []; /** * A list with annotations in namespaced format * that are not causing exceptions when not resolved to an annotation class. * * @var bool[] indexed by namespace name */ private $ignoredAnnotationNamespaces = []; /** @var string */ private $context = ''; /** * Hash-map for caching annotation metadata. * * @var array */ private static $annotationMetadata = [ Annotation\Target::class => [ 'is_annotation' => true, 'has_constructor' => true, 'has_named_argument_constructor' => false, 'properties' => [], 'targets_literal' => 'ANNOTATION_CLASS', 'targets' => Target::TARGET_CLASS, 'default_property' => 'value', 'attribute_types' => [ 'value' => [ 'required' => false, 'type' => 'array', 'array_type' => 'string', 'value' => 'array', ], ], ], Annotation\Attribute::class => [ 'is_annotation' => true, 'has_constructor' => false, 'has_named_argument_constructor' => false, 'targets_literal' => 'ANNOTATION_ANNOTATION', 'targets' => Target::TARGET_ANNOTATION, 'default_property' => 'name', 'properties' => [ 'name' => 'name', 'type' => 'type', 'required' => 'required', ], 'attribute_types' => [ 'value' => [ 'required' => true, 'type' => 'string', 'value' => 'string', ], 'type' => [ 'required' => true, 'type' => 'string', 'value' => 'string', ], 'required' => [ 'required' => false, 'type' => 'boolean', 'value' => 'boolean', ], ], ], Annotation\Attributes::class => [ 'is_annotation' => true, 'has_constructor' => false, 'has_named_argument_constructor' => false, 'targets_literal' => 'ANNOTATION_CLASS', 'targets' => Target::TARGET_CLASS, 'default_property' => 'value', 'properties' => ['value' => 'value'], 'attribute_types' => [ 'value' => [ 'type' => 'array', 'required' => true, 'array_type' => Annotation\Attribute::class, 'value' => 'array<' . Annotation\Attribute::class . '>', ], ], ], Annotation\Enum::class => [ 'is_annotation' => true, 'has_constructor' => true, 'has_named_argument_constructor' => false, 'targets_literal' => 'ANNOTATION_PROPERTY', 'targets' => Target::TARGET_PROPERTY, 'default_property' => 'value', 'properties' => ['value' => 'value'], 'attribute_types' => [ 'value' => [ 'type' => 'array', 'required' => true, ], 'literal' => [ 'type' => 'array', 'required' => false, ], ], ], Annotation\NamedArgumentConstructor::class => [ 'is_annotation' => true, 'has_constructor' => false, 'has_named_argument_constructor' => false, 'targets_literal' => 'ANNOTATION_CLASS', 'targets' => Target::TARGET_CLASS, 'default_property' => null, 'properties' => [], 'attribute_types' => [], ], ]; /** * Hash-map for handle types declaration. * * @var array */ private static $typeMap = [ 'float' => 'double', 'bool' => 'boolean', // allow uppercase Boolean in honor of George Boole 'Boolean' => 'boolean', 'int' => 'integer', ]; /** * Constructs a new DocParser. */ public function __construct() { $this->lexer = new DocLexer(); } /** * Sets the annotation names that are ignored during the parsing process. * * The names are supposed to be the raw names as used in the class, not the * fully qualified class names. * * @param bool[] $names indexed by annotation name * * @return void */ public function setIgnoredAnnotationNames(array $names) { $this->ignoredAnnotationNames = $names; } /** * Sets the annotation namespaces that are ignored during the parsing process. * * @param bool[] $ignoredAnnotationNamespaces indexed by annotation namespace name * * @return void */ public function setIgnoredAnnotationNamespaces(array $ignoredAnnotationNamespaces) { $this->ignoredAnnotationNamespaces = $ignoredAnnotationNamespaces; } /** * Sets ignore on not-imported annotations. * * @return void */ public function setIgnoreNotImportedAnnotations(bool $bool) { $this->ignoreNotImportedAnnotations = $bool; } /** * Sets the default namespaces. * * @return void * * @throws RuntimeException */ public function addNamespace(string $namespace) { if ($this->imports) { throw new RuntimeException('You must either use addNamespace(), or setImports(), but not both.'); } $this->namespaces[] = $namespace; } /** * Sets the imports. * * @param array $imports * * @return void * * @throws RuntimeException */ public function setImports(array $imports) { if ($this->namespaces) { throw new RuntimeException('You must either use addNamespace(), or setImports(), but not both.'); } $this->imports = $imports; } /** * Sets current target context as bitmask. * * @return void */ public function setTarget(int $target) { $this->target = $target; } /** * Parses the given docblock string for annotations. * * @phpstan-return list Array of annotations. If no annotations are found, an empty array is returned. * * @throws AnnotationException * @throws ReflectionException */ public function parse(string $input, string $context = '') { $pos = $this->findInitialTokenPosition($input); if ($pos === null) { return []; } $this->context = $context; $this->lexer->setInput(trim(substr($input, $pos), '* /')); $this->lexer->moveNext(); return $this->Annotations(); } /** * Finds the first valid annotation */ private function findInitialTokenPosition(string $input): ?int { $pos = 0; // search for first valid annotation while (($pos = strpos($input, '@', $pos)) !== false) { $preceding = substr($input, $pos - 1, 1); // if the @ is preceded by a space, a tab or * it is valid if ($pos === 0 || $preceding === ' ' || $preceding === '*' || $preceding === "\t") { return $pos; } $pos++; } return null; } /** * Attempts to match the given token with the current lookahead token. * If they match, updates the lookahead token; otherwise raises a syntax error. * * @param int $token Type of token. * * @return bool True if tokens match; false otherwise. * * @throws AnnotationException */ private function match(int $token): bool { if (! $this->lexer->isNextToken($token)) { throw $this->syntaxError($this->lexer->getLiteral($token)); } return $this->lexer->moveNext(); } /** * Attempts to match the current lookahead token with any of the given tokens. * * If any of them matches, this method updates the lookahead token; otherwise * a syntax error is raised. * * @phpstan-param list $tokens * * @throws AnnotationException */ private function matchAny(array $tokens): bool { if (! $this->lexer->isNextTokenAny($tokens)) { throw $this->syntaxError(implode(' or ', array_map([$this->lexer, 'getLiteral'], $tokens))); } return $this->lexer->moveNext(); } /** * Generates a new syntax error. * * @param string $expected Expected string. * @param mixed[]|null $token Optional token. */ private function syntaxError(string $expected, ?array $token = null): AnnotationException { if ($token === null) { $token = $this->lexer->lookahead; } $message = sprintf('Expected %s, got ', $expected); $message .= $this->lexer->lookahead === null ? 'end of string' : sprintf("'%s' at position %s", $token->value, $token->position); if (strlen($this->context)) { $message .= ' in ' . $this->context; } $message .= '.'; return AnnotationException::syntaxError($message); } /** * Attempts to check if a class exists or not. This never goes through the PHP autoloading mechanism * but uses the {@link AnnotationRegistry} to load classes. * * @param class-string $fqcn */ private function classExists(string $fqcn): bool { if (isset($this->classExists[$fqcn])) { return $this->classExists[$fqcn]; } // first check if the class already exists, maybe loaded through another AnnotationReader if (class_exists($fqcn, false)) { return $this->classExists[$fqcn] = true; } // final check, does this class exist? return $this->classExists[$fqcn] = AnnotationRegistry::loadAnnotationClass($fqcn); } /** * Collects parsing metadata for a given annotation class * * @param class-string $name The annotation name * * @throws AnnotationException * @throws ReflectionException */ private function collectAnnotationMetadata(string $name): void { if (self::$metadataParser === null) { self::$metadataParser = new self(); self::$metadataParser->setIgnoreNotImportedAnnotations(true); self::$metadataParser->setIgnoredAnnotationNames($this->ignoredAnnotationNames); self::$metadataParser->setImports([ 'enum' => Enum::class, 'target' => Target::class, 'attribute' => Attribute::class, 'attributes' => Attributes::class, 'namedargumentconstructor' => NamedArgumentConstructor::class, ]); // Make sure that annotations from metadata are loaded class_exists(Enum::class); class_exists(Target::class); class_exists(Attribute::class); class_exists(Attributes::class); class_exists(NamedArgumentConstructor::class); } $class = new ReflectionClass($name); $docComment = $class->getDocComment(); // Sets default values for annotation metadata $constructor = $class->getConstructor(); $metadata = [ 'default_property' => null, 'has_constructor' => $constructor !== null && $constructor->getNumberOfParameters() > 0, 'constructor_args' => [], 'properties' => [], 'property_types' => [], 'attribute_types' => [], 'targets_literal' => null, 'targets' => Target::TARGET_ALL, 'is_annotation' => strpos($docComment, '@Annotation') !== false, ]; $metadata['has_named_argument_constructor'] = false; // verify that the class is really meant to be an annotation if ($metadata['is_annotation']) { self::$metadataParser->setTarget(Target::TARGET_CLASS); foreach (self::$metadataParser->parse($docComment, 'class @' . $name) as $annotation) { if ($annotation instanceof Target) { $metadata['targets'] = $annotation->targets; $metadata['targets_literal'] = $annotation->literal; continue; } if ($annotation instanceof NamedArgumentConstructor) { $metadata['has_named_argument_constructor'] = $metadata['has_constructor']; if ($metadata['has_named_argument_constructor']) { // choose the first argument as the default property $metadata['default_property'] = $constructor->getParameters()[0]->getName(); } } if (! ($annotation instanceof Attributes)) { continue; } foreach ($annotation->value as $attribute) { $this->collectAttributeTypeMetadata($metadata, $attribute); } } // if not has a constructor will inject values into public properties if ($metadata['has_constructor'] === false) { // collect all public properties foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { $metadata['properties'][$property->name] = $property->name; $propertyComment = $property->getDocComment(); if ($propertyComment === false) { continue; } $attribute = new Attribute(); $attribute->required = (strpos($propertyComment, '@Required') !== false); $attribute->name = $property->name; $attribute->type = (strpos($propertyComment, '@var') !== false && preg_match('/@var\s+([^\s]+)/', $propertyComment, $matches)) ? $matches[1] : 'mixed'; $this->collectAttributeTypeMetadata($metadata, $attribute); // checks if the property has @Enum if (strpos($propertyComment, '@Enum') === false) { continue; } $context = 'property ' . $class->name . '::$' . $property->name; self::$metadataParser->setTarget(Target::TARGET_PROPERTY); foreach (self::$metadataParser->parse($propertyComment, $context) as $annotation) { if (! $annotation instanceof Enum) { continue; } $metadata['enum'][$property->name]['value'] = $annotation->value; $metadata['enum'][$property->name]['literal'] = (! empty($annotation->literal)) ? $annotation->literal : $annotation->value; } } // choose the first property as default property $metadata['default_property'] = reset($metadata['properties']); } elseif ($metadata['has_named_argument_constructor']) { foreach ($constructor->getParameters() as $parameter) { if ($parameter->isVariadic()) { break; } $metadata['constructor_args'][$parameter->getName()] = [ 'position' => $parameter->getPosition(), 'default' => $parameter->isOptional() ? $parameter->getDefaultValue() : null, ]; } } } self::$annotationMetadata[$name] = $metadata; } /** * Collects parsing metadata for a given attribute. * * @param mixed[] $metadata */ private function collectAttributeTypeMetadata(array &$metadata, Attribute $attribute): void { // handle internal type declaration $type = self::$typeMap[$attribute->type] ?? $attribute->type; // handle the case if the property type is mixed if ($type === 'mixed') { return; } // Evaluate type $pos = strpos($type, '<'); if ($pos !== false) { // Checks if the property has array $arrayType = substr($type, $pos + 1, -1); $type = 'array'; if (isset(self::$typeMap[$arrayType])) { $arrayType = self::$typeMap[$arrayType]; } $metadata['attribute_types'][$attribute->name]['array_type'] = $arrayType; } else { // Checks if the property has type[] $pos = strrpos($type, '['); if ($pos !== false) { $arrayType = substr($type, 0, $pos); $type = 'array'; if (isset(self::$typeMap[$arrayType])) { $arrayType = self::$typeMap[$arrayType]; } $metadata['attribute_types'][$attribute->name]['array_type'] = $arrayType; } } $metadata['attribute_types'][$attribute->name]['type'] = $type; $metadata['attribute_types'][$attribute->name]['value'] = $attribute->type; $metadata['attribute_types'][$attribute->name]['required'] = $attribute->required; } /** * Annotations ::= Annotation {[ "*" ]* [Annotation]}* * * @phpstan-return list * * @throws AnnotationException * @throws ReflectionException */ private function Annotations(): array { $annotations = []; while ($this->lexer->lookahead !== null) { if ($this->lexer->lookahead->type !== DocLexer::T_AT) { $this->lexer->moveNext(); continue; } // make sure the @ is preceded by non-catchable pattern if ( $this->lexer->token !== null && $this->lexer->lookahead->position === $this->lexer->token->position + strlen( $this->lexer->token->value ) ) { $this->lexer->moveNext(); continue; } // make sure the @ is followed by either a namespace separator, or // an identifier token $peek = $this->lexer->glimpse(); if ( ($peek === null) || ($peek->type !== DocLexer::T_NAMESPACE_SEPARATOR && ! in_array( $peek->type, self::$classIdentifiers, true )) || $peek->position !== $this->lexer->lookahead->position + 1 ) { $this->lexer->moveNext(); continue; } $this->isNestedAnnotation = false; $annot = $this->Annotation(); if ($annot === false) { continue; } $annotations[] = $annot; } return $annotations; } /** * Annotation ::= "@" AnnotationName MethodCall * AnnotationName ::= QualifiedName | SimpleName * QualifiedName ::= NameSpacePart "\" {NameSpacePart "\"}* SimpleName * NameSpacePart ::= identifier | null | false | true * SimpleName ::= identifier | null | false | true * * @return object|false False if it is not a valid annotation. * * @throws AnnotationException * @throws ReflectionException */ private function Annotation() { $this->match(DocLexer::T_AT); // check if we have an annotation $name = $this->Identifier(); if ( $this->lexer->isNextToken(DocLexer::T_MINUS) && $this->lexer->nextTokenIsAdjacent() ) { // Annotations with dashes, such as "@foo-" or "@foo-bar", are to be discarded return false; } // only process names which are not fully qualified, yet // fully qualified names must start with a \ $originalName = $name; if ($name[0] !== '\\') { $pos = strpos($name, '\\'); $alias = ($pos === false) ? $name : substr($name, 0, $pos); $found = false; $loweredAlias = strtolower($alias); if ($this->namespaces) { foreach ($this->namespaces as $namespace) { if ($this->classExists($namespace . '\\' . $name)) { $name = $namespace . '\\' . $name; $found = true; break; } } } elseif (isset($this->imports[$loweredAlias])) { $namespace = ltrim($this->imports[$loweredAlias], '\\'); $name = ($pos !== false) ? $namespace . substr($name, $pos) : $namespace; $found = $this->classExists($name); } elseif ( ! isset($this->ignoredAnnotationNames[$name]) && isset($this->imports['__NAMESPACE__']) && $this->classExists($this->imports['__NAMESPACE__'] . '\\' . $name) ) { $name = $this->imports['__NAMESPACE__'] . '\\' . $name; $found = true; } elseif (! isset($this->ignoredAnnotationNames[$name]) && $this->classExists($name)) { $found = true; } if (! $found) { if ($this->isIgnoredAnnotation($name)) { return false; } throw AnnotationException::semanticalError(sprintf( <<<'EXCEPTION' The annotation "@%s" in %s was never imported. Did you maybe forget to add a "use" statement for this annotation? EXCEPTION , $name, $this->context )); } } $name = ltrim($name, '\\'); if (! $this->classExists($name)) { throw AnnotationException::semanticalError(sprintf( 'The annotation "@%s" in %s does not exist, or could not be auto-loaded.', $name, $this->context )); } // at this point, $name contains the fully qualified class name of the // annotation, and it is also guaranteed that this class exists, and // that it is loaded // collects the metadata annotation only if there is not yet if (! isset(self::$annotationMetadata[$name])) { $this->collectAnnotationMetadata($name); } // verify that the class is really meant to be an annotation and not just any ordinary class if (self::$annotationMetadata[$name]['is_annotation'] === false) { if ($this->isIgnoredAnnotation($originalName) || $this->isIgnoredAnnotation($name)) { return false; } throw AnnotationException::semanticalError(sprintf( <<<'EXCEPTION' The class "%s" is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the _class_ doc comment of "%s". If it is indeed no annotation, then you need to add @IgnoreAnnotation("%s") to the _class_ doc comment of %s. EXCEPTION , $name, $name, $originalName, $this->context )); } //if target is nested annotation $target = $this->isNestedAnnotation ? Target::TARGET_ANNOTATION : $this->target; // Next will be nested $this->isNestedAnnotation = true; //if annotation does not support current target if ((self::$annotationMetadata[$name]['targets'] & $target) === 0 && $target) { throw AnnotationException::semanticalError( sprintf( <<<'EXCEPTION' Annotation @%s is not allowed to be declared on %s. You may only use this annotation on these code elements: %s. EXCEPTION , $originalName, $this->context, self::$annotationMetadata[$name]['targets_literal'] ) ); } $arguments = $this->MethodCall(); $values = $this->resolvePositionalValues($arguments, $name); if (isset(self::$annotationMetadata[$name]['enum'])) { // checks all declared attributes foreach (self::$annotationMetadata[$name]['enum'] as $property => $enum) { // checks if the attribute is a valid enumerator if (isset($values[$property]) && ! in_array($values[$property], $enum['value'])) { throw AnnotationException::enumeratorError( $property, $name, $this->context, $enum['literal'], $values[$property] ); } } } // checks all declared attributes foreach (self::$annotationMetadata[$name]['attribute_types'] as $property => $type) { if ( $property === self::$annotationMetadata[$name]['default_property'] && ! isset($values[$property]) && isset($values['value']) ) { $property = 'value'; } // handle a not given attribute or null value if (! isset($values[$property])) { if ($type['required']) { throw AnnotationException::requiredError( $property, $originalName, $this->context, 'a(n) ' . $type['value'] ); } continue; } if ($type['type'] === 'array') { // handle the case of a single value if (! is_array($values[$property])) { $values[$property] = [$values[$property]]; } // checks if the attribute has array type declaration, such as "array" if (isset($type['array_type'])) { foreach ($values[$property] as $item) { if (gettype($item) !== $type['array_type'] && ! $item instanceof $type['array_type']) { throw AnnotationException::attributeTypeError( $property, $originalName, $this->context, 'either a(n) ' . $type['array_type'] . ', or an array of ' . $type['array_type'] . 's', $item ); } } } } elseif (gettype($values[$property]) !== $type['type'] && ! $values[$property] instanceof $type['type']) { throw AnnotationException::attributeTypeError( $property, $originalName, $this->context, 'a(n) ' . $type['value'], $values[$property] ); } } if (self::$annotationMetadata[$name]['has_named_argument_constructor']) { if (PHP_VERSION_ID >= 80000) { foreach ($values as $property => $value) { if (! isset(self::$annotationMetadata[$name]['constructor_args'][$property])) { throw AnnotationException::creationError(sprintf( <<<'EXCEPTION' The annotation @%s declared on %s does not have a property named "%s" that can be set through its named arguments constructor. Available named arguments: %s EXCEPTION , $originalName, $this->context, $property, implode(', ', array_keys(self::$annotationMetadata[$name]['constructor_args'])) )); } } return $this->instantiateAnnotiation($originalName, $this->context, $name, $values); } $positionalValues = []; foreach (self::$annotationMetadata[$name]['constructor_args'] as $property => $parameter) { $positionalValues[$parameter['position']] = $parameter['default']; } foreach ($values as $property => $value) { if (! isset(self::$annotationMetadata[$name]['constructor_args'][$property])) { throw AnnotationException::creationError(sprintf( <<<'EXCEPTION' The annotation @%s declared on %s does not have a property named "%s" that can be set through its named arguments constructor. Available named arguments: %s EXCEPTION , $originalName, $this->context, $property, implode(', ', array_keys(self::$annotationMetadata[$name]['constructor_args'])) )); } $positionalValues[self::$annotationMetadata[$name]['constructor_args'][$property]['position']] = $value; } return $this->instantiateAnnotiation($originalName, $this->context, $name, $positionalValues); } // check if the annotation expects values via the constructor, // or directly injected into public properties if (self::$annotationMetadata[$name]['has_constructor'] === true) { return $this->instantiateAnnotiation($originalName, $this->context, $name, [$values]); } $instance = $this->instantiateAnnotiation($originalName, $this->context, $name, []); foreach ($values as $property => $value) { if (! isset(self::$annotationMetadata[$name]['properties'][$property])) { if ($property !== 'value') { throw AnnotationException::creationError(sprintf( <<<'EXCEPTION' The annotation @%s declared on %s does not have a property named "%s". Available properties: %s EXCEPTION , $originalName, $this->context, $property, implode(', ', self::$annotationMetadata[$name]['properties']) )); } // handle the case if the property has no annotations $property = self::$annotationMetadata[$name]['default_property']; if (! $property) { throw AnnotationException::creationError(sprintf( 'The annotation @%s declared on %s does not accept any values, but got %s.', $originalName, $this->context, json_encode($values) )); } } $instance->{$property} = $value; } return $instance; } /** * MethodCall ::= ["(" [Values] ")"] * * @psalm-return Arguments * * @throws AnnotationException * @throws ReflectionException */ private function MethodCall(): array { $values = []; if (! $this->lexer->isNextToken(DocLexer::T_OPEN_PARENTHESIS)) { return $values; } $this->match(DocLexer::T_OPEN_PARENTHESIS); if (! $this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) { $values = $this->Values(); } $this->match(DocLexer::T_CLOSE_PARENTHESIS); return $values; } /** * Values ::= Array | Value {"," Value}* [","] * * @psalm-return Arguments * * @throws AnnotationException * @throws ReflectionException */ private function Values(): array { $values = [$this->Value()]; while ($this->lexer->isNextToken(DocLexer::T_COMMA)) { $this->match(DocLexer::T_COMMA); if ($this->lexer->isNextToken(DocLexer::T_CLOSE_PARENTHESIS)) { break; } $token = $this->lexer->lookahead; $value = $this->Value(); $values[] = $value; } $namedArguments = []; $positionalArguments = []; foreach ($values as $k => $value) { if (is_object($value) && $value instanceof stdClass) { $namedArguments[$value->name] = $value->value; } else { $positionalArguments[$k] = $value; } } return ['named_arguments' => $namedArguments, 'positional_arguments' => $positionalArguments]; } /** * Constant ::= integer | string | float | boolean * * @return mixed * * @throws AnnotationException */ private function Constant() { $identifier = $this->Identifier(); if (! defined($identifier) && strpos($identifier, '::') !== false && $identifier[0] !== '\\') { [$className, $const] = explode('::', $identifier); $pos = strpos($className, '\\'); $alias = ($pos === false) ? $className : substr($className, 0, $pos); $found = false; $loweredAlias = strtolower($alias); switch (true) { case ! empty($this->namespaces): foreach ($this->namespaces as $ns) { if (class_exists($ns . '\\' . $className) || interface_exists($ns . '\\' . $className)) { $className = $ns . '\\' . $className; $found = true; break; } } break; case isset($this->imports[$loweredAlias]): $found = true; $className = ($pos !== false) ? $this->imports[$loweredAlias] . substr($className, $pos) : $this->imports[$loweredAlias]; break; default: if (isset($this->imports['__NAMESPACE__'])) { $ns = $this->imports['__NAMESPACE__']; if (class_exists($ns . '\\' . $className) || interface_exists($ns . '\\' . $className)) { $className = $ns . '\\' . $className; $found = true; } } break; } if ($found) { $identifier = $className . '::' . $const; } } /** * Checks if identifier ends with ::class and remove the leading backslash if it exists. */ if ( $this->identifierEndsWithClassConstant($identifier) && ! $this->identifierStartsWithBackslash($identifier) ) { return substr($identifier, 0, $this->getClassConstantPositionInIdentifier($identifier)); } if ($this->identifierEndsWithClassConstant($identifier) && $this->identifierStartsWithBackslash($identifier)) { return substr($identifier, 1, $this->getClassConstantPositionInIdentifier($identifier) - 1); } if (! defined($identifier)) { throw AnnotationException::semanticalErrorConstants($identifier, $this->context); } return constant($identifier); } private function identifierStartsWithBackslash(string $identifier): bool { return $identifier[0] === '\\'; } private function identifierEndsWithClassConstant(string $identifier): bool { return $this->getClassConstantPositionInIdentifier($identifier) === strlen($identifier) - strlen('::class'); } /** @return int|false */ private function getClassConstantPositionInIdentifier(string $identifier) { return stripos($identifier, '::class'); } /** * Identifier ::= string * * @throws AnnotationException */ private function Identifier(): string { // check if we have an annotation if (! $this->lexer->isNextTokenAny(self::$classIdentifiers)) { throw $this->syntaxError('namespace separator or identifier'); } $this->lexer->moveNext(); $className = $this->lexer->token->value; while ( $this->lexer->lookahead !== null && $this->lexer->lookahead->position === ($this->lexer->token->position + strlen($this->lexer->token->value)) && $this->lexer->isNextToken(DocLexer::T_NAMESPACE_SEPARATOR) ) { $this->match(DocLexer::T_NAMESPACE_SEPARATOR); $this->matchAny(self::$classIdentifiers); $className .= '\\' . $this->lexer->token->value; } return $className; } /** * Value ::= PlainValue | FieldAssignment * * @return mixed * * @throws AnnotationException * @throws ReflectionException */ private function Value() { $peek = $this->lexer->glimpse(); if ($peek->type === DocLexer::T_EQUALS) { return $this->FieldAssignment(); } return $this->PlainValue(); } /** * PlainValue ::= integer | string | float | boolean | Array | Annotation * * @return mixed * * @throws AnnotationException * @throws ReflectionException */ private function PlainValue() { if ($this->lexer->isNextToken(DocLexer::T_OPEN_CURLY_BRACES)) { return $this->Arrayx(); } if ($this->lexer->isNextToken(DocLexer::T_AT)) { return $this->Annotation(); } if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) { return $this->Constant(); } switch ($this->lexer->lookahead->type) { case DocLexer::T_STRING: $this->match(DocLexer::T_STRING); return $this->lexer->token->value; case DocLexer::T_INTEGER: $this->match(DocLexer::T_INTEGER); return (int) $this->lexer->token->value; case DocLexer::T_FLOAT: $this->match(DocLexer::T_FLOAT); return (float) $this->lexer->token->value; case DocLexer::T_TRUE: $this->match(DocLexer::T_TRUE); return true; case DocLexer::T_FALSE: $this->match(DocLexer::T_FALSE); return false; case DocLexer::T_NULL: $this->match(DocLexer::T_NULL); return null; default: throw $this->syntaxError('PlainValue'); } } /** * FieldAssignment ::= FieldName "=" PlainValue * FieldName ::= identifier * * @throws AnnotationException * @throws ReflectionException */ private function FieldAssignment(): stdClass { $this->match(DocLexer::T_IDENTIFIER); $fieldName = $this->lexer->token->value; $this->match(DocLexer::T_EQUALS); $item = new stdClass(); $item->name = $fieldName; $item->value = $this->PlainValue(); return $item; } /** * Array ::= "{" ArrayEntry {"," ArrayEntry}* [","] "}" * * @return mixed[] * * @throws AnnotationException * @throws ReflectionException */ private function Arrayx(): array { $array = $values = []; $this->match(DocLexer::T_OPEN_CURLY_BRACES); // If the array is empty, stop parsing and return. if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) { $this->match(DocLexer::T_CLOSE_CURLY_BRACES); return $array; } $values[] = $this->ArrayEntry(); while ($this->lexer->isNextToken(DocLexer::T_COMMA)) { $this->match(DocLexer::T_COMMA); // optional trailing comma if ($this->lexer->isNextToken(DocLexer::T_CLOSE_CURLY_BRACES)) { break; } $values[] = $this->ArrayEntry(); } $this->match(DocLexer::T_CLOSE_CURLY_BRACES); foreach ($values as $value) { [$key, $val] = $value; if ($key !== null) { $array[$key] = $val; } else { $array[] = $val; } } return $array; } /** * ArrayEntry ::= Value | KeyValuePair * KeyValuePair ::= Key ("=" | ":") PlainValue | Constant * Key ::= string | integer | Constant * * @phpstan-return array{mixed, mixed} * * @throws AnnotationException * @throws ReflectionException */ private function ArrayEntry(): array { $peek = $this->lexer->glimpse(); if ( $peek->type === DocLexer::T_EQUALS || $peek->type === DocLexer::T_COLON ) { if ($this->lexer->isNextToken(DocLexer::T_IDENTIFIER)) { $key = $this->Constant(); } else { $this->matchAny([DocLexer::T_INTEGER, DocLexer::T_STRING]); $key = $this->lexer->token->value; } $this->matchAny([DocLexer::T_EQUALS, DocLexer::T_COLON]); return [$key, $this->PlainValue()]; } return [null, $this->Value()]; } /** * Checks whether the given $name matches any ignored annotation name or namespace */ private function isIgnoredAnnotation(string $name): bool { if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) { return true; } foreach (array_keys($this->ignoredAnnotationNamespaces) as $ignoredAnnotationNamespace) { $ignoredAnnotationNamespace = rtrim($ignoredAnnotationNamespace, '\\') . '\\'; if (stripos(rtrim($name, '\\') . '\\', $ignoredAnnotationNamespace) === 0) { return true; } } return false; } /** * Resolve positional arguments (without name) to named ones * * @psalm-param Arguments $arguments * * @return array */ private function resolvePositionalValues(array $arguments, string $name): array { $positionalArguments = $arguments['positional_arguments'] ?? []; $values = $arguments['named_arguments'] ?? []; if ( self::$annotationMetadata[$name]['has_named_argument_constructor'] && self::$annotationMetadata[$name]['default_property'] !== null ) { // We must ensure that we don't have positional arguments after named ones $positions = array_keys($positionalArguments); $lastPosition = null; foreach ($positions as $position) { if ( ($lastPosition === null && $position !== 0) || ($lastPosition !== null && $position !== $lastPosition + 1) ) { throw $this->syntaxError('Positional arguments after named arguments is not allowed'); } $lastPosition = $position; } foreach (self::$annotationMetadata[$name]['constructor_args'] as $property => $parameter) { $position = $parameter['position']; if (isset($values[$property]) || ! isset($positionalArguments[$position])) { continue; } $values[$property] = $positionalArguments[$position]; } } else { if (count($positionalArguments) > 0 && ! isset($values['value'])) { if (count($positionalArguments) === 1) { $value = array_pop($positionalArguments); } else { $value = array_values($positionalArguments); } $values['value'] = $value; } } return $values; } /** * Try to instantiate the annotation and catch and process any exceptions related to failure * * @param class-string $name * @param array $arguments * * @return object * * @throws AnnotationException */ private function instantiateAnnotiation(string $originalName, string $context, string $name, array $arguments) { try { return new $name(...$arguments); } catch (Throwable $exception) { throw AnnotationException::creationError( sprintf( 'An error occurred while instantiating the annotation @%s declared on %s: "%s".', $originalName, $context, $exception->getMessage() ), $exception ); } } } ================================================ FILE: lib/Doctrine/Common/Annotations/ImplicitlyIgnoredAnnotationNames.php ================================================ true, 'Attribute' => true, 'Attributes' => true, /* Can we enable this? 'Enum' => true, */ 'Required' => true, 'Target' => true, 'NamedArgumentConstructor' => true, ]; private const WidelyUsedNonStandard = [ 'fix' => true, 'fixme' => true, 'override' => true, ]; private const PhpDocumentor1 = [ 'abstract' => true, 'access' => true, 'code' => true, 'deprec' => true, 'endcode' => true, 'exception' => true, 'final' => true, 'ingroup' => true, 'inheritdoc' => true, 'inheritDoc' => true, 'magic' => true, 'name' => true, 'private' => true, 'static' => true, 'staticvar' => true, 'staticVar' => true, 'toc' => true, 'tutorial' => true, 'throw' => true, ]; private const PhpDocumentor2 = [ 'api' => true, 'author' => true, 'category' => true, 'copyright' => true, 'deprecated' => true, 'example' => true, 'filesource' => true, 'global' => true, 'ignore' => true, /* Can we enable this? 'index' => true, */ 'internal' => true, 'license' => true, 'link' => true, 'method' => true, 'package' => true, 'param' => true, 'property' => true, 'property-read' => true, 'property-write' => true, 'return' => true, 'see' => true, 'since' => true, 'source' => true, 'subpackage' => true, 'throws' => true, 'todo' => true, 'TODO' => true, 'usedby' => true, 'uses' => true, 'var' => true, 'version' => true, ]; private const PHPUnit = [ 'author' => true, 'after' => true, 'afterClass' => true, 'backupGlobals' => true, 'backupStaticAttributes' => true, 'before' => true, 'beforeClass' => true, 'codeCoverageIgnore' => true, 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true, 'covers' => true, 'coversDefaultClass' => true, 'coversNothing' => true, 'dataProvider' => true, 'depends' => true, 'doesNotPerformAssertions' => true, 'expectedException' => true, 'expectedExceptionCode' => true, 'expectedExceptionMessage' => true, 'expectedExceptionMessageRegExp' => true, 'group' => true, 'large' => true, 'medium' => true, 'preserveGlobalState' => true, 'requires' => true, 'runTestsInSeparateProcesses' => true, 'runInSeparateProcess' => true, 'small' => true, 'test' => true, 'testdox' => true, 'testWith' => true, 'ticket' => true, 'uses' => true, ]; private const PhpCheckStyle = ['SuppressWarnings' => true]; private const PhpStorm = ['noinspection' => true]; private const PEAR = ['package_version' => true]; private const PlainUML = [ 'startuml' => true, 'enduml' => true, ]; private const Symfony = ['experimental' => true]; private const PhpCodeSniffer = [ 'codingStandardsIgnoreStart' => true, 'codingStandardsIgnoreEnd' => true, ]; private const SlevomatCodingStandard = ['phpcsSuppress' => true]; private const Phan = ['suppress' => true]; private const Rector = ['noRector' => true]; private const StaticAnalysis = [ // PHPStan, Psalm 'extends' => true, 'implements' => true, 'readonly' => true, 'template' => true, 'use' => true, // Psalm 'pure' => true, 'immutable' => true, ]; public const LIST = self::Reserved + self::WidelyUsedNonStandard + self::PhpDocumentor1 + self::PhpDocumentor2 + self::PHPUnit + self::PhpCheckStyle + self::PhpStorm + self::PEAR + self::PlainUML + self::Symfony + self::SlevomatCodingStandard + self::PhpCodeSniffer + self::Phan + self::Rector + self::StaticAnalysis; private function __construct() { } } ================================================ FILE: lib/Doctrine/Common/Annotations/IndexedReader.php ================================================ delegate = $reader; } /** * {@inheritDoc} */ public function getClassAnnotations(ReflectionClass $class) { $annotations = []; foreach ($this->delegate->getClassAnnotations($class) as $annot) { $annotations[get_class($annot)] = $annot; } return $annotations; } /** * {@inheritDoc} */ public function getClassAnnotation(ReflectionClass $class, $annotationName) { return $this->delegate->getClassAnnotation($class, $annotationName); } /** * {@inheritDoc} */ public function getMethodAnnotations(ReflectionMethod $method) { $annotations = []; foreach ($this->delegate->getMethodAnnotations($method) as $annot) { $annotations[get_class($annot)] = $annot; } return $annotations; } /** * {@inheritDoc} */ public function getMethodAnnotation(ReflectionMethod $method, $annotationName) { return $this->delegate->getMethodAnnotation($method, $annotationName); } /** * {@inheritDoc} */ public function getPropertyAnnotations(ReflectionProperty $property) { $annotations = []; foreach ($this->delegate->getPropertyAnnotations($property) as $annot) { $annotations[get_class($annot)] = $annot; } return $annotations; } /** * {@inheritDoc} */ public function getPropertyAnnotation(ReflectionProperty $property, $annotationName) { return $this->delegate->getPropertyAnnotation($property, $annotationName); } /** * Proxies all methods to the delegate. * * @param mixed[] $args * * @return mixed */ public function __call(string $method, array $args) { return call_user_func_array([$this->delegate, $method], $args); } } ================================================ FILE: lib/Doctrine/Common/Annotations/PhpParser.php ================================================ a list with use statements in the form (Alias => FQN). */ public function parseUseStatements($reflection): array { if (method_exists($reflection, 'getUseStatements')) { return $reflection->getUseStatements(); } $filename = $reflection->getFileName(); if ($filename === false) { return []; } $content = $this->getFileContent($filename, $reflection->getStartLine()); if ($content === null) { return []; } $namespace = preg_quote($reflection->getNamespaceName()); $content = preg_replace('/^.*?(\bnamespace\s+' . $namespace . '\s*[;{].*)$/s', '\\1', $content); $tokenizer = new TokenParser('parseUseStatements($reflection->getNamespaceName()); } /** * Gets the content of the file right up to the given line number. * * @param string $filename The name of the file to load. * @param int $lineNumber The number of lines to read from file. * * @return string|null The content of the file or null if the file does not exist. */ private function getFileContent(string $filename, $lineNumber) { if (! is_file($filename)) { return null; } $content = ''; $lineCnt = 0; $file = new SplFileObject($filename); while (! $file->eof()) { if ($lineCnt++ === $lineNumber) { break; } $content .= $file->fgets(); } return $content; } } ================================================ FILE: lib/Doctrine/Common/Annotations/PsrCachedReader.php ================================================ > */ private $loadedAnnotations = []; /** @var int[] */ private $loadedFilemtimes = []; public function __construct(Reader $reader, CacheItemPoolInterface $cache, bool $debug = false) { $this->delegate = $reader; $this->cache = $cache; $this->debug = (bool) $debug; } /** * {@inheritDoc} */ public function getClassAnnotations(ReflectionClass $class) { $cacheKey = $class->getName(); if (isset($this->loadedAnnotations[$cacheKey])) { return $this->loadedAnnotations[$cacheKey]; } $annots = $this->fetchFromCache($cacheKey, $class, 'getClassAnnotations', $class); return $this->loadedAnnotations[$cacheKey] = $annots; } /** * {@inheritDoc} */ public function getClassAnnotation(ReflectionClass $class, $annotationName) { foreach ($this->getClassAnnotations($class) as $annot) { if ($annot instanceof $annotationName) { return $annot; } } return null; } /** * {@inheritDoc} */ public function getPropertyAnnotations(ReflectionProperty $property) { $class = $property->getDeclaringClass(); $cacheKey = $class->getName() . '$' . $property->getName(); if (isset($this->loadedAnnotations[$cacheKey])) { return $this->loadedAnnotations[$cacheKey]; } $annots = $this->fetchFromCache($cacheKey, $class, 'getPropertyAnnotations', $property); return $this->loadedAnnotations[$cacheKey] = $annots; } /** * {@inheritDoc} */ public function getPropertyAnnotation(ReflectionProperty $property, $annotationName) { foreach ($this->getPropertyAnnotations($property) as $annot) { if ($annot instanceof $annotationName) { return $annot; } } return null; } /** * {@inheritDoc} */ public function getMethodAnnotations(ReflectionMethod $method) { $class = $method->getDeclaringClass(); $cacheKey = $class->getName() . '#' . $method->getName(); if (isset($this->loadedAnnotations[$cacheKey])) { return $this->loadedAnnotations[$cacheKey]; } $annots = $this->fetchFromCache($cacheKey, $class, 'getMethodAnnotations', $method); return $this->loadedAnnotations[$cacheKey] = $annots; } /** * {@inheritDoc} */ public function getMethodAnnotation(ReflectionMethod $method, $annotationName) { foreach ($this->getMethodAnnotations($method) as $annot) { if ($annot instanceof $annotationName) { return $annot; } } return null; } public function clearLoadedAnnotations(): void { $this->loadedAnnotations = []; $this->loadedFilemtimes = []; } /** @return mixed[] */ private function fetchFromCache( string $cacheKey, ReflectionClass $class, string $method, Reflector $reflector ): array { $cacheKey = rawurlencode($cacheKey); $item = $this->cache->getItem($cacheKey); if (($this->debug && ! $this->refresh($cacheKey, $class)) || ! $item->isHit()) { $this->cache->save($item->set($this->delegate->{$method}($reflector))); } return $item->get(); } /** * Used in debug mode to check if the cache is fresh. * * @return bool Returns true if the cache was fresh, or false if the class * being read was modified since writing to the cache. */ private function refresh(string $cacheKey, ReflectionClass $class): bool { $lastModification = $this->getLastModification($class); if ($lastModification === 0) { return true; } $item = $this->cache->getItem('[C]' . $cacheKey); if ($item->isHit() && $item->get() >= $lastModification) { return true; } $this->cache->save($item->set(time())); return false; } /** * Returns the time the class was last modified, testing traits and parents */ private function getLastModification(ReflectionClass $class): int { $filename = $class->getFileName(); if (isset($this->loadedFilemtimes[$filename])) { return $this->loadedFilemtimes[$filename]; } $parent = $class->getParentClass(); $lastModification = max(array_merge( [$filename !== false && is_file($filename) ? filemtime($filename) : 0], array_map(function (ReflectionClass $reflectionTrait): int { return $this->getTraitLastModificationTime($reflectionTrait); }, $class->getTraits()), array_map(function (ReflectionClass $class): int { return $this->getLastModification($class); }, $class->getInterfaces()), $parent ? [$this->getLastModification($parent)] : [] )); assert($lastModification !== false); return $this->loadedFilemtimes[$filename] = $lastModification; } private function getTraitLastModificationTime(ReflectionClass $reflectionTrait): int { $fileName = $reflectionTrait->getFileName(); if (isset($this->loadedFilemtimes[$fileName])) { return $this->loadedFilemtimes[$fileName]; } $lastModificationTime = max(array_merge( [$fileName !== false && is_file($fileName) ? filemtime($fileName) : 0], array_map(function (ReflectionClass $reflectionTrait): int { return $this->getTraitLastModificationTime($reflectionTrait); }, $reflectionTrait->getTraits()) )); assert($lastModificationTime !== false); return $this->loadedFilemtimes[$fileName] = $lastModificationTime; } } ================================================ FILE: lib/Doctrine/Common/Annotations/Reader.php ================================================ An array of Annotations. */ public function getClassAnnotations(ReflectionClass $class); /** * Gets a class annotation. * * @param ReflectionClass $class The ReflectionClass of the class from which * the class annotations should be read. * @param class-string $annotationName The name of the annotation. * * @return T|null The Annotation or NULL, if the requested annotation does not exist. * * @template T */ public function getClassAnnotation(ReflectionClass $class, $annotationName); /** * Gets the annotations applied to a method. * * @param ReflectionMethod $method The ReflectionMethod of the method from which * the annotations should be read. * * @return array An array of Annotations. */ public function getMethodAnnotations(ReflectionMethod $method); /** * Gets a method annotation. * * @param ReflectionMethod $method The ReflectionMethod to read the annotations from. * @param class-string $annotationName The name of the annotation. * * @return T|null The Annotation or NULL, if the requested annotation does not exist. * * @template T */ public function getMethodAnnotation(ReflectionMethod $method, $annotationName); /** * Gets the annotations applied to a property. * * @param ReflectionProperty $property The ReflectionProperty of the property * from which the annotations should be read. * * @return array An array of Annotations. */ public function getPropertyAnnotations(ReflectionProperty $property); /** * Gets a property annotation. * * @param ReflectionProperty $property The ReflectionProperty to read the annotations from. * @param class-string $annotationName The name of the annotation. * * @return T|null The Annotation or NULL, if the requested annotation does not exist. * * @template T */ public function getPropertyAnnotation(ReflectionProperty $property, $annotationName); } ================================================ FILE: lib/Doctrine/Common/Annotations/TokenParser.php ================================================ */ private $tokens; /** * The number of tokens. * * @var int */ private $numTokens; /** * The current array pointer. * * @var int */ private $pointer = 0; public function __construct(string $contents) { $this->tokens = token_get_all($contents); // The PHP parser sets internal compiler globals for certain things. Annoyingly, the last docblock comment it // saw gets stored in doc_comment. When it comes to compile the next thing to be include()d this stored // doc_comment becomes owned by the first thing the compiler sees in the file that it considers might have a // docblock. If the first thing in the file is a class without a doc block this would cause calls to // getDocBlock() on said class to return our long lost doc_comment. Argh. // To workaround, cause the parser to parse an empty docblock. Sure getDocBlock() will return this, but at least // it's harmless to us. token_get_all("numTokens = count($this->tokens); } /** * Gets the next non whitespace and non comment token. * * @param bool $docCommentIsComment If TRUE then a doc comment is considered a comment and skipped. * If FALSE then only whitespace and normal comments are skipped. * * @return mixed[]|string|null The token if exists, null otherwise. */ public function next(bool $docCommentIsComment = true) { for ($i = $this->pointer; $i < $this->numTokens; $i++) { $this->pointer++; if ( $this->tokens[$i][0] === T_WHITESPACE || $this->tokens[$i][0] === T_COMMENT || ($docCommentIsComment && $this->tokens[$i][0] === T_DOC_COMMENT) ) { continue; } return $this->tokens[$i]; } return null; } /** * Parses a single use statement. * * @return array A list with all found class names for a use statement. */ public function parseUseStatement() { $groupRoot = ''; $class = ''; $alias = ''; $statements = []; $explicitAlias = false; while (($token = $this->next())) { if (! $explicitAlias && $token[0] === T_STRING) { $class .= $token[1]; $alias = $token[1]; } elseif ($explicitAlias && $token[0] === T_STRING) { $alias = $token[1]; } elseif ( PHP_VERSION_ID >= 80000 && ($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED) ) { $class .= $token[1]; $classSplit = explode('\\', $token[1]); $alias = $classSplit[count($classSplit) - 1]; } elseif ($token[0] === T_NS_SEPARATOR) { $class .= '\\'; $alias = ''; } elseif ($token[0] === T_AS) { $explicitAlias = true; $alias = ''; } elseif ($token === ',') { $statements[strtolower($alias)] = $groupRoot . $class; $class = ''; $alias = ''; $explicitAlias = false; } elseif ($token === ';') { $statements[strtolower($alias)] = $groupRoot . $class; break; } elseif ($token === '{') { $groupRoot = $class; $class = ''; } elseif ($token === '}') { continue; } else { break; } } return $statements; } /** * Gets all use statements. * * @param string $namespaceName The namespace name of the reflected class. * * @return array A list with all found use statements. */ public function parseUseStatements(string $namespaceName) { $statements = []; while (($token = $this->next())) { if ($token[0] === T_USE) { $statements = array_merge($statements, $this->parseUseStatement()); continue; } if ($token[0] !== T_NAMESPACE || $this->parseNamespace() !== $namespaceName) { continue; } // Get fresh array for new namespace. This is to prevent the parser to collect the use statements // for a previous namespace with the same name. This is the case if a namespace is defined twice // or if a namespace with the same name is commented out. $statements = []; } return $statements; } /** * Gets the namespace. * * @return string The found namespace. */ public function parseNamespace() { $name = ''; while ( ($token = $this->next()) && ($token[0] === T_STRING || $token[0] === T_NS_SEPARATOR || ( PHP_VERSION_ID >= 80000 && ($token[0] === T_NAME_QUALIFIED || $token[0] === T_NAME_FULLY_QUALIFIED) )) ) { $name .= $token[1]; } return $name; } /** * Gets the class name. * * @return string The found class name. */ public function parseClass() { // Namespaces and class names are tokenized the same: T_STRINGs // separated by T_NS_SEPARATOR so we can use one function to provide // both. return $this->parseNamespace(); } } ================================================ FILE: phpbench.json.dist ================================================ { "bootstrap": "tests/Doctrine/Performance/Common/bootstrap.php", "path": "tests/Doctrine/Performance/Common/Annotations" } ================================================ FILE: phpcs.xml.dist ================================================ lib tests */lib/Doctrine/Common/Annotations/DocParser.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/ClassNoNamespaceNoComment.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/SimpleAnnotationReaderTest.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */tests/Doctrine/Tests/Common/Annotations/Fixtures/* */lib/Doctrine/Common/Annotations/DocParser.php */tests/Doctrine/Tests/Common/Annotations/PhpParserTest.php */tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM141Test.php */tests/* */tests/* */tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM58Test.php */tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php */tests/Doctrine/Tests/Common/Annotations/PhpParserTest.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/NamespaceWithClosureDeclaration.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/ClassWithRequire.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/SingleClassLOC1000.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/NonNamespacedClass.php */tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM58Entity.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/ClassNoNamespaceNoComment.php */tests/Doctrine/Tests/Common/Annotations/TopLevelAnnotation.php */tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php */tests/Doctrine/Tests/Common/Annotations/DummyClass.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/ClassWithValidAnnotationTarget.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/GroupUseStatement.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/AnnotationWithVarType.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/EmptyInterface.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/InterfaceThatExtendsAnInterface.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/InterfaceThatExtendsAnInterface.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/InterfaceWithConstants.php */lib/Doctrine/Common/Annotations/ImplicitlyIgnoredAnnotationNames.php */tests/Doctrine/Tests/Common/Annotations/Fixtures/functions.php */tests ================================================ FILE: phpstan.neon ================================================ parameters: level: 3 phpVersion: 80500 paths: - lib - tests scanFiles: - tests/Doctrine/Tests/Common/Annotations/Fixtures/GlobalNamespacesPerFileWithClassAsFirst.php - tests/Doctrine/Tests/Common/Annotations/Fixtures/GlobalNamespacesPerFileWithClassAsLast.php - tests/Doctrine/Tests/Common/Annotations/Fixtures/NonNamespacedClass.php - tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM58Entity.php excludePaths: - tests/*/Fixtures/* - tests/Doctrine/Tests/Common/Annotations/ReservedKeywordsClasses.php - tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM58Entity.php - tests/Doctrine/Tests/DoctrineTestCase.php polluteScopeWithLoopInitialAssignments: true ignoreErrors: - '#Instantiated class Doctrine_Tests_Common_Annotations_Fixtures_ClassNoNamespaceNoComment not found#' - '#Property Doctrine\\Tests\\Common\\Annotations\\DummyClassNonAnnotationProblem::\$foo has unknown class#' - '#Call to an undefined static method PHPUnit\\Framework\\TestCase::expectExceptionMessageRegExp\(\)#' - identifier: phpDoc.parseError path: tests/Doctrine/Tests/Common/Annotations/AbstractReaderTest.php ================================================ FILE: phpunit.xml.dist ================================================ ./tests/Doctrine/ ./lib/Doctrine/ ================================================ FILE: psalm.xml ================================================ ================================================ FILE: tests/Doctrine/Performance/Common/Annotations/CachedReadPerformanceWithInMemoryBench.php ================================================ reader = new PsrCachedReader(new AnnotationReader(), new ArrayAdapter()); $this->method = new ReflectionMethod(Controller::class, 'helloAction'); } /** * @Revs(500) * @Iterations(5) */ public function bench(): void { $this->reader->getMethodAnnotations($this->method); } } ================================================ FILE: tests/Doctrine/Performance/Common/Annotations/DocLexerPerformanceBench.php ================================================ lexer = new DocLexer(); } /** * @Revs(500) * @Iterations(5) */ public function benchMethod(): void { $this->lexer->setInput($this->methodDocBlock); } /** * @Revs(500) * @Iterations(5) */ public function benchClass(): void { $this->lexer->setInput($this->classDocBlock); } } ================================================ FILE: tests/Doctrine/Performance/Common/Annotations/DocParserPerformanceBench.php ================================================ 'Annotations\Annotation\IgnorePhpDoc', 'ignoreannotation' => 'Annotations\Annotation\IgnoreAnnotation', 'route' => Route::class, 'template' => Template::class, '__NAMESPACE__' => 'Doctrine\Tests\Common\Annotations\Fixtures', ]; private const IGNORED = [ 'access', 'author', 'copyright', 'deprecated', 'example', 'ignore', 'internal', 'link', 'see', 'since', 'tutorial', 'version', 'package', 'subpackage', 'name', 'global', 'param', 'return', 'staticvar', 'static', 'var', 'throws', 'inheritdoc', ]; /** @var DocParser */ private $parser; public function initialize(): void { $this->parser = new DocParser(); $this->parser->setImports(self::IMPORTS); $this->parser->setIgnoredAnnotationNames(array_fill_keys(self::IGNORED, true)); $this->parser->setIgnoreNotImportedAnnotations(true); } /** * @Revs(200) * @Iterations(5) */ public function benchMethodParsing(): void { $this->parser->parse($this->methodDocBlock); } /** * @Revs(200) * @Iterations(5) */ public function benchClassParsing(): void { $this->parser->parse($this->classDocBlock); } } ================================================ FILE: tests/Doctrine/Performance/Common/Annotations/MethodInitializer.php ================================================ method = new ReflectionMethod(Controller::class, 'helloAction'); $this->methodDocBlock = $this->method->getDocComment(); $this->classDocBlock = $this->method->getDeclaringClass()->getDocComment(); } } ================================================ FILE: tests/Doctrine/Performance/Common/Annotations/PhpParserPerformanceWithShortCutBench.php ================================================ class = new ReflectionClass(NamespacedSingleClassLOC1000::class); $this->parser = new PhpParser(); } /** * @Revs(500) * @Iterations(5) */ public function bench(): void { $this->parser->parseUseStatements($this->class); } } ================================================ FILE: tests/Doctrine/Performance/Common/Annotations/PhpParserPerformanceWithoutShortCutBench.php ================================================ class = new ReflectionClass(SingleClassLOC1000::class); $this->parser = new PhpParser(); } /** * @Revs(500) * @Iterations(5) */ public function bench(): void { $this->parser->parseUseStatements($this->class); } } ================================================ FILE: tests/Doctrine/Performance/Common/Annotations/ReadPerformanceBench.php ================================================ reader = new AnnotationReader(); } /** * @Revs(500) * @Iterations(5) */ public function bench(): void { $this->reader->getMethodAnnotations($this->method); } } ================================================ FILE: tests/Doctrine/Performance/Common/bootstrap.php ================================================ expectException = false; } public function getReflectionClass(): ReflectionClass { return new ReflectionClass(DummyClass::class); } public function testAnnotations(): void { $class = $this->getReflectionClass(); $reader = $this->getReader(); self::assertCount(1, $reader->getClassAnnotations($class)); self::assertInstanceOf( $annotName = DummyAnnotation::class, $annot = $reader->getClassAnnotation($class, $annotName) ); self::assertEquals('hello', $annot->dummyValue); $field1Prop = $class->getProperty('field1'); $propAnnots = $reader->getPropertyAnnotations($field1Prop); self::assertCount(1, $propAnnots); self::assertInstanceOf($annotName, $annot = $reader->getPropertyAnnotation($field1Prop, $annotName)); self::assertEquals('fieldHello', $annot->dummyValue); $getField1Method = $class->getMethod('getField1'); $methodAnnots = $reader->getMethodAnnotations($getField1Method); self::assertCount(1, $methodAnnots); self::assertInstanceOf($annotName, $annot = $reader->getMethodAnnotation($getField1Method, $annotName)); self::assertEquals([1, 2, 'three'], $annot->value); $field2Prop = $class->getProperty('field2'); $propAnnots = $reader->getPropertyAnnotations($field2Prop); self::assertCount(1, $propAnnots); self::assertInstanceOf( $annotName = DummyJoinTable::class, $joinTableAnnot = $reader->getPropertyAnnotation($field2Prop, $annotName) ); self::assertCount(1, $joinTableAnnot->joinColumns); self::assertCount(1, $joinTableAnnot->inverseJoinColumns); self::assertInstanceOf(DummyJoinColumn::class, $joinTableAnnot->joinColumns[0]); self::assertInstanceOf(DummyJoinColumn::class, $joinTableAnnot->inverseJoinColumns[0]); self::assertEquals('col1', $joinTableAnnot->joinColumns[0]->name); self::assertEquals('col2', $joinTableAnnot->joinColumns[0]->referencedColumnName); self::assertEquals('col3', $joinTableAnnot->inverseJoinColumns[0]->name); self::assertEquals('col4', $joinTableAnnot->inverseJoinColumns[0]->referencedColumnName); $dummyAnnot = $reader->getMethodAnnotation($class->getMethod('getField1'), DummyAnnotation::class); self::assertEquals('', $dummyAnnot->dummyValue); self::assertEquals([1, 2, 'three'], $dummyAnnot->value); $dummyAnnot = $reader->getMethodAnnotation($class->getMethod('getField3'), DummyAnnotation::class); self::assertEquals('\d{4}-[01]\d-[0-3]\d [0-2]\d:[0-5]\d:[0-5]\d', $dummyAnnot->value); $dummyAnnot = $reader->getPropertyAnnotation($class->getProperty('field1'), DummyAnnotation::class); self::assertEquals('fieldHello', $dummyAnnot->dummyValue); $classAnnot = $reader->getClassAnnotation($class, DummyAnnotation::class); self::assertEquals('hello', $classAnnot->dummyValue); } public function testAnnotationsWithValidTargets(): void { $reader = $this->getReader(); $class = new ReflectionClass(Fixtures\ClassWithValidAnnotationTarget::class); self::assertCount(1, $reader->getClassAnnotations($class)); self::assertCount(1, $reader->getPropertyAnnotations($class->getProperty('foo'))); self::assertCount(1, $reader->getMethodAnnotations($class->getMethod('someFunction'))); self::assertCount(1, $reader->getPropertyAnnotations($class->getProperty('nested'))); } public function testAnnotationsWithVarType(): void { $reader = $this->getReader(); $class = new ReflectionClass(Fixtures\ClassWithAnnotationWithVarType::class); self::assertCount(1, $fooAnnot = $reader->getPropertyAnnotations($class->getProperty('foo'))); self::assertCount(1, $barAnnot = $reader->getMethodAnnotations($class->getMethod('bar'))); self::assertIsString($fooAnnot[0]->string); self::assertInstanceOf(Fixtures\AnnotationTargetAll::class, $barAnnot[0]->annotation); } public function testAtInDescription(): void { $reader = $this->getReader(); $class = new ReflectionClass(Fixtures\ClassWithAtInDescriptionAndAnnotation::class); self::assertCount(1, $fooAnnot = $reader->getPropertyAnnotations($class->getProperty('foo'))); self::assertCount(1, $barAnnot = $reader->getPropertyAnnotations($class->getProperty('bar'))); self::assertInstanceOf(Fixtures\AnnotationTargetPropertyMethod::class, $fooAnnot[0]); self::assertInstanceOf(Fixtures\AnnotationTargetPropertyMethod::class, $barAnnot[0]); } public function testClassWithWithDanglingComma(): void { $reader = $this->getReader(); $annots = $reader->getClassAnnotations(new ReflectionClass(DummyClassWithDanglingComma::class)); self::assertCount(1, $annots); } public function testClassWithInvalidAnnotationTargetAtClassDocBlock(): void { $reader = $this->getReader(); if ($this->expectException) { $this->expectException(AnnotationException::class); $this->expectExceptionMessage( '[Semantical Error] Annotation @AnnotationTargetPropertyMethod is not allowed to be declared on class' . ' Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtClass.' . ' You may only use this annotation on these code elements: METHOD, PROPERTY' ); } $reader->getClassAnnotations(new ReflectionClass(Fixtures\ClassWithInvalidAnnotationTargetAtClass::class)); } public function testClassWithWithInclude(): void { $reader = $this->getReader(); $annots = $reader->getClassAnnotations(new ReflectionClass(Fixtures\ClassWithRequire::class)); self::assertCount(1, $annots); } public function testClassWithInvalidAnnotationTargetAtPropertyDocBlock(): void { $reader = $this->getReader(); if ($this->expectException) { $this->expectException(AnnotationException::class); $this->expectExceptionMessage( '[Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on property' . ' Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$foo. ' . 'You may only use this annotation on these code elements: CLASS' ); } $reader->getPropertyAnnotations(new ReflectionProperty( Fixtures\ClassWithInvalidAnnotationTargetAtProperty::class, 'foo' )); } public function testClassWithInvalidNestedAnnotationTargetAtPropertyDocBlock(): void { $reader = $this->getReader(); if ($this->expectException) { $this->expectException(AnnotationException::class); $this->expectExceptionMessage( '[Semantical Error] Annotation @AnnotationTargetAnnotation is not allowed to be declared on property' . ' Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtProperty::$bar.' . ' You may only use this annotation on these code elements: ANNOTATION' ); } $reader->getPropertyAnnotations(new ReflectionProperty( Fixtures\ClassWithInvalidAnnotationTargetAtProperty::class, 'bar' )); } public function testClassWithInvalidAnnotationTargetAtMethodDocBlock(): void { $reader = $this->getReader(); if ($this->expectException) { $this->expectException(AnnotationException::class); $this->expectExceptionMessage( '[Semantical Error] Annotation @AnnotationTargetClass is not allowed to be declared on method' . ' Doctrine\Tests\Common\Annotations\Fixtures\ClassWithInvalidAnnotationTargetAtMethod' . '::functionName(). You may only use this annotation on these code elements: CLASS' ); } $reader->getMethodAnnotations(new ReflectionMethod( Fixtures\ClassWithInvalidAnnotationTargetAtMethod::class, 'functionName' )); } public function testClassWithAnnotationWithTargetSyntaxErrorAtClassDocBlock(): void { $reader = $this->getReader(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( "Expected namespace separator or identifier, got ')' at position 24" . ' in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.' ); $reader->getClassAnnotations(new ReflectionClass( Fixtures\ClassWithAnnotationWithTargetSyntaxError::class )); } public function testClassWithAnnotationWithTargetSyntaxErrorAtPropertyDocBlock(): void { $reader = $this->getReader(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( "Expected namespace separator or identifier, got ')' at position 24" . ' in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.' ); $reader->getPropertyAnnotations(new ReflectionProperty( Fixtures\ClassWithAnnotationWithTargetSyntaxError::class, 'foo' )); } public function testClassWithAnnotationWithTargetSyntaxErrorAtMethodDocBlock(): void { $reader = $this->getReader(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( "Expected namespace separator or identifier, got ')' at position 24" . ' in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.' ); $reader->getMethodAnnotations(new ReflectionMethod( Fixtures\ClassWithAnnotationWithTargetSyntaxError::class, 'bar' )); } public function testClassWithPropertyInvalidVarTypeError(): void { $reader = $this->getReader(); $class = new ReflectionClass(Fixtures\ClassWithAnnotationWithVarType::class); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( '[Type Error] Attribute "string" of @AnnotationWithVarType declared on property' . ' Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType::$invalidProperty' . ' expects a(n) string, but got integer.' ); $reader->getPropertyAnnotations($class->getProperty('invalidProperty')); } public function testClassWithMethodInvalidVarTypeError(): void { $reader = $this->getReader(); $class = new ReflectionClass(Fixtures\ClassWithAnnotationWithVarType::class); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( '[Type Error] Attribute "annotation" of @AnnotationWithVarType declared' . ' on method Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationWithVarType::invalidMethod()' . ' expects a(n) \Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll,' . ' but got an instance of Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation.' ); $reader->getMethodAnnotations($class->getMethod('invalidMethod')); } public function testClassSyntaxErrorContext(): void { $reader = $this->getReader(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( "Expected namespace separator or identifier, got ')' at position 18" . ' in class Doctrine\Tests\Common\Annotations\DummyClassSyntaxError.' ); $reader->getClassAnnotations(new ReflectionClass(DummyClassSyntaxError::class)); } public function testMethodSyntaxErrorContext(): void { $reader = $this->getReader(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( "Expected namespace separator or identifier, got ')' at position 18" . ' in method Doctrine\Tests\Common\Annotations\DummyClassMethodSyntaxError::foo().' ); $reader->getMethodAnnotations(new ReflectionMethod(DummyClassMethodSyntaxError::class, 'foo')); } public function testPropertySyntaxErrorContext(): void { $reader = $this->getReader(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( "Expected namespace separator or identifier, got ')'" . ' at position 36 in property Doctrine\Tests\Common\Annotations\DummyClassPropertySyntaxError::$foo.' ); $reader->getPropertyAnnotations(new ReflectionProperty(DummyClassPropertySyntaxError::class, 'foo')); } /** * @group regression */ public function testMultipleAnnotationsOnSameLine(): void { $reader = $this->getReader(); $annots = $reader->getPropertyAnnotations(new ReflectionProperty(DummyClass2::class, 'id')); self::assertCount(3, $annots); } public function testNonAnnotationProblem(): void { $reader = $this->getReader(); self::assertNotNull($annot = $reader->getPropertyAnnotation( new ReflectionProperty(DummyClassNonAnnotationProblem::class, 'foo'), $name = DummyAnnotation::class )); self::assertInstanceOf($name, $annot); } public function testIncludeIgnoreAnnotation(): void { $reader = $this->getReader(); $reader->getPropertyAnnotations(new ReflectionProperty(Fixtures\ClassWithIgnoreAnnotation::class, 'foo')); self::assertFalse(class_exists(Fixtures\IgnoreAnnotationClass::class, false)); } public function testImportWithConcreteAnnotation(): void { $reader = $this->getReader(); $property = new ReflectionProperty(TestImportWithConcreteAnnotation::class, 'field'); $annotations = $reader->getPropertyAnnotations($property); self::assertCount(1, $annotations); self::assertNotNull($reader->getPropertyAnnotation($property, DummyAnnotation::class)); } public function testImportWithInheritance(): void { $reader = $this->getReader(); $class = new TestParentClass(); $ref = new ReflectionClass($class); $childAnnotations = $reader->getPropertyAnnotations($ref->getProperty('child')); self::assertCount(1, $childAnnotations); self::assertInstanceOf(Foo\Name::class, reset($childAnnotations)); $parentAnnotations = $reader->getPropertyAnnotations($ref->getProperty('parent')); self::assertCount(1, $parentAnnotations); self::assertInstanceOf(Bar\Name::class, reset($parentAnnotations)); } public function testImportDetectsNotImportedAnnotation(): void { $reader = $this->getReader(); if ($this->expectException) { $this->expectException(AnnotationException::class); $this->expectExceptionMessage( 'The annotation "@NameFoo" in property' . ' Doctrine\Tests\Common\Annotations\TestAnnotationNotImportedClass::$field was never imported.' ); } $reader->getPropertyAnnotations(new ReflectionProperty(TestAnnotationNotImportedClass::class, 'field')); } public function testImportDetectsNonExistentAnnotation(): void { $reader = $this->getReader(); if ($this->expectException) { $this->expectException(AnnotationException::class); $this->expectExceptionMessage( 'The annotation "@Foo\Bar\Name" in property' . ' Doctrine\Tests\Common\Annotations\TestNonExistentAnnotationClass::$field was never imported.' ); } $reader->getPropertyAnnotations(new ReflectionProperty(TestNonExistentAnnotationClass::class, 'field')); } public function testTopLevelAnnotation(): void { $reader = $this->getReader(); $annotations = $reader->getPropertyAnnotations(new ReflectionProperty( TestTopLevelAnnotationClass::class, 'field' )); self::assertCount(1, $annotations); self::assertInstanceOf(TopLevelAnnotation::class, reset($annotations)); } public function testIgnoresAnnotationsNotPrefixedWithWhitespace(): void { $reader = $this->getReader(); $annotation = $reader->getClassAnnotation( new ReflectionClass(new TestIgnoresNonAnnotationsClass()), NameBar::class ); self::assertInstanceOf(NameBar::class, $annotation); } private static $testResetsPhpParserAfterUseRun = false; /** * When getUseStatements isn't available on ReflectionClass the PhpParser has to use token_get_all(). If that * happens various PHP compiler globals get set, and these can have seriously bad effects on the next file to be * parsed. * Notably the doc_comment compiler global can end up containing a docblock comment. The next file to be parsed * on an include() will have this docblock comment attached to the first thing in the file that the compiler * considers to own comments. If this is a class then any later calls to getDocComment() for that class will have * undesirable effects. *sigh* */ public function testResetsPhpParserAfterUse(): void { // If someone has already included our main test fixture this test is invalid. It's important that our require // causes this file to be parsed and compiled at a certain point. self::assertFalse(! self::$testResetsPhpParserAfterUseRun && class_exists( Doctrine_Tests_Common_Annotations_Fixtures_ClassNoNamespaceNoComment::class ), 'Test invalid if class has already been compiled'); self::$testResetsPhpParserAfterUseRun = true; $reader = $this->getReader(); // First make sure the annotation cache knows about the annotations we want to use. // If we don't do this then loading of annotations into the cache will cause the parser to get out of the bad // state we want to test. $class = new ReflectionClass(Fixtures\ClassWithValidAnnotationTarget::class); $reader->getClassAnnotations($class); // Now import an incredibly dull class which makes use of the same // class level annotation that the previous class does. $class = new ReflectionClass(Fixtures\ClassWithClassAnnotationOnly::class); $annotations = $reader->getClassAnnotations($class); // This include needs to be here since we need the PHP compiler to run over it as the next thing the PHP // parser sees since PhpParser called token_get_all() on the intro to ClassWithClassAnnotationOnly. // Our test class cannot be in a namespace (some versions of PHP reset the doc_comment compiler global when // you hit a namespace declaration), so cannot be autoloaded. require_once __DIR__ . '/Fixtures/ClassNoNamespaceNoComment.php'; // So, hopefully, if all has gone OK, our class with class annotations should actually have them. // If this fails then something is quite badly wrong elsewhere. // Note that if this happens before the require it can cause other PHP files to be included, resetting the // compiler global state, and invalidating this test case. self::assertNotEmpty($annotations); $annotations = $reader->getClassAnnotations(new ReflectionClass( new Doctrine_Tests_Common_Annotations_Fixtures_ClassNoNamespaceNoComment() )); // And if our workaround for this bug is OK, our class with no doc // comment should not have any class annotations. self::assertEmpty($annotations); } public function testErrorWhenInvalidAnnotationIsUsed(): void { $reader = $this->getReader(); $ref = new ReflectionClass(Fixtures\InvalidAnnotationUsageClass::class); if ($this->expectException) { $this->expectException(AnnotationException::class); $this->expectExceptionMessage( 'The class "Doctrine\Tests\Common\Annotations\Fixtures\NoAnnotation" is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the _class_ doc comment of' . ' "Doctrine\Tests\Common\Annotations\Fixtures\NoAnnotation". If it is indeed no annotation, then you need to add @IgnoreAnnotation("NoAnnotation") to the _class_ doc comment' . ' of class Doctrine\Tests\Common\Annotations\Fixtures\InvalidAnnotationUsageClass.' ); } $reader->getClassAnnotations($ref); } public function testInvalidAnnotationUsageButIgnoredClass(): void { $reader = $this->getReader(); $ref = new ReflectionClass(Fixtures\InvalidAnnotationUsageButIgnoredClass::class); $annots = $reader->getClassAnnotations($ref); self::assertCount(2, $annots); } /** * @group DDC-1660 * @group regression */ public function testInvalidAnnotationButIgnored(): void { $reader = $this->getReader(); $class = new ReflectionClass(Fixtures\ClassDDC1660::class); self::assertTrue(class_exists(Fixtures\Annotation\Version::class)); self::assertEmpty($reader->getClassAnnotations($class)); self::assertEmpty($reader->getMethodAnnotations($class->getMethod('bar'))); self::assertEmpty($reader->getPropertyAnnotations($class->getProperty('foo'))); } public function testAnnotationEnumeratorException(): void { $reader = $this->getReader(); $class = new ReflectionClass(Fixtures\ClassWithAnnotationEnum::class); self::assertCount(1, $bar = $reader->getMethodAnnotations($class->getMethod('bar'))); self::assertCount(1, $foo = $reader->getPropertyAnnotations($class->getProperty('foo'))); self::assertInstanceOf(Fixtures\AnnotationEnum::class, $bar[0]); self::assertInstanceOf(Fixtures\AnnotationEnum::class, $foo[0]); try { $reader->getPropertyAnnotations($class->getProperty('invalidProperty')); $this->fail(); } catch (AnnotationException $exc) { self::assertEquals( '[Enum Error] Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnum' . ' declared on property Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationEnum' . '::$invalidProperty accepts only [ONE, TWO, THREE], but got FOUR.', $exc->getMessage() ); } try { $reader->getMethodAnnotations($class->getMethod('invalidMethod')); $this->fail(); } catch (AnnotationException $exc) { self::assertEquals( '[Enum Error] Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnum' . ' declared on method Doctrine\Tests\Common\Annotations\Fixtures\ClassWithAnnotationEnum' . '::invalidMethod() accepts only [ONE, TWO, THREE], but got 5.', $exc->getMessage() ); } } /** * @group DCOM-106 */ public function testIgnoreFixMeAndUpperCaseToDo(): void { $reader = $this->getReader(); $ref = new ReflectionClass(DCOM106::class); self::assertEmpty($reader->getClassAnnotations($ref)); } public function testWillSkipAnnotationsContainingDashes(): void { self::assertEmpty( $this ->getReader() ->getClassAnnotations(new ReflectionClass( Fixtures\ClassWithInvalidAnnotationContainingDashes::class )) ); } public function testWillFailOnAnnotationConstantReferenceContainingDashes(): void { $reader = $this->getReader(); $reflection = new ReflectionClass(Fixtures\ClassWithAnnotationConstantReferenceWithDashes::class); $this->expectExceptionMessage( '[Syntax Error] Expected Doctrine\Common\Annotations\DocLexer::T_CLOSE_PARENTHESIS, got \'-\' at' . ' position 14 in class ' . Fixtures\ClassWithAnnotationConstantReferenceWithDashes::class . '.' ); $reader->getClassAnnotations($reflection); } abstract protected function getReader(): Reader; } /** * @parseAnnotation("var") */ class TestParseAnnotationClass { /** @var */ public $field; } /** * @NameBar * @author Johannes M. Schmitt */ class TestIgnoresNonAnnotationsClass { } class TestTopLevelAnnotationClass { /** * @var mixed * @\TopLevelAnnotation */ public $field; } class TestNonExistentAnnotationClass { /** * @var mixed * @Foo\Bar\Name */ public $field; } class TestAnnotationNotImportedClass { /** * @var mixed * @NameFoo */ public $field; } class TestChildClass { /** * @var mixed * @\Doctrine\Tests\Common\Annotations\Foo\Name(name = "foo") */ protected $child; } class TestParentClass extends TestChildClass { /** * @var mixed * @\Doctrine\Tests\Common\Annotations\Bar\Name(name = "bar") */ public $parent; } class TestImportWithConcreteAnnotation { /** * @var mixed * @DummyAnnotation(dummyValue = "bar") */ public $field; } /** * @ignoreAnnotation("var") */ class DummyClass2 { /** * @DummyId * @DummyColumn(type="integer") * @DummyGeneratedValue * @var int */ public $id; } /** @Annotation */ class NameBar extends Annotation { } /** @Annotation */ class DummyId extends Annotation { } /** @Annotation */ class DummyColumn extends Annotation { /** @var mixed */ public $type; } /** @Annotation */ class DummyGeneratedValue extends Annotation { } /** @Annotation */ class DummyAnnotation extends Annotation { /** @var mixed */ public $dummyValue; } /** * @Annotation */ class DummyAnnotationWithIgnoredAnnotation extends Annotation { /** @var mixed */ public $dummyValue; } /** @Annotation */ class DummyJoinColumn extends Annotation { /** @var mixed */ public $name; /** @var mixed */ public $referencedColumnName; } /** @Annotation */ class DummyJoinTable extends Annotation { /** @var mixed */ public $name; /** @var mixed */ public $joinColumns; /** @var mixed */ public $inverseJoinColumns; } /** * @DummyAnnotation(dummyValue = "bar",) */ class DummyClassWithDanglingComma { } /** * @DummyAnnotation(@) */ class DummyClassSyntaxError { } class DummyClassMethodSyntaxError { /** * @DummyAnnotation(@) */ public function foo(): void { } } class DummyClassPropertySyntaxError { /** * @var mixed * @DummyAnnotation(@) */ public $foo; } /** * @ignoreAnnotation({"since", "var"}) */ class DummyClassNonAnnotationProblem { /** * @DummyAnnotation * @var Test */ public $foo; } /** * @DummyAnnotation Foo bar */ class DummyClassWithEmail { } /** * @fixme public * @TODO */ class DCOM106 { } namespace Doctrine\Tests\Common\Annotations\Foo; use Doctrine\Common\Annotations\Annotation; /** @Annotation */ class Name extends Annotation { /** @var mixed */ public $name; } namespace Doctrine\Tests\Common\Annotations\Bar; use Doctrine\Common\Annotations\Annotation; /** @Annotation */ class Name extends Annotation { /** @var mixed */ public $name; } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Annotation/TargetTest.php ================================================ ['ALL']]); self::assertEquals(Target::TARGET_ALL, $target->targets); $target = new Target(['value' => ['METHOD', 'METHOD']]); self::assertEquals(Target::TARGET_METHOD, $target->targets); self::assertNotEquals(Target::TARGET_PROPERTY, $target->targets); $target = new Target(['value' => ['PROPERTY', 'METHOD']]); self::assertEquals(Target::TARGET_METHOD | Target::TARGET_PROPERTY, $target->targets); } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/AnnotationReaderTest.php ================================================ getReader(); $ref = new ReflectionClass(Fixtures\ClassUsesTrait::class); $annotations = $reader->getMethodAnnotations($ref->getMethod('someMethod')); self::assertInstanceOf(Bar\Autoload::class, $annotations[0]); $annotations = $reader->getMethodAnnotations($ref->getMethod('traitMethod')); self::assertInstanceOf(Fixtures\Annotation\Autoload::class, $annotations[0]); } public function testMethodAnnotationFromOverwrittenTrait(): void { $reader = $this->getReader(); $ref = new ReflectionClass(Fixtures\ClassOverwritesTrait::class); $annotations = $reader->getMethodAnnotations($ref->getMethod('traitMethod')); self::assertInstanceOf(Bar2\Autoload::class, $annotations[0]); } public function testPropertyAnnotationFromTrait(): void { $reader = $this->getReader(); $ref = new ReflectionClass(Fixtures\ClassUsesTrait::class); $annotations = $reader->getPropertyAnnotations($ref->getProperty('aProperty')); self::assertInstanceOf(Bar\Autoload::class, $annotations[0]); $annotations = $reader->getPropertyAnnotations($ref->getProperty('traitProperty')); self::assertInstanceOf(Fixtures\Annotation\Autoload::class, $annotations[0]); } public function testOmitNotRegisteredAnnotation(): void { $parser = new DocParser(); $parser->setIgnoreNotImportedAnnotations(true); $reader = $this->getReader($parser); $ref = new ReflectionClass(Fixtures\ClassWithNotRegisteredAnnotationUsed::class); $annotations = $reader->getMethodAnnotations($ref->getMethod('methodWithNotRegisteredAnnotation')); self::assertEquals([], $annotations); } public function testClassAnnotationSupportsSelfAccessorForConstants(): void { $reader = $this->getReader(); $ref = new ReflectionClass(Fixtures\ClassWithAnnotationWithSelfConstantReference::class); $annotations = $reader->getClassAnnotations($ref); self::assertCount(1, $annotations); $annotation = $annotations[0]; self::assertInstanceOf(Fixtures\AnnotationWithConstants::class, $annotation); self::assertEquals( $annotation->value, Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_CLASS ); } public function testPropertyAnnotationSupportsSelfAccessorForConstants(): void { $reader = $this->getReader(); $ref = new ReflectionClass(Fixtures\ClassWithAnnotationWithSelfConstantReference::class); $classProperty = $ref->getProperty('classProperty'); $classAnnotation = $reader->getPropertyAnnotation($classProperty, Fixtures\AnnotationWithConstants::class); self::assertNotNull($classAnnotation); self::assertEquals( $classAnnotation->value, Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_CLASS ); $traitProperty = $ref->getProperty('traitProperty'); $traitAnnotation = $reader->getPropertyAnnotation($traitProperty, Fixtures\AnnotationWithConstants::class); self::assertNotNull($traitAnnotation); self::assertEquals( $traitAnnotation->value, Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_TRAIT ); } public function testMethodAnnotationSupportsSelfAccessorForConstants(): void { $reader = $this->getReader(); $ref = new ReflectionClass(Fixtures\ClassWithAnnotationWithSelfConstantReference::class); $classMethod = $ref->getMethod('classMethod'); $classAnnotation = $reader->getMethodAnnotation($classMethod, Fixtures\AnnotationWithConstants::class); self::assertNotNull($classAnnotation); self::assertEquals( $classAnnotation->value, Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_CLASS ); $traitMethod = $ref->getMethod('traitMethod'); $traitAnnotation = $reader->getMethodAnnotation($traitMethod, Fixtures\AnnotationWithConstants::class); self::assertNotNull($traitAnnotation); self::assertEquals( $traitAnnotation->value, Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_TRAIT ); } /** * @group 45 * @runInSeparateProcess */ public function testClassAnnotationIsIgnored(): void { $reader = $this->getReader(); $ref = new ReflectionClass(AnnotatedAtClassLevel::class); $reader::addGlobalIgnoredNamespace('SomeClassAnnotationNamespace'); self::assertEmpty($reader->getClassAnnotations($ref)); } /** * @group 45 * @runInSeparateProcess */ public function testMethodAnnotationIsIgnored(): void { $reader = $this->getReader(); $ref = new ReflectionClass(AnnotatedAtMethodLevel::class); $reader::addGlobalIgnoredNamespace('SomeMethodAnnotationNamespace'); self::assertEmpty($reader->getMethodAnnotations($ref->getMethod('test'))); } /** * @group 45 * @runInSeparateProcess */ public function testPropertyAnnotationIsIgnored(): void { $reader = $this->getReader(); $ref = new ReflectionClass(AnnotatedAtPropertyLevel::class); $reader::addGlobalIgnoredNamespace('SomePropertyAnnotationNamespace'); self::assertEmpty($reader->getPropertyAnnotations($ref->getProperty('property'))); } /** * @group 244 * @runInSeparateProcess */ public function testAnnotationWithAliasIsIgnored(): void { $reader = $this->getReader(); $ref = new ReflectionClass(AnnotatedWithAlias::class); $reader::addGlobalIgnoredNamespace('SomePropertyAnnotationNamespace'); self::assertEmpty($reader->getPropertyAnnotations($ref->getProperty('property'))); } public function testClassWithFullPathUseStatement(): void { if (class_exists(SingleUseAnnotation::class, false)) { throw new LogicException( 'The SingleUseAnnotation must not be used in other tests for this test to be useful.' . 'If the class is already loaded then the code path that finds the class to load is not used and ' . 'this test becomes useless.' ); } $reader = $this->getReader(); $ref = new ReflectionClass(ClassWithFullPathUseStatement::class); $annotations = $reader->getClassAnnotations($ref); self::assertInstanceOf(SingleUseAnnotation::class, $annotations[0]); } public function testPhpCsSuppressAnnotationIsIgnored(): void { $reader = $this->getReader(); $ref = new ReflectionClass(ClassWithPhpCsSuppressAnnotation::class); self::assertEmpty($reader->getMethodAnnotations($ref->getMethod('foo'))); } public function testGloballyIgnoredAnnotationNotIgnored(): void { $reader = $this->getReader(); $class = new ReflectionClass(Fixtures\ClassDDC1660::class); $testLoader = static function (string $className): bool { if ($className === 'since') { throw new InvalidArgumentException( 'Globally ignored annotation names should never be passed to an autoloader.' ); } return false; }; spl_autoload_register($testLoader, true, true); try { self::assertEmpty($reader->getClassAnnotations($class)); } finally { spl_autoload_unregister($testLoader); } } public function testPHPCodeSnifferAnnotationsAreIgnored(): void { $reader = $this->getReader(); $ref = new ReflectionClass(ClassWithPHPCodeSnifferAnnotation::class); self::assertEmpty($reader->getClassAnnotations($ref)); } public function testPHPStanGenericsAnnotationsAreIgnored(): void { $reader = $this->getReader(); $ref = new ReflectionClass(ClassWithPHPStanGenericsAnnotations::class); self::assertEmpty($reader->getClassAnnotations($ref)); self::assertEmpty($reader->getPropertyAnnotations($ref->getProperty('bar'))); self::assertEmpty($reader->getMethodAnnotations($ref->getMethod('foo'))); $this->expectException('\Doctrine\Common\Annotations\AnnotationException'); $this->expectExceptionMessage( '[Semantical Error] The annotation "@Template" in method' . ' Doctrine\Tests\Common\Annotations\Fixtures\ClassWithPHPStanGenericsAnnotations' . '::twigTemplateFunctionName() was never imported.' ); self::assertEmpty($reader->getMethodAnnotations($ref->getMethod('twigTemplateFunctionName'))); } public function testImportedIgnoredAnnotationIsStillIgnored(): void { $reader = $this->getReader(); $ref = new ReflectionClass(ClassWithImportedIgnoredAnnotation::class); self::assertEmpty($reader->getMethodAnnotations($ref->getMethod('something'))); } public function testFunctionsAnnotation(): void { $reader = $this->getReader(); $ref = new ReflectionFunction('Doctrine\Tests\Common\Annotations\Fixtures\foo'); $annotations = $reader->getFunctionAnnotations($ref); self::assertCount(1, $annotations); self::assertInstanceOf(Fixtures\Annotation\Autoload::class, $annotations[0]); } public function testFunctionAnnotation(): void { $reader = $this->getReader(); $ref = new ReflectionFunction('Doctrine\Tests\Common\Annotations\Fixtures\foo'); $annotation = $reader->getFunctionAnnotation($ref, Fixtures\Annotation\Autoload::class); self::assertInstanceOf(Fixtures\Annotation\Autoload::class, $annotation); } /** * @requires PHP 8.1 * @dataProvider provideEnumProperties */ public function testAnnotationWithEnum(string $property, Suit $expectedValue): void { $reader = $this->getReader(); $ref = new ReflectionClass(ClassWithEnumAnnotations::class); $annotation = $reader->getPropertyAnnotation($ref->getProperty($property), AnnotationWithEnumProperty::class); self::assertSame($expectedValue, $annotation->suit); } /** * @return array */ public function provideEnumProperties(): array { if (PHP_VERSION_ID < 80100) { return []; } return [ 'annotationWithDefaults' => ['annotationWithDefaults', Suit::Hearts], 'annotationWithSpades' => ['annotationWithSpades', Suit::Spades], ]; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/AnnotationRegistryTest.php ================================================ expectException(BadMethodCallException::class); $this->expectExceptionMessage(sprintf( "Unknown property '%s' on annotation '%s'.", $name, Annotation::class )); // @phpstan-ignore-next-line This is expected not to exist $annotation->{$name}; } public function testMagicSetThrowsBadMethodCallException(): void { $name = 'foo'; $annotation = new Annotation([]); $this->expectException(BadMethodCallException::class); $this->expectExceptionMessage(sprintf( "Unknown property '%s' on annotation '%s'.", $name, Annotation::class )); // @phpstan-ignore-next-line This is expected not to exist $annotation->{$name} = 9001; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/DocLexerTest.php ================================================ setInput('@Name'); self::assertNull($lexer->token); self::assertNull($lexer->lookahead); self::assertTrue($lexer->moveNext()); self::assertNull($lexer->token); self::assertNotNull($lexer->lookahead); self::assertEquals('@', $lexer->lookahead->value); self::assertTrue($lexer->moveNext()); self::assertNotNull($lexer->token); self::assertEquals('@', $lexer->token->value); self::assertEquals('Name', $lexer->lookahead->value); self::assertFalse($lexer->moveNext()); } public function testScannerTokenizesDocBlockWhitConstants(): void { $lexer = new DocLexer(); $docblock = '@AnnotationWithConstants( PHP_EOL, ClassWithConstants::SOME_VALUE, ClassWithConstants::CONSTANT_, ClassWithConstants::CONST_ANT3, \Doctrine\Tests\Common\Annotations\Fixtures\InterfaceWithConstants::SOME_VALUE )'; $tokens = [ [ 'value' => '@', 'position' => 0, 'type' => DocLexer::T_AT, ], [ 'value' => 'AnnotationWithConstants', 'position' => 1, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => '(', 'position' => 24, 'type' => DocLexer::T_OPEN_PARENTHESIS, ], [ 'value' => 'PHP_EOL', 'position' => 38, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => ',', 'position' => 45, 'type' => DocLexer::T_COMMA, ], [ 'value' => 'ClassWithConstants::SOME_VALUE', 'position' => 59, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => ',', 'position' => 89, 'type' => DocLexer::T_COMMA, ], [ 'value' => 'ClassWithConstants::CONSTANT_', 'position' => 103, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => ',', 'position' => 132, 'type' => DocLexer::T_COMMA, ], [ 'value' => 'ClassWithConstants::CONST_ANT3', 'position' => 146, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => ',', 'position' => 176, 'type' => DocLexer::T_COMMA, ], [ 'value' => '\\Doctrine\\Tests\\Common\\Annotations\\Fixtures\\InterfaceWithConstants::SOME_VALUE', 'position' => 190, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => ')', 'position' => 277, 'type' => DocLexer::T_CLOSE_PARENTHESIS, ], ]; $lexer->setInput($docblock); foreach ($tokens as $expected) { $lexer->moveNext(); $lookahead = $lexer->lookahead; self::assertEquals($expected['value'], $lookahead->value); self::assertEquals($expected['type'], $lookahead->type); self::assertEquals($expected['position'], $lookahead->position); } self::assertFalse($lexer->moveNext()); } public function testScannerTokenizesDocBlockWhitInvalidIdentifier(): void { $lexer = new DocLexer(); $docblock = '@Foo\3.42'; $tokens = [ [ 'value' => '@', 'position' => 0, 'type' => DocLexer::T_AT, ], [ 'value' => 'Foo', 'position' => 1, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => '\\', 'position' => 4, 'type' => DocLexer::T_NAMESPACE_SEPARATOR, ], [ 'value' => 3.42, 'position' => 5, 'type' => DocLexer::T_FLOAT, ], ]; $lexer->setInput($docblock); foreach ($tokens as $expected) { $lexer->moveNext(); $lookahead = $lexer->lookahead; self::assertEquals($expected['value'], $lookahead->value); self::assertEquals($expected['type'], $lookahead->type); self::assertEquals($expected['position'], $lookahead->position); } self::assertFalse($lexer->moveNext()); } /** * @group 44 */ public function testWithinDoubleQuotesVeryVeryLongStringWillNotOverflowPregSplitStackLimit(): void { $lexer = new DocLexer(); $lexer->setInput('"' . str_repeat('.', 20240) . '"'); self::assertInstanceOf(Token::class, $lexer->glimpse()); } /** * @group 44 */ public function testRecognizesDoubleQuotesEscapeSequence(): void { $lexer = new DocLexer(); $docblock = '@Foo("""' . "\n" . '""")'; $tokens = [ [ 'value' => '@', 'position' => 0, 'type' => DocLexer::T_AT, ], [ 'value' => 'Foo', 'position' => 1, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => '(', 'position' => 4, 'type' => DocLexer::T_OPEN_PARENTHESIS, ], [ 'value' => "\"\n\"", 'position' => 5, 'type' => DocLexer::T_STRING, ], [ 'value' => ')', 'position' => 12, 'type' => DocLexer::T_CLOSE_PARENTHESIS, ], ]; $lexer->setInput($docblock); foreach ($tokens as $expected) { $lexer->moveNext(); $lookahead = $lexer->lookahead; self::assertEquals($expected['value'], $lookahead->value); self::assertEquals($expected['type'], $lookahead->type); self::assertEquals($expected['position'], $lookahead->position); } self::assertFalse($lexer->moveNext()); } public function testDoesNotRecognizeFullAnnotationWithDashInIt(): void { $this->expectDocblockTokens( '@foo-bar--', [ [ 'value' => '@', 'position' => 0, 'type' => DocLexer::T_AT, ], [ 'value' => 'foo', 'position' => 1, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => '-', 'position' => 4, 'type' => DocLexer::T_MINUS, ], [ 'value' => 'bar', 'position' => 5, 'type' => DocLexer::T_IDENTIFIER, ], [ 'value' => '-', 'position' => 8, 'type' => DocLexer::T_MINUS, ], [ 'value' => '-', 'position' => 9, 'type' => DocLexer::T_MINUS, ], ] ); } public function testRecognizesNegativeNumbers(): void { $this->expectDocblockTokens( '-12.34 -56', [ [ 'value' => '-12.34', 'position' => 0, 'type' => DocLexer::T_FLOAT, ], [ 'value' => '-56', 'position' => 7, 'type' => DocLexer::T_INTEGER, ], ] ); } /** * @phpstan-param list $expectedTokens */ private function expectDocblockTokens(string $docBlock, array $expectedTokens): void { $lexer = new DocLexer(); $lexer->setInput($docBlock); $actualTokens = []; while ($lexer->moveNext()) { $lookahead = $lexer->lookahead; $actualTokens[] = [ 'value' => $lookahead->value, 'type' => $lookahead->type, 'position' => $lookahead->position, ]; } self::assertEquals($expectedTokens, $actualTokens); } public function testTokenAdjacency(): void { $lexer = new DocLexer(); $lexer->setInput('-- -'); self::assertTrue($lexer->nextTokenIsAdjacent()); self::assertTrue($lexer->moveNext()); self::assertTrue($lexer->nextTokenIsAdjacent()); self::assertTrue($lexer->moveNext()); self::assertTrue($lexer->nextTokenIsAdjacent()); self::assertTrue($lexer->moveNext()); self::assertFalse($lexer->nextTokenIsAdjacent()); self::assertFalse($lexer->moveNext()); } public function testItReturnsNullWhenThereIsNothingToParse(): void { self::assertNull((new DocLexer())->peek()); } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/DocParserTest.php ================================================ createTestParser(); // Nested arrays with nested annotations $result = $parser->parse('@Name(foo={1,2, {"key"=@Name}})'); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertNull($annot->value); self::assertCount(3, $annot->foo); self::assertEquals(1, $annot->foo[0]); self::assertEquals(2, $annot->foo[1]); self::assertIsArray($annot->foo[2]); $nestedArray = $annot->foo[2]; self::assertTrue(isset($nestedArray['key'])); self::assertInstanceOf(Name::class, $nestedArray['key']); } public function testBasicAnnotations(): void { $parser = $this->createTestParser(); // Marker annotation $result = $parser->parse('@Name'); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertNull($annot->value); self::assertNull($annot->foo); // Associative arrays $result = $parser->parse('@Name(foo={"key1" = "value1"})'); $annot = $result[0]; self::assertNull($annot->value); self::assertIsArray($annot->foo); self::assertTrue(isset($annot->foo['key1'])); // Numerical arrays $result = $parser->parse('@Name({2="foo", 4="bar"})'); $annot = $result[0]; self::assertIsArray($annot->value); self::assertEquals('foo', $annot->value[2]); self::assertEquals('bar', $annot->value[4]); self::assertFalse(isset($annot->value[0])); self::assertFalse(isset($annot->value[1])); self::assertFalse(isset($annot->value[3])); // Multiple values $result = $parser->parse('@Name(@Name, @Name)'); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertIsArray($annot->value); self::assertInstanceOf(Name::class, $annot->value[0]); self::assertInstanceOf(Name::class, $annot->value[1]); // Positionals arguments following named arguments $result = $parser->parse('@Name(foo="bar", @Name)'); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertEquals('bar', $annot->foo); self::assertInstanceOf(Name::class, $annot->value); // Multiple positionals arguments following named arguments $result = $parser->parse('@Name(@Name, foo="baz", @Name)'); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertEquals('baz', $annot->foo); self::assertIsArray($annot->value); self::assertInstanceOf(Name::class, $annot->value[0]); self::assertInstanceOf(Name::class, $annot->value[1]); // Multiple scalar values $result = $parser->parse('@Name("foo", "bar")'); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertIsArray($annot->value); self::assertEquals('foo', $annot->value[0]); self::assertEquals('bar', $annot->value[1]); // Multiple types as values $result = $parser->parse('@Name(foo="Bar", @Name, {"key1"="value1", "key2"="value2"})'); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertIsArray($annot->value); self::assertInstanceOf(Name::class, $annot->value[0]); self::assertIsArray($annot->value[1]); self::assertEquals('value1', $annot->value[1]['key1']); self::assertEquals('value2', $annot->value[1]['key2']); // Complete docblock $docblock = <<<'DOCBLOCK' /** * Some nifty class. * * @author Mr.X * @Name(foo="bar") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertEquals('bar', $annot->foo); self::assertNull($annot->value); } public function testDefaultValueAnnotations(): void { $parser = $this->createTestParser(); // Array as first value $result = $parser->parse('@Name({"key1"="value1"})'); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertIsArray($annot->value); self::assertEquals('value1', $annot->value['key1']); // Array as first value and additional values $result = $parser->parse('@Name({"key1"="value1"}, foo="bar")'); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertIsArray($annot->value); self::assertEquals('value1', $annot->value['key1']); self::assertEquals('bar', $annot->foo); } public function testNamespacedAnnotations(): void { $parser = new DocParser(); $parser->setIgnoreNotImportedAnnotations(true); $docblock = <<<'DOCBLOCK' /** * Some nifty class. * * @package foo * @subpackage bar * @author Mr.X * @Doctrine\Tests\Common\Annotations\Name(foo="bar") * @ignore */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertEquals('bar', $annot->foo); } /** * @group debug */ public function testTypicalMethodDocBlock(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * Some nifty method. * * @since 2.0 * @Doctrine\Tests\Common\Annotations\Name(foo="bar") * @param string $foo This is foo. * @param mixed $bar This is bar. * @return string Foo and bar. * @This is irrelevant * @Marker */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(2, $result); self::assertTrue(isset($result[0])); self::assertTrue(isset($result[1])); $annot = $result[0]; self::assertInstanceOf(Name::class, $annot); self::assertEquals('bar', $annot->foo); $marker = $result[1]; self::assertInstanceOf(Marker::class, $marker); } public function testAnnotationWithoutConstructor(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructor("Some data") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertInstanceOf(SomeAnnotationClassNameWithoutConstructor::class, $annot); self::assertNull($annot->name); self::assertNotNull($annot->data); self::assertEquals($annot->data, 'Some data'); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructor(name="Some Name", data = "Some data") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertNotNull($annot); self::assertInstanceOf(SomeAnnotationClassNameWithoutConstructor::class, $annot); self::assertEquals($annot->name, 'Some Name'); self::assertEquals($annot->data, 'Some data'); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructor(data = "Some data") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertEquals($annot->data, 'Some data'); self::assertNull($annot->name); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructor(name = "Some name") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertEquals($annot->name, 'Some name'); self::assertNull($annot->data); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructor("Some data") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertEquals($annot->data, 'Some data'); self::assertNull($annot->name); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructor("Some data",name = "Some name") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertEquals($annot->name, 'Some name'); self::assertEquals($annot->data, 'Some data'); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationWithConstructorWithoutParams(name = "Some name") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertEquals($annot->name, 'Some name'); self::assertEquals($annot->data, 'Some data'); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructorAndProperties() */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); self::assertInstanceOf(SomeAnnotationClassNameWithoutConstructorAndProperties::class, $result[0]); } public function testAnnotationTarget(): void { $parser = new DocParser(); $parser->setImports(['__NAMESPACE__' => 'Doctrine\Tests\Common\Annotations\Fixtures']); $class = new ReflectionClass(Fixtures\ClassWithValidAnnotationTarget::class); $context = 'class ' . $class->getName(); $docComment = $class->getDocComment(); $parser->setTarget(Target::TARGET_CLASS); self::assertNotNull($parser->parse($docComment, $context)); $property = $class->getProperty('foo'); $docComment = $property->getDocComment(); $context = 'property ' . $class->getName() . '::$' . $property->getName(); $parser->setTarget(Target::TARGET_PROPERTY); self::assertNotNull($parser->parse($docComment, $context)); $method = $class->getMethod('someFunction'); $docComment = $property->getDocComment(); $context = 'method ' . $class->getName() . '::' . $method->getName() . '()'; $parser->setTarget(Target::TARGET_METHOD); self::assertNotNull($parser->parse($docComment, $context)); try { $class = new ReflectionClass(Fixtures\ClassWithInvalidAnnotationTargetAtClass::class); $context = 'class ' . $class->getName(); $docComment = $class->getDocComment(); $parser->setTarget(Target::TARGET_CLASS); $parser->parse($docComment, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertNotNull($exc->getMessage()); } try { $class = new ReflectionClass(Fixtures\ClassWithInvalidAnnotationTargetAtMethod::class); $method = $class->getMethod('functionName'); $docComment = $method->getDocComment(); $context = 'method ' . $class->getName() . '::' . $method->getName() . '()'; $parser->setTarget(Target::TARGET_METHOD); $parser->parse($docComment, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertNotNull($exc->getMessage()); } try { $class = new ReflectionClass(Fixtures\ClassWithInvalidAnnotationTargetAtProperty::class); $property = $class->getProperty('foo'); $docComment = $property->getDocComment(); $context = 'property ' . $class->getName() . '::$' . $property->getName(); $parser->setTarget(Target::TARGET_PROPERTY); $parser->parse($docComment, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertNotNull($exc->getMessage()); } } /** * @phpstan-return list */ public function getAnnotationVarTypeProviderValid() { //({attribute name}, {attribute value}) return [ // mixed type ['mixed', '"String Value"'], ['mixed', 'true'], ['mixed', 'false'], ['mixed', '1'], ['mixed', '1.2'], ['mixed', '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll'], // boolean type ['boolean', 'true'], ['boolean', 'false'], // alias for internal type boolean ['bool', 'true'], ['bool', 'false'], // integer type ['integer', '0'], ['integer', '1'], ['integer', '123456789'], ['integer', '9223372036854775807'], // alias for internal type double ['float', '0.1'], ['float', '1.2'], ['float', '123.456'], // string type ['string', '"String Value"'], ['string', '"true"'], ['string', '"123"'], // array type ['array', '{@AnnotationExtendsAnnotationTargetAll}'], ['array', '{@AnnotationExtendsAnnotationTargetAll,@AnnotationExtendsAnnotationTargetAll}'], ['arrayOfIntegers', '1'], ['arrayOfIntegers', '{1}'], ['arrayOfIntegers', '{1,2,3,4}'], ['arrayOfAnnotations', '@AnnotationExtendsAnnotationTargetAll'], ['arrayOfAnnotations', '{@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll}'], [ 'arrayOfAnnotations', '{ @AnnotationExtendsAnnotationTargetAll, @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll }', ], // annotation instance ['annotation', '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll'], ['annotation', '@AnnotationExtendsAnnotationTargetAll'], ]; } /** * @phpstan-return list */ public function getAnnotationVarTypeProviderInvalid(): array { //({attribute name}, {type declared type}, {attribute value} , {given type or class}) return [ // boolean type ['boolean','boolean','1','integer'], ['boolean','boolean','1.2','double'], ['boolean','boolean','"str"','string'], ['boolean','boolean','{1,2,3}','array'], ['boolean','boolean','@Name', 'an instance of Doctrine\Tests\Common\Annotations\Name'], // alias for internal type boolean ['bool','bool', '1','integer'], ['bool','bool', '1.2','double'], ['bool','bool', '"str"','string'], ['bool','bool', '{"str"}','array'], // integer type ['integer','integer', 'true','boolean'], ['integer','integer', 'false','boolean'], ['integer','integer', '1.2','double'], ['integer','integer', '"str"','string'], ['integer','integer', '{"str"}','array'], ['integer','integer', '{1,2,3,4}','array'], // alias for internal type double ['float','float', 'true','boolean'], ['float','float', 'false','boolean'], ['float','float', '123','integer'], ['float','float', '"str"','string'], ['float','float', '{"str"}','array'], ['float','float', '{12.34}','array'], ['float','float', '{1,2,3}','array'], // string type ['string','string', 'true','boolean'], ['string','string', 'false','boolean'], ['string','string', '12','integer'], ['string','string', '1.2','double'], ['string','string', '{"str"}','array'], ['string','string', '{1,2,3,4}','array'], // annotation instance ['annotation', AnnotationTargetAll::class, 'true','boolean'], ['annotation', AnnotationTargetAll::class, 'false','boolean'], ['annotation', AnnotationTargetAll::class, '12','integer'], ['annotation', AnnotationTargetAll::class, '1.2','double'], ['annotation', AnnotationTargetAll::class, '{"str"}','array'], ['annotation', AnnotationTargetAll::class, '{1,2,3,4}','array'], [ 'annotation', AnnotationTargetAll::class, '@Name', 'an instance of Doctrine\Tests\Common\Annotations\Name', ], ]; } /** * @phpstan-return list */ public function getAnnotationVarTypeArrayProviderInvalid() { //({attribute name}, {type declared type}, {attribute value} , {given type or class}) return [ ['arrayOfIntegers', 'integer', 'true', 'boolean'], ['arrayOfIntegers', 'integer', 'false', 'boolean'], ['arrayOfIntegers', 'integer', '{true,true}', 'boolean'], ['arrayOfIntegers', 'integer', '{1,true}', 'boolean'], ['arrayOfIntegers', 'integer', '{1,2,1.2}', 'double'], ['arrayOfIntegers', 'integer', '{1,2,"str"}', 'string'], ['arrayOfStrings', 'string', 'true', 'boolean'], ['arrayOfStrings', 'string', 'false', 'boolean'], ['arrayOfStrings', 'string', '{true,true}', 'boolean'], ['arrayOfStrings', 'string', '{"foo",true}', 'boolean'], ['arrayOfStrings', 'string', '{"foo","bar",1.2}', 'double'], ['arrayOfStrings', 'string', '1', 'integer'], ['arrayOfAnnotations', AnnotationTargetAll::class, 'true', 'boolean'], ['arrayOfAnnotations', AnnotationTargetAll::class, 'false', 'boolean'], [ 'arrayOfAnnotations', AnnotationTargetAll::class, '{@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll,true}', 'boolean', ], [ 'arrayOfAnnotations', AnnotationTargetAll::class, '{@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll,true}', 'boolean', ], [ 'arrayOfAnnotations', AnnotationTargetAll::class, '{@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll,1.2}', 'double', ], [ 'arrayOfAnnotations', AnnotationTargetAll::class, '{ @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll, @AnnotationExtendsAnnotationTargetAll, "str" }', 'string', ], ]; } /** * @dataProvider getAnnotationVarTypeProviderValid */ public function testAnnotationWithVarType(string $attribute, string $value): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::$invalidProperty.'; $docblock = sprintf( '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType(%s = %s)', $attribute, $value ); $parser->setTarget(Target::TARGET_PROPERTY); $result = $parser->parse($docblock, $context); self::assertCount(1, $result); self::assertInstanceOf(Fixtures\AnnotationWithVarType::class, $result[0]); self::assertNotNull($result[0]->$attribute); } /** * @dataProvider getAnnotationVarTypeProviderInvalid */ public function testAnnotationWithVarTypeError( string $attribute, string $type, string $value, string $given ): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::invalidProperty.'; $docblock = sprintf( '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType(%s = %s)', $attribute, $value ); $parser->setTarget(Target::TARGET_PROPERTY); try { $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertStringMatchesFormat( '[Type Error] Attribute "' . $attribute . '" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType' . ' declared on property SomeClassName::invalidProperty. expects a(n) %A' . $type . ', but got ' . $given . '.', $exc->getMessage() ); } } /** * @dataProvider getAnnotationVarTypeArrayProviderInvalid */ public function testAnnotationWithVarTypeArrayError( string $attribute, string $type, string $value, string $given ): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::invalidProperty.'; $docblock = sprintf( '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType(%s = %s)', $attribute, $value ); $parser->setTarget(Target::TARGET_PROPERTY); try { $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertStringMatchesFormat( '[Type Error] Attribute "' . $attribute . '" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithVarType' . ' declared on property SomeClassName::invalidProperty. expects either a(n) %A' . $type . ', or an array of %A' . $type . 's, but got ' . $given . '.', $exc->getMessage() ); } } /** * @dataProvider getAnnotationVarTypeProviderValid */ public function testAnnotationWithAttributes(string $attribute, string $value): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::$invalidProperty.'; $docblock = sprintf( '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes(%s = %s)', $attribute, $value ); $parser->setTarget(Target::TARGET_PROPERTY); $result = $parser->parse($docblock, $context); self::assertCount(1, $result); self::assertInstanceOf(Fixtures\AnnotationWithAttributes::class, $result[0]); $getter = 'get' . ucfirst($attribute); self::assertNotNull($result[0]->$getter()); } /** * @dataProvider getAnnotationVarTypeProviderInvalid */ public function testAnnotationWithAttributesError( string $attribute, string $type, string $value, string $given ): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::invalidProperty.'; $docblock = sprintf( '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes(%s = %s)', $attribute, $value ); $parser->setTarget(Target::TARGET_PROPERTY); try { $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertStringContainsString(sprintf( '[Type Error] Attribute "%s" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes' . ' declared on property SomeClassName::invalidProperty. expects a(n) %s, but got %s.', $attribute, $type, $given ), $exc->getMessage()); } } /** * @dataProvider getAnnotationVarTypeArrayProviderInvalid */ public function testAnnotationWithAttributesWithVarTypeArrayError( string $attribute, string $type, string $value, string $given ): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::invalidProperty.'; $docblock = sprintf( '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes(%s = %s)', $attribute, $value ); $parser->setTarget(Target::TARGET_PROPERTY); try { $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertStringContainsString(sprintf( '[Type Error] Attribute "%s" of' . ' @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithAttributes declared' . ' on property SomeClassName::invalidProperty. expects either a(n) %s, or an array of %ss, but got %s.', $attribute, $type, $type, $given ), $exc->getMessage()); } } public function testAnnotationWithRequiredAttributes(): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::invalidProperty.'; $parser->setTarget(Target::TARGET_PROPERTY); $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes' . '("Some Value", annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)'; $result = $parser->parse($docblock); self::assertCount(1, $result); $annotation = $result[0]; assert($annotation instanceof Fixtures\AnnotationWithRequiredAttributes); self::assertInstanceOf(Fixtures\AnnotationWithRequiredAttributes::class, $annotation); self::assertEquals('Some Value', $annotation->getValue()); self::assertInstanceOf(Fixtures\AnnotationTargetAnnotation::class, $annotation->getAnnot()); $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes("Some Value")'; try { $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertStringContainsString( 'Attribute "annot" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes' . ' declared on property SomeClassName::invalidProperty. expects a(n)' . ' Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation.' . ' This value should not be null.', $exc->getMessage() ); } $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes' . '(annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)'; try { $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertStringContainsString( 'Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributes' . ' declared on property SomeClassName::invalidProperty. expects a(n) string.' . ' This value should not be null.', $exc->getMessage() ); } } public function testAnnotationWithRequiredAttributesWithoutConstructor(): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::invalidProperty.'; $parser->setTarget(Target::TARGET_PROPERTY); $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor' . '("Some Value", annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)'; $result = $parser->parse($docblock); self::assertCount(1, $result); self::assertInstanceOf(Fixtures\AnnotationWithRequiredAttributesWithoutConstructor::class, $result[0]); self::assertEquals('Some Value', $result[0]->value); self::assertInstanceOf(Fixtures\AnnotationTargetAnnotation::class, $result[0]->annot); $docblock = <<<'ANNOTATION' @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor("Some Value") ANNOTATION; try { $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertStringContainsString( 'Attribute "annot" of' . ' @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor' . ' declared on property SomeClassName::invalidProperty. expects a(n)' . ' \Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation.' . ' This value should not be null.', $exc->getMessage() ); } $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor' . '(annot = @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAnnotation)'; try { $parser->parse($docblock, $context); $this->fail(); } catch (AnnotationException $exc) { self::assertStringContainsString( 'Attribute "value" of' . ' @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithRequiredAttributesWithoutConstructor' . ' declared on property SomeClassName::invalidProperty. expects a(n) string.' . ' This value should not be null.', $exc->getMessage() ); } } public function testAnnotationEnumeratorException(): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::invalidProperty.'; $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnum("FOUR")'; $parser->setIgnoreNotImportedAnnotations(false); $parser->setTarget(Target::TARGET_PROPERTY); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( 'Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnum declared' . ' on property SomeClassName::invalidProperty. accepts only [ONE, TWO, THREE], but got FOUR.' ); $parser->parse($docblock, $context); } public function testAnnotationEnumeratorLiteralException(): void { $parser = $this->createTestParser(); $context = 'property SomeClassName::invalidProperty.'; $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteral(4)'; $parser->setIgnoreNotImportedAnnotations(false); $parser->setTarget(Target::TARGET_PROPERTY); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( 'Attribute "value" of @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteral declared' . ' on property SomeClassName::invalidProperty. accepts only' . ' [AnnotationEnumLiteral::ONE, AnnotationEnumLiteral::TWO, AnnotationEnumLiteral::THREE], but got 4.' ); $parser->parse($docblock, $context); } public function testAnnotationEnumInvalidTypeDeclarationException(): void { $parser = $this->createTestParser(); $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumInvalid("foo")'; $parser->setIgnoreNotImportedAnnotations(false); $this->expectException(AnnotationException::class); try { $parser->parse($docblock); } catch (AnnotationException $exc) { $previous = $exc->getPrevious(); $this->assertInstanceOf(InvalidArgumentException::class, $previous); $this->assertThat($previous, new ExceptionMessage('@Enum supports only scalar values "array" given.')); throw $exc; } } public function testAnnotationEnumInvalidLiteralDeclarationException(): void { $parser = $this->createTestParser(); $docblock = '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationEnumLiteralInvalid("foo")'; $parser->setIgnoreNotImportedAnnotations(false); $this->expectException(AnnotationException::class); try { $parser->parse($docblock); } catch (AnnotationException $exc) { $previous = $exc->getPrevious(); $this->assertInstanceOf(InvalidArgumentException::class, $previous); $this->assertThat( $previous, new ExceptionMessage('Undefined enumerator value "3" for literal "AnnotationEnumLiteral::THREE".') ); throw $exc; } } /** * @phpstan-return array */ public function getConstantsProvider(): array { $provider = []; $provider[] = [ '@AnnotationWithConstants(PHP_EOL)', PHP_EOL, ]; $provider[] = [ '@AnnotationWithConstants(AnnotationWithConstants::INTEGER)', AnnotationWithConstants::INTEGER, ]; $provider[] = [ '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants(AnnotationWithConstants::STRING)', AnnotationWithConstants::STRING, ]; $provider[] = [ '@AnnotationWithConstants(Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants::FLOAT)', AnnotationWithConstants::FLOAT, ]; $provider[] = [ '@AnnotationWithConstants(ClassWithConstants::SOME_VALUE)', ClassWithConstants::SOME_VALUE, ]; $provider[] = [ '@AnnotationWithConstants(ClassWithConstants::OTHER_KEY_)', ClassWithConstants::OTHER_KEY_, ]; $provider[] = [ '@AnnotationWithConstants(ClassWithConstants::OTHER_KEY_2)', ClassWithConstants::OTHER_KEY_2, ]; $provider[] = [ '@AnnotationWithConstants(Doctrine\Tests\Common\Annotations\Fixtures\ClassWithConstants::SOME_VALUE)', ClassWithConstants::SOME_VALUE, ]; $provider[] = [ '@AnnotationWithConstants(InterfaceWithConstants::SOME_VALUE)', InterfaceWithConstants::SOME_VALUE, ]; $provider[] = [ '@AnnotationWithConstants(\Doctrine\Tests\Common\Annotations\Fixtures\InterfaceWithConstants::SOME_VALUE)', InterfaceWithConstants::SOME_VALUE, ]; $provider[] = [<<<'ANNOTATION' @AnnotationWithConstants({ AnnotationWithConstants::STRING, AnnotationWithConstants::INTEGER, AnnotationWithConstants::FLOAT }) ANNOTATION , [AnnotationWithConstants::STRING, AnnotationWithConstants::INTEGER, AnnotationWithConstants::FLOAT], ]; $provider[] = [ '@AnnotationWithConstants({ AnnotationWithConstants::STRING = AnnotationWithConstants::INTEGER })', [AnnotationWithConstants::STRING => AnnotationWithConstants::INTEGER], ]; $provider[] = [<<<'ANNOTATION' @AnnotationWithConstants({ Doctrine\Tests\Common\Annotations\Fixtures\InterfaceWithConstants::SOME_KEY = AnnotationWithConstants::INTEGER }) ANNOTATION , [InterfaceWithConstants::SOME_KEY => AnnotationWithConstants::INTEGER], ]; $provider[] = [<<<'ANNOTATION' @AnnotationWithConstants({ \Doctrine\Tests\Common\Annotations\Fixtures\InterfaceWithConstants::SOME_KEY = AnnotationWithConstants::INTEGER }) ANNOTATION , [InterfaceWithConstants::SOME_KEY => AnnotationWithConstants::INTEGER], ]; $provider[] = [<<<'ANNOTATION' @AnnotationWithConstants({ AnnotationWithConstants::STRING = AnnotationWithConstants::INTEGER, ClassWithConstants::SOME_KEY = ClassWithConstants::SOME_VALUE, Doctrine\Tests\Common\Annotations\Fixtures\InterfaceWithConstants::SOME_KEY = InterfaceWithConstants::SOME_VALUE }) ANNOTATION , [ AnnotationWithConstants::STRING => AnnotationWithConstants::INTEGER, ClassWithConstants::SOME_KEY => ClassWithConstants::SOME_VALUE, InterfaceWithConstants::SOME_KEY => InterfaceWithConstants::SOME_VALUE, ], ]; $provider[] = [ '@AnnotationWithConstants(AnnotationWithConstants::class)', AnnotationWithConstants::class, ]; $provider[] = [ '@AnnotationWithConstants({AnnotationWithConstants::class = AnnotationWithConstants::class})', [AnnotationWithConstants::class => AnnotationWithConstants::class], ]; $provider[] = [ '@AnnotationWithConstants(Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants::class)', AnnotationWithConstants::class, ]; $provider[] = [ '@Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants' . '(Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants::class)', AnnotationWithConstants::class, ]; return array_combine(array_column($provider, 0), $provider); } /** * @param mixed $expected * * @dataProvider getConstantsProvider */ public function testSupportClassConstants(string $docblock, $expected): void { $parser = $this->createTestParser(); $parser->setImports([ 'classwithconstants' => ClassWithConstants::class, 'interfacewithconstants' => InterfaceWithConstants::class, 'annotationwithconstants' => AnnotationWithConstants::class, ]); $result = $parser->parse($docblock); self::assertInstanceOf(AnnotationWithConstants::class, $annotation = $result[0]); self::assertEquals($expected, $annotation->value); } public function testWithoutConstructorWhenIsNotDefaultValue(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructorAndProperties("Foo") */ DOCBLOCK; $parser->setTarget(Target::TARGET_CLASS); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( 'The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on ' . ' does not accept any values, but got {"value":"Foo"}.' ); $parser->parse($docblock); } public function testWithoutConstructorWhenHasNoProperties(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructorAndProperties(value = "Foo") */ DOCBLOCK; $parser->setTarget(Target::TARGET_CLASS); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( 'The annotation @SomeAnnotationClassNameWithoutConstructorAndProperties declared on ' . ' does not accept any values, but got {"value":"Foo"}.' ); $parser->parse($docblock); } public function testAnnotationTargetSyntaxError(): void { $parser = $this->createTestParser(); $context = 'class SomeClassName'; $docblock = <<<'DOCBLOCK' /** * @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError() */ DOCBLOCK; $parser->setTarget(Target::TARGET_CLASS); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( "Expected namespace separator or identifier, got ')' at position 24" . ' in class @Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithTargetSyntaxError.' ); $parser->parse($docblock, $context); } public function testAnnotationWithInvalidTargetDeclarationError(): void { $parser = $this->createTestParser(); $context = 'class SomeClassName'; $docblock = <<<'DOCBLOCK' /** * @AnnotationWithInvalidTargetDeclaration() */ DOCBLOCK; $parser->setTarget(Target::TARGET_CLASS); $this->expectException(AnnotationException::class); try { $parser->parse($docblock, $context); } catch (AnnotationException $exc) { $previous = $exc->getPrevious(); $this->assertInstanceOf(InvalidArgumentException::class, $previous); $this->assertThat( $previous, new ExceptionMessage( 'Invalid Target "Foo". Available targets: [ALL, CLASS, METHOD, PROPERTY, FUNCTION, ANNOTATION]' ) ); throw $exc; } } public function testAnnotationWithTargetEmptyError(): void { $parser = $this->createTestParser(); $context = 'class SomeClassName'; $docblock = <<<'DOCBLOCK' /** * @AnnotationWithTargetEmpty() */ DOCBLOCK; $parser->setTarget(Target::TARGET_CLASS); $this->expectException(AnnotationException::class); try { $parser->parse($docblock, $context); } catch (AnnotationException $exc) { $previous = $exc->getPrevious(); $this->assertInstanceOf(InvalidArgumentException::class, $previous); $this->assertThat( $previous, new ExceptionMessage('@Target expects either a string value, or an array of strings, "NULL" given.') ); throw $exc; } } /** * @group DDC-575 */ public function testRegressionDDC575(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * @Name * * Will trigger error. */ DOCBLOCK; $result = $parser->parse($docblock); self::assertInstanceOf(Name::class, $result[0]); $docblock = <<<'DOCBLOCK' /** * @Name * @Marker * * Will trigger error. */ DOCBLOCK; $result = $parser->parse($docblock); self::assertInstanceOf(Name::class, $result[0]); } /** * @group DDC-77 */ public function testAnnotationWithoutClassIsIgnoredWithoutWarning(): void { $parser = new DocParser(); $parser->setIgnoreNotImportedAnnotations(true); $result = $parser->parse('@param'); self::assertEmpty($result); } /** * Tests if it's possible to ignore whole namespaces * * @param string $ignoreAnnotationName annotation/namespace to ignore * @param string $input annotation/namespace from the docblock * * @dataProvider provideTestIgnoreWholeNamespaces * @group 45 */ public function testIgnoreWholeNamespaces($ignoreAnnotationName, $input): void { $parser = new DocParser(); $parser->setIgnoredAnnotationNamespaces([$ignoreAnnotationName => true]); $result = $parser->parse($input); self::assertEmpty($result); } /** * @phpstan-return list */ public function provideTestIgnoreWholeNamespaces(): array { return [ ['Namespace', '@Namespace'], ['Namespace\\', '@Namespace'], ['Namespace', '@Namespace\Subnamespace'], ['Namespace\\', '@Namespace\Subnamespace'], ['Namespace', '@Namespace\Subnamespace\SubSubNamespace'], ['Namespace\\', '@Namespace\Subnamespace\SubSubNamespace'], ['Namespace\Subnamespace', '@Namespace\Subnamespace'], ['Namespace\Subnamespace\\', '@Namespace\Subnamespace'], ['Namespace\Subnamespace', '@Namespace\Subnamespace\SubSubNamespace'], ['Namespace\Subnamespace\\', '@Namespace\Subnamespace\SubSubNamespace'], ['Namespace\Subnamespace\SubSubNamespace', '@Namespace\Subnamespace\SubSubNamespace'], ['Namespace\Subnamespace\SubSubNamespace\\', '@Namespace\Subnamespace\SubSubNamespace'], ]; } /** * @group DCOM-168 */ public function testNotAnAnnotationClassIsIgnoredWithoutWarning(): void { $parser = new DocParser(); $parser->setIgnoredAnnotationNames([TestCase::class => true]); $result = $parser->parse('@\PHPUnit\Framework\TestCase'); self::assertEmpty($result); } public function testNotAnAnnotationClassIsIgnoredWithoutWarningWithoutCheating(): void { $parser = new DocParser(); $parser->setIgnoreNotImportedAnnotations(true); $result = $parser->parse('@\PHPUnit\Framework\TestCase'); self::assertEmpty($result); } public function testAnnotationDontAcceptSingleQuotes(): void { $parser = $this->createTestParser(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage("Expected PlainValue, got ''' at position 10."); $parser->parse("@Name(foo='bar')"); } /** * @group DCOM-41 */ public function testAnnotationDoesntThrowExceptionWhenAtSignIsNotFollowedByIdentifier(): void { $parser = new DocParser(); $result = $parser->parse("'@'"); self::assertEmpty($result); } /** * @group DCOM-41 */ public function testAnnotationThrowsExceptionWhenAtSignIsNotFollowedByIdentifierInNestedAnnotation(): void { $parser = new DocParser(); $this->expectException(AnnotationException::class); $parser->parse("@Doctrine\Tests\Common\Annotations\Name(@')"); } /** * @group DCOM-56 */ public function testAutoloadAnnotation(): void { self::assertFalse( class_exists('Doctrine\Tests\Common\Annotations\Fixture\Annotation\Autoload', false), 'Pre-condition: Doctrine\Tests\Common\Annotations\Fixture\Annotation\Autoload not allowed to be loaded.' ); $parser = new DocParser(); $parser->setImports([ 'autoload' => Fixtures\Annotation\Autoload::class, ]); $annotations = $parser->parse('@Autoload'); self::assertCount(1, $annotations); self::assertInstanceOf(Fixtures\Annotation\Autoload::class, $annotations[0]); } public function createTestParser(): DocParser { $parser = new DocParser(); $parser->setIgnoreNotImportedAnnotations(true); $parser->setImports([ 'name' => Name::class, '__NAMESPACE__' => 'Doctrine\Tests\Common\Annotations', ]); return $parser; } /** * @group DDC-78 */ public function testSyntaxErrorWithContextDescription(): void { $parser = $this->createTestParser(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( "Expected PlainValue, got ''' at position 10 in class \Doctrine\Tests\Common\Annotations\Name" ); $parser->parse("@Name(foo='bar')", 'class \Doctrine\Tests\Common\Annotations\Name'); } /** * @group DDC-183 */ public function testSyntaxErrorWithUnknownCharacters(): void { $docblock = <<<'DOCBLOCK' /** * @test at. */ class A { } DOCBLOCK; //$lexer = new \Doctrine\Common\Annotations\Lexer(); //$lexer->setInput(trim($docblock, '/ *')); //var_dump($lexer); try { $parser = $this->createTestParser(); self::assertEmpty($parser->parse($docblock)); } catch (AnnotationException $e) { $this->fail($e->getMessage()); } } /** * @group DCOM-14 */ public function testIgnorePHPDocThrowTag(): void { $docblock = <<<'DOCBLOCK' /** * @throws \RuntimeException */ class A { } DOCBLOCK; try { $parser = $this->createTestParser(); self::assertEmpty($parser->parse($docblock)); } catch (AnnotationException $e) { $this->fail($e->getMessage()); } } /** * @group DCOM-38 */ public function testCastInt(): void { $parser = $this->createTestParser(); $result = $parser->parse('@Name(foo=1234)'); $annot = $result[0]; self::assertIsInt($annot->foo); } /** * @group DCOM-38 */ public function testCastNegativeInt(): void { $parser = $this->createTestParser(); $result = $parser->parse('@Name(foo=-1234)'); $annot = $result[0]; self::assertIsInt($annot->foo); } /** * @group DCOM-38 */ public function testCastFloat(): void { $parser = $this->createTestParser(); $result = $parser->parse('@Name(foo=1234.345)'); $annot = $result[0]; self::assertIsFloat($annot->foo); } /** * @group DCOM-38 */ public function testCastNegativeFloat(): void { $parser = $this->createTestParser(); $result = $parser->parse('@Name(foo=-1234.345)'); $annot = $result[0]; self::assertIsFloat($annot->foo); $result = $parser->parse('@Marker(-1234.345)'); $annot = $result[0]; self::assertIsFloat($annot->value); } public function testSetValuesException(): void { $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationClassNameWithoutConstructor(invalidaProperty = "Some val") */ DOCBLOCK; $this->expectException(AnnotationException::class); $this->expectExceptionMessage( '[Creation Error] The annotation @SomeAnnotationClassNameWithoutConstructor declared' . ' on some class does not have a property named "invalidaProperty". Available properties: data, name' ); $this->createTestParser()->parse($docblock, 'some class'); } public function testInvalidIdentifierInAnnotation(): void { $parser = $this->createTestParser(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage('[Syntax Error] Expected Doctrine\Common\Annotations\DocLexer::T_IDENTIFIER' . ' or Doctrine\Common\Annotations\DocLexer::T_TRUE' . ' or Doctrine\Common\Annotations\DocLexer::T_FALSE' . " or Doctrine\Common\Annotations\DocLexer::T_NULL, got '3.42' at position 5."); $parser->parse('@Foo\3.42'); } public function testTrailingCommaIsAllowed(): void { $parser = $this->createTestParser(); $annots = $parser->parse('@Name({ "Foo", "Bar", })'); self::assertCount(1, $annots); self::assertEquals(['Foo', 'Bar'], $annots[0]->value); } public function testTabPrefixIsAllowed(): void { $docblock = <<<'DOCBLOCK' /** * @Name */ DOCBLOCK; $parser = $this->createTestParser(); $result = $parser->parse($docblock); self::assertCount(1, $result); self::assertInstanceOf(Name::class, $result[0]); } public function testDefaultAnnotationValueIsNotOverwritten(): void { $parser = $this->createTestParser(); $annots = $parser->parse('@Doctrine\Tests\Common\Annotations\Fixtures\Annotation\AnnotWithDefaultValue'); self::assertCount(1, $annots); self::assertEquals('bar', $annots[0]->foo); } public function testArrayWithColon(): void { $parser = $this->createTestParser(); $annots = $parser->parse('@Name({"foo": "bar"})'); self::assertCount(1, $annots); self::assertEquals(['foo' => 'bar'], $annots[0]->value); } public function testInvalidContantName(): void { $parser = $this->createTestParser(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage("[Semantical Error] Couldn't find constant foo."); $parser->parse('@Name(foo: "bar")'); } /** * Tests parsing empty arrays. */ public function testEmptyArray(): void { $parser = $this->createTestParser(); $annots = $parser->parse('@Name({"foo": {}})'); self::assertCount(1, $annots); self::assertEquals(['foo' => []], $annots[0]->value); } public function testKeyHasNumber(): void { $parser = $this->createTestParser(); $annots = $parser->parse('@SettingsAnnotation(foo="test", bar2="test")'); self::assertCount(1, $annots); self::assertEquals(['foo' => 'test', 'bar2' => 'test'], $annots[0]->settings); } /** * @group 44 */ public function testSupportsEscapedQuotedValues(): void { $result = $this->createTestParser()->parse('@Doctrine\Tests\Common\Annotations\Name(foo="""bar""")'); self::assertCount(1, $result); self::assertInstanceOf(Name::class, $result[0]); self::assertEquals('"bar"', $result[0]->foo); } /** * @see http://php.net/manual/en/mbstring.configuration.php * mbstring.func_overload can be changed only in php.ini * so for testing this case instead of skipping it you need to manually configure your php installation */ public function testMultiByteAnnotation(): void { $overloadStringFunctions = 2; if (! extension_loaded('mbstring') || (ini_get('mbstring.func_overload') & $overloadStringFunctions) === 0) { $this->markTestSkipped('This test requires mbstring function overloading is turned on'); } $docblock = <<<'DOCBLOCK' /** * Мультибайтовый текст ломал парсер при оверлоадинге строковых функций * @Doctrine\Tests\Common\Annotations\Name */ DOCBLOCK; $docParser = $this->createTestParser(); $result = $docParser->parse($docblock); self::assertCount(1, $result); } public function testWillNotParseAnnotationSucceededByAnImmediateDash(): void { $parser = $this->createTestParser(); self::assertEmpty($parser->parse('@SomeAnnotationClassNameWithoutConstructorAndProperties-')); } public function testWillParseAnnotationSucceededByANonImmediateDash(): void { $result = $this ->createTestParser() ->parse('@SomeAnnotationClassNameWithoutConstructorAndProperties -'); self::assertCount(1, $result); self::assertInstanceOf(SomeAnnotationClassNameWithoutConstructorAndProperties::class, $result[0]); } public function testNamedArgumentsConstructorAnnotation(): void { $result = $this ->createTestParser() ->parse('/** @AnotherNamedAnnotation(foo="baz", bar=2222) */'); self::assertCount(1, $result); self::assertInstanceOf(AnotherNamedAnnotation::class, $result[0]); self::assertSame('baz', $result[0]->getFoo()); self::assertSame(2222, $result[0]->getBar()); } public function testNamedReorderedArgumentsConstructorAnnotation(): void { $result = $this ->createTestParser() ->parse('/** @AnotherNamedAnnotation(bar=2222, foo="baz") */'); self::assertCount(1, $result); self::assertInstanceOf(AnotherNamedAnnotation::class, $result[0]); self::assertSame('baz', $result[0]->getFoo()); self::assertSame(2222, $result[0]->getBar()); } public function testNamedArgumentsConstructorAnnotationWithDefaultValue(): void { $result = $this ->createTestParser() ->parse('/** @AnotherNamedAnnotation(foo="baz") */'); self::assertCount(1, $result); self::assertInstanceOf(AnotherNamedAnnotation::class, $result[0]); self::assertSame('baz', $result[0]->getFoo()); self::assertSame(1234, $result[0]->getBar()); } public function testNamedArgumentsConstructorAnnotationWithDefaultProperty(): void { $result = $this ->createTestParser() ->parse('/** @AnotherNamedAnnotation("baz") */'); self::assertCount(1, $result); self::assertInstanceOf(AnotherNamedAnnotation::class, $result[0]); self::assertSame('baz', $result[0]->getFoo()); self::assertSame(1234, $result[0]->getBar()); } public function testNamedArgumentsConstructorAnnotationWithExtraArguments(): void { $docParser = $this->createTestParser(); $this->expectException(AnnotationException::class); $this->expectExceptionMessageMatches( '/does not have a property named "invalid"\s.*\sAvailable named arguments: foo, bar/' ); $docParser->parse('/** @AnotherNamedAnnotation(foo="baz", invalid="uh oh") */'); } public function testNamedArgumentsConstructorAnnotationWithDefaultPropertyAsArray(): void { $result = $this ->createTestParser() ->parse('/** @NamedAnnotationWithArray({"foo","bar","baz"},bar=567) */'); self::assertCount(1, $result); self::assertInstanceOf(NamedAnnotationWithArray::class, $result[0]); self::assertSame(['foo', 'bar', 'baz'], $result[0]->getFoo()); self::assertSame(567, $result[0]->getBar()); } public function testNamedArgumentsConstructorAnnotationWithDefaultPropertySet(): void { $result = $this ->createTestParser() ->parse('/** @AnotherNamedAnnotation("baz", foo="bar") */'); self::assertCount(1, $result); self::assertInstanceOf(AnotherNamedAnnotation::class, $result[0]); self::assertSame('bar', $result[0]->getFoo()); } public function testNamedArgumentsConstructorAnnotationWithInvalidArguments(): void { $parser = $this->createTestParser(); $this->expectException(AnnotationException::class); $this->expectExceptionMessage( '[Syntax Error] Expected Positional arguments after named arguments is not allowed' ); $parser->parse('/** @AnotherNamedAnnotation("foo", bar=666, "hey") */'); } public function testNamedArgumentsConstructorAnnotationWithWrongArgumentType(): void { $context = 'property SomeClassName::invalidProperty.'; $docblock = '@NamedAnnotationWithArray(foo = "no array!")'; $parser = $this->createTestParser(); $this->expectException(AnnotationException::class); $this->expectExceptionMessageMatches( '/\[Creation Error\] An error occurred while instantiating the annotation ' . '@NamedAnnotationWithArray declared on property SomeClassName::invalidProperty\.: ".*"\.$/' ); try { $parser->parse($docblock, $context); } catch (AnnotationException $exc) { $this->assertInstanceOf(TypeError::class, $exc->getPrevious()); throw $exc; } } public function testAnnotationWithConstructorWithVariadicParamAndExtraNamedArguments(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationWithConstructorWithVariadicParam(name = "Some data", foo = "Foo", bar = "Bar") */ DOCBLOCK; $this->expectException(AnnotationException::class); $this->expectExceptionMessageMatches( '/does not have a property named "foo"\s.*\sAvailable named arguments: name/' ); $parser->parse($docblock); } public function testAnnotationWithConstructorWithVariadicParamAndExtraNamedArgumentsShuffled(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationWithConstructorWithVariadicParam(foo = "Foo", name = "Some data", bar = "Bar") */ DOCBLOCK; $this->expectException(AnnotationException::class); $this->expectExceptionMessageMatches( '/does not have a property named "foo"\s.*\sAvailable named arguments: name/' ); $parser->parse($docblock); } public function testAnnotationWithConstructorWithVariadicParamAndCombinedNamedAndPositionalArguments(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationWithConstructorWithVariadicParam("Some data", "Foo", bar = "Bar") */ DOCBLOCK; $this->expectException(AnnotationException::class); $this->expectExceptionMessageMatches( '/does not have a property named "bar"\s.*\sAvailable named arguments: name/' ); $parser->parse($docblock); } public function testAnnotationWithConstructorWithVariadicParamPassOneNamedArgument(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationWithConstructorWithVariadicParam(name = "Some data", data = "Foo") */ DOCBLOCK; $this->expectException(AnnotationException::class); $this->expectExceptionMessageMatches( '/does not have a property named "data"\s.*\sAvailable named arguments: name/' ); $parser->parse($docblock); } public function testAnnotationWithConstructorWithVariadicParamPassPositionalArguments(): void { $parser = $this->createTestParser(); $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationWithConstructorWithVariadicParam("Some data", "Foo", "Bar") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertInstanceOf(SomeAnnotationWithConstructorWithVariadicParam::class, $annot); self::assertSame('Some data', $annot->name); // Positional extra arguments will be ignored self::assertSame([], $annot->data); } public function testAnnotationWithConstructorWithVariadicParamNoArgs(): void { $parser = $this->createTestParser(); // Without variadic arguments $docblock = <<<'DOCBLOCK' /** * @SomeAnnotationWithConstructorWithVariadicParam("Some data") */ DOCBLOCK; $result = $parser->parse($docblock); self::assertCount(1, $result); $annot = $result[0]; self::assertInstanceOf(SomeAnnotationWithConstructorWithVariadicParam::class, $annot); self::assertSame('Some data', $annot->name); self::assertSame([], $annot->data); } /** * Override for BC with PHPUnit <8 */ public function expectExceptionMessageMatches(string $regularExpression): void { if (method_exists(get_parent_class($this), 'expectExceptionMessageMatches')) { parent::expectExceptionMessageMatches($regularExpression); } else { parent::expectExceptionMessageRegExp($regularExpression); } } } /** * @Annotation * @NamedArgumentConstructor */ class AnotherNamedAnnotation { /** @var string */ private $foo; /** @var int */ private $bar; /** @var string */ private $baz; public function __construct(string $foo, int $bar = 1234, string $baz = 'baz') { $this->foo = $foo; $this->bar = $bar; $this->baz = $baz; } public function getFoo(): string { return $this->foo; } public function getBar(): int { return $this->bar; } public function getBaz(): string { return $this->baz; } } /** * @Annotation * @NamedArgumentConstructor */ class NamedAnnotationWithArray { /** @var mixed[] */ private $foo; /** @var int */ private $bar; /** * @param mixed[] $foo */ public function __construct(array $foo, int $bar = 1234) { $this->foo = $foo; $this->bar = $bar; } /** @return mixed[] */ public function getFoo(): array { return $this->foo; } public function getBar(): int { return $this->bar; } } /** * @Annotation * @NamedArgumentConstructor */ class SomeAnnotationWithConstructorWithVariadicParam { public function __construct(string $name, string ...$data) { $this->name = $name; $this->data = $data; } /** @var string[] */ public $data; /** @var string */ public $name; } /** @Annotation */ class SettingsAnnotation { /** @var mixed[] */ public $settings; /** * @param mixed[] $settings */ public function __construct($settings) { $this->settings = $settings; } } /** @Annotation */ class SomeAnnotationClassNameWithoutConstructor { /** @var mixed */ public $data; /** @var mixed */ public $name; } /** @Annotation */ class SomeAnnotationWithConstructorWithoutParams { public function __construct() { $this->data = 'Some data'; } /** @var mixed */ public $data; /** @var mixed */ public $name; } /** @Annotation */ class SomeAnnotationClassNameWithoutConstructorAndProperties { } /** * @Annotation * @Target("Foo") */ class AnnotationWithInvalidTargetDeclaration { } /** * @Annotation * @Target */ class AnnotationWithTargetEmpty { } /** @Annotation */ class AnnotationExtendsAnnotationTargetAll extends AnnotationTargetAll { } /** @Annotation */ class Name extends Annotation { /** @var mixed */ public $foo; } /** @Annotation */ class Marker { /** @var mixed */ public $value; } namespace Doctrine\Tests\Common\Annotations\FooBar; use Doctrine\Common\Annotations\Annotation; /** @Annotation */ class Name extends Annotation { } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/DummyClass.php ================================================ roles = $values['value']; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/ShouldNeverBeLoaded.php ================================================ name = $values['value'] ?? null; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/Annotation/Version.php ================================================ "), @Attribute("arrayOfStrings", type = "string[]"), @Attribute("annotation", type = "Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll"), @Attribute("arrayOfAnnotations", type = "array"), }) */ final class AnnotationWithAttributes { /** * @param mixed[] $data */ public function __construct(array $data) { foreach ($data as $key => $value) { $this->$key = $value; } } /** @var mixed */ private $mixed; /** @var bool */ private $boolean; /** @var bool */ private $bool; /** @var float */ private $float; /** @var string */ private $string; /** @var integer */ private $integer; /** @var mixed[] */ private $array; /** @var object */ private $annotation; /** @var int[] */ private $arrayOfIntegers; /** @var string[] */ private $arrayOfStrings; /** @var object[] */ private $arrayOfAnnotations; /** * @return mixed */ public function getMixed() { return $this->mixed; } /** * @return boolean */ public function getBoolean() { return $this->boolean; } /** * @return bool */ public function getBool() { return $this->bool; } /** * @return float */ public function getFloat() { return $this->float; } /** * @return string */ public function getString() { return $this->string; } public function getInteger(): int { return $this->integer; } /** * @return mixed[] */ public function getArray() { return $this->array; } /** * @return AnnotationTargetAll */ public function getAnnotation() { return $this->annotation; } /** * @return string[] */ public function getArrayOfStrings() { return $this->arrayOfStrings; } /** * @return array */ public function getArrayOfIntegers() { return $this->arrayOfIntegers; } /** * @return array */ public function getArrayOfAnnotations() { return $this->arrayOfAnnotations; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/AnnotationWithConstants.php ================================================ $value) { $this->$key = $value; } } /** @var string */ private $value; /** @var AnnotationTargetAnnotation */ private $annot; /** * @return string */ public function getValue() { return $this->value; } /** * @return AnnotationTargetAnnotation */ public function getAnnot() { return $this->annot; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/AnnotationWithRequiredAttributesWithoutConstructor.php ================================================ */ public $arrayOfIntegers; /** @var string[] */ public $arrayOfStrings; /** @var \Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetAll[] */ public $arrayOfAnnotations; } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/Api.php ================================================ classProperty; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/ClassWithAnnotationWithTargetSyntaxError.php ================================================ events->filter(static function ($item) use ($year, $month, $day) { $leftDate = new \DateTime($year . '-' . $month . '-' . $day . ' 00:00'); $rigthDate = new \DateTime($year . '-' . $month . '-' . $day . ' +1 day 00:00'); return ( $leftDate <= $item->getDateStart() ) && ( $item->getDateStart() < $rigthDate ); }); return $extractEvents; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/ClassWithConstants.php ================================================ * @extends ClassWithPHPStanExtendsAnnotationsGeneric */ class ClassWithPHPStanGenericsAnnotations extends ClassWithPHPStanExtendsAnnotationsGeneric implements WithPHPStanExtendsAnnotations { /** * @use GenericPHPStanTrait */ use GenericPHPStanTrait; /** @var array */ private $bar; /** * @param array $array * * @return array */ public function foo($array) { return $this->bar; } /** * @Template("@foo.html.twig") */ public function twigTemplateFunctionName(): void { } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/ClassWithPhpCsSuppressAnnotation.php ================================================ */ class Controller { /** * @return mixed[] * * @Route("/", name="_demo") * @Template() */ public function indexAction(): array { return []; } /** * @return mixed[] * * @Route("/hello/{name}", name="_demo_hello") * @Template() */ public function helloAction(string $name): array { return ['name' => $name]; } /** * @return mixed[] * * @Route("/contact", name="_demo_contact") * @Template() */ public function contactAction(): array { $form = ContactForm::create($this->get('form.context'), 'contact'); $form->bind($this->container->get('request'), $form); if ($form->isValid()) { $form->send($this->get('mailer')); $this->get('session')->setFlash('notice', 'Message sent!'); return new RedirectResponse($this->generateUrl('_demo')); } return ['form' => $form]; } /** * Creates the ACL for the passed object identity */ private function createObjectIdentity(ObjectIdentityInterface $oid): void { $classId = $this->createOrRetrieveClassId($oid->getType()); $this->connection->executeQuery($this->getInsertObjectIdentitySql($oid->getIdentifier(), $classId, true)); } /** * Returns the primary key for the passed class type. * * If the type does not yet exist in the database, it will be created. * * @param string $classType * * @return integer */ private function createOrRetrieveClassId($classType) { $id = $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchColumn(); if ($id !== false) { return $id; } $this->connection->executeQuery($this->getInsertClassSql($classType)); return $this->connection->executeQuery($this->getSelectClassIdSql($classType))->fetchColumn(); } /** * Returns the primary key for the passed security identity. * * If the security identity does not yet exist in the database, it will be * created. * * @return integer */ private function createOrRetrieveSecurityIdentityId(SecurityIdentityInterface $sid) { $id = $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchColumn(); if ($id !== false) { return $id; } $this->connection->executeQuery($this->getInsertSecurityIdentitySql($sid)); return $this->connection->executeQuery($this->getSelectSecurityIdentityIdSql($sid))->fetchColumn(); } /** * Deletes all ACEs for the given object identity primary key. * * @param integer $oidPK */ private function deleteAccessControlEntries($oidPK): void { $this->connection->executeQuery($this->getDeleteAccessControlEntriesSql($oidPK)); } /** * Deletes the object identity from the database. * * @param integer $pk */ private function deleteObjectIdentity($pk): void { $this->connection->executeQuery($this->getDeleteObjectIdentitySql($pk)); } /** * Deletes all entries from the relations table from the database. * * @param integer $pk */ private function deleteObjectIdentityRelations($pk): void { $this->connection->executeQuery($this->getDeleteObjectIdentityRelationsSql($pk)); } /** * This regenerates the ancestor table which is used for fast read access. */ private function regenerateAncestorRelations(AclInterface $acl): void { $pk = $acl->getId(); $this->connection->executeQuery($this->getDeleteObjectIdentityRelationsSql($pk)); $this->connection->executeQuery($this->getInsertObjectIdentityRelationSql($pk, $pk)); $parentAcl = $acl->getParentAcl(); while ($parentAcl !== null) { $this->connection->executeQuery($this->getInsertObjectIdentityRelationSql($pk, $parentAcl->getId())); $parentAcl = $parentAcl->getParentAcl(); } } /** * This processes changes on an ACE related property (classFieldAces, or objectFieldAces). * * @param string $name * @param mixed[] $changes */ private function updateFieldAceProperty($name, array $changes): void { $sids = new \SplObjectStorage(); $classIds = new \SplObjectStorage(); $currentIds = []; foreach ($changes[1] as $field => $new) { for ($i = 0,$c = count($new); $i < $c; $i++) { $ace = $new[$i]; if ($ace->getId() === null) { if ($sids->contains($ace->getSecurityIdentity())) { $sid = $sids->offsetGet($ace->getSecurityIdentity()); } else { $sid = $this->createOrRetrieveSecurityIdentityId($ace->getSecurityIdentity()); } $oid = $ace->getAcl()->getObjectIdentity(); if ($classIds->contains($oid)) { $classId = $classIds->offsetGet($oid); } else { $classId = $this->createOrRetrieveClassId($oid->getType()); } $objectIdentityId = $name === 'classFieldAces' ? null : $ace->getAcl()->getId(); $this->connection->executeQuery($this->getInsertAccessControlEntrySql( $classId, $objectIdentityId, $field, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure() )); $aceId = $this->connection->executeQuery( $this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, $field, $i) )->fetchColumn(); $this->loadedAces[$aceId] = $ace; $aceIdProperty = new \ReflectionProperty('Symfony\Component\Security\Acl\Domain\Entry', 'id'); if (PHP_VERSION_ID < 80100) { $aceIdProperty->setAccessible(true); } $aceIdProperty->setValue($ace, (int) $aceId); } else { $currentIds[$ace->getId()] = true; } } } foreach ($changes[0] as $old) { for ($i = 0,$c = count($old); $i < $c; $i++) { $ace = $old[$i]; if (isset($currentIds[$ace->getId()])) { continue; } $this->connection->executeQuery($this->getDeleteAccessControlEntrySql($ace->getId())); unset($this->loadedAces[$ace->getId()]); } } } /** * This processes changes on an ACE related property (classAces, or objectAces). * * @param string $name * @param mixed[] $changes */ private function updateAceProperty($name, array $changes): void { [$old, $new] = $changes; $sids = new \SplObjectStorage(); $classIds = new \SplObjectStorage(); $currentIds = []; for ($i = 0,$c = count($new); $i < $c; $i++) { $ace = $new[$i]; if ($ace->getId() === null) { if ($sids->contains($ace->getSecurityIdentity())) { $sid = $sids->offsetGet($ace->getSecurityIdentity()); } else { $sid = $this->createOrRetrieveSecurityIdentityId($ace->getSecurityIdentity()); } $oid = $ace->getAcl()->getObjectIdentity(); if ($classIds->contains($oid)) { $classId = $classIds->offsetGet($oid); } else { $classId = $this->createOrRetrieveClassId($oid->getType()); } $objectIdentityId = $name === 'classAces' ? null : $ace->getAcl()->getId(); $this->connection->executeQuery($this->getInsertAccessControlEntrySql( $classId, $objectIdentityId, null, $i, $sid, $ace->getStrategy(), $ace->getMask(), $ace->isGranting(), $ace->isAuditSuccess(), $ace->isAuditFailure() )); $aceId = $this->connection->executeQuery( $this->getSelectAccessControlEntryIdSql($classId, $objectIdentityId, null, $i) )->fetchColumn(); $this->loadedAces[$aceId] = $ace; $aceIdProperty = new \ReflectionProperty($ace, 'id'); if (PHP_VERSION_ID < 80100) { $aceIdProperty->setAccessible(true); } $aceIdProperty->setValue($ace, (int) $aceId); } else { $currentIds[$ace->getId()] = true; } } for ($i = 0,$c = count($old); $i < $c; $i++) { $ace = $old[$i]; if (isset($currentIds[$ace->getId()])) { continue; } $this->connection->executeQuery($this->getDeleteAccessControlEntrySql($ace->getId())); unset($this->loadedAces[$ace->getId()]); } } /** * Persists the changes which were made to ACEs to the database. */ private function updateAces(\SplObjectStorage $aces): void { foreach ($aces as $ace) { $propertyChanges = $aces->offsetGet($ace); $sets = []; if (isset($propertyChanges['mask'])) { $sets[] = sprintf('mask = %d', $propertyChanges['mask'][1]); } if (isset($propertyChanges['strategy'])) { $sets[] = sprintf('granting_strategy = %s', $this->connection->quote($propertyChanges['strategy'])); } if (isset($propertyChanges['aceOrder'])) { $sets[] = sprintf('ace_order = %d', $propertyChanges['aceOrder'][1]); } if (isset($propertyChanges['auditSuccess'])) { $sets[] = sprintf( 'audit_success = %s', $this->connection->getDatabasePlatform()->convertBooleans($propertyChanges['auditSuccess'][1]) ); } if (isset($propertyChanges['auditFailure'])) { $sets[] = sprintf( 'audit_failure = %s', $this->connection->getDatabasePlatform()->convertBooleans($propertyChanges['auditFailure'][1]) ); } $this->connection->executeQuery($this->getUpdateAccessControlEntrySql($ace->getId(), $sets)); } } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/ControllerWithParentClass.php ================================================ */ class ControllerWithParentClass extends AbstractController { /** * @return mixed[] * * @Route("/", name="_demo") * @Template() */ public function indexAction(): array { return []; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/ControllerWithTrait.php ================================================ */ class ControllerWithTrait { use SecretRouteTrait; /** * @return mixed[] * * @Route("/", name="_demo") * @Template() */ public function indexAction(): array { return []; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/DifferentNamespacesPerFileWithClassAsFirst.php ================================================ test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test2(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test3(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test4(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test5(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test6(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test7(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test8(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test9(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test10(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test11(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test12(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test13(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test14(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test15(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test16(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test17(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test18(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test19(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test20(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test21(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test22(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test23(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test24(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test25(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test26(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test27(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test28(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test29(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test30(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test31(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test32(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test33(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test34(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test35(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test36(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test37(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test38(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test39(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/NoAnnotation.php ================================================ test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test2(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test3(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test4(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test5(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test6(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test7(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test8(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test9(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test10(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test11(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test12(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test13(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test14(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test15(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test16(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test17(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test18(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test19(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test20(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test21(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test22(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test23(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test24(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test25(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test26(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test27(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test28(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test29(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test30(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test31(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test32(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test33(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test34(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test35(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test36(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test37(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test38(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } public function test39(): string { echo $this->test1; echo $this->test2; echo $this->test3; $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; foreach ($array as $key => $value) { echo $key . ' => ' . $value; } $val = (string) self::TEST1; $val .= (string) self::TEST2; $val .= (string) self::TEST3; $val .= (string) self::TEST4; $val .= (string) self::TEST5; $val .= (string) self::TEST6; $val .= (string) self::TEST7; $val .= (string) self::TEST8; $val .= (string) self::TEST9; strtolower($val); return $val; } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Fixtures/Suit.php ================================================ __NAMESPACE__ . '\Fixtures\Annotation\Route', 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', ], $parser->parseUseStatements($class)); } public function testParseClassWithMultipleImportsInUseStatement(): void { $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\MultipleImportsInUseStatement'); $parser = new PhpParser(); self::assertEquals([ 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', ], $parser->parseUseStatements($class)); } /** * @requires PHP 7.0 */ public function testParseClassWithGroupUseStatement(): void { $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\GroupUseStatement'); $parser = new PhpParser(); self::assertEquals([ 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', 'supersecure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); } public function testParseClassWhenNotUserDefined(): void { $parser = new PhpParser(); self::assertEquals([], $parser->parseUseStatements(new ReflectionClass(stdClass::class))); } public function testClassFileDoesNotExist(): void { $class = $this->createMock(ReflectionClass::class); $class->expects($this->once()) ->method('getFilename') ->will($this->returnValue('/valid/class/Fake.php(35) : eval()d code')); $parser = new PhpParser(); self::assertEquals([], $parser->parseUseStatements($class)); } public function testParseClassWhenClassIsNotNamespaced(): void { $parser = new PhpParser(); $class = new ReflectionClass(\AnnotationsTestsFixturesNonNamespacedClass::class); self::assertEquals([ 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); } public function testParseClassWhenClassIsInterface(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\HereForTesting'); self::assertEquals([ 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', ], $parser->parseUseStatements($class)); } public function testClassWithFullyQualifiedUseStatements(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\ClassWithFullyQualifiedUseStatements'); self::assertEquals([ 'secure' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Secure', 'route' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Route', 'template' => '\\' . __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); } public function testNamespaceAndClassCommentedOut(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceAndClassCommentedOut'); self::assertEquals([ 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); } public function testEqualNamespacesPerFileWithClassAsFirst(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\EqualNamespacesPerFileWithClassAsFirst'); self::assertEquals([ 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', ], $parser->parseUseStatements($class)); } public function testEqualNamespacesPerFileWithClassAsLast(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\EqualNamespacesPerFileWithClassAsLast'); self::assertEquals([ 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); } public function testDifferentNamespacesPerFileWithClassAsFirst(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\DifferentNamespacesPerFileWithClassAsFirst'); self::assertEquals([ 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', ], $parser->parseUseStatements($class)); } public function testDifferentNamespacesPerFileWithClassAsLast(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\DifferentNamespacesPerFileWithClassAsLast'); self::assertEquals([ 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); } public function testGlobalNamespacesPerFileWithClassAsFirst(): void { $parser = new PhpParser(); $class = new ReflectionClass(\GlobalNamespacesPerFileWithClassAsFirst::class); self::assertEquals([ 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', ], $parser->parseUseStatements($class)); } public function testGlobalNamespacesPerFileWithClassAsLast(): void { $parser = new PhpParser(); $class = new ReflectionClass(\GlobalNamespacesPerFileWithClassAsLast::class); self::assertEquals([ 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); } public function testNamespaceWithClosureDeclaration(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceWithClosureDeclaration'); self::assertEquals([ 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); } public function testIfPointerResetsOnMultipleParsingTries(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\NamespaceWithClosureDeclaration'); self::assertEquals([ 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); self::assertEquals([ 'secure' => __NAMESPACE__ . '\Fixtures\Annotation\Secure', 'route' => __NAMESPACE__ . '\Fixtures\Annotation\Route', 'template' => __NAMESPACE__ . '\Fixtures\Annotation\Template', ], $parser->parseUseStatements($class)); } /** * @group DCOM-97 * @group regression */ public function testClassWithClosure(): void { $parser = new PhpParser(); $class = new ReflectionClass(__NAMESPACE__ . '\Fixtures\ClassWithClosure'); self::assertEquals([ 'annotationtargetall' => __NAMESPACE__ . '\Fixtures\AnnotationTargetAll', 'annotationtargetannotation' => __NAMESPACE__ . '\Fixtures\AnnotationTargetAnnotation', ], $parser->parseUseStatements($class)); } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/PsrCachedReaderTest.php ================================================ doTestCacheStale(Fixtures\Controller::class, $cache); } /** * @group 62 */ public function testIgnoresStaleCacheWithParentClass(): void { $cache = time() - 10; touch(__DIR__ . '/Fixtures/ControllerWithParentClass.php', $cache - 10); touch(__DIR__ . '/Fixtures/AbstractController.php', $cache + 10); $this->doTestCacheStale(Fixtures\ControllerWithParentClass::class, $cache); } /** * @group 62 */ public function testIgnoresStaleCacheWithTraits(): void { $cache = time() - 10; touch(__DIR__ . '/Fixtures/ControllerWithTrait.php', $cache - 10); touch(__DIR__ . '/Fixtures/Traits/SecretRouteTrait.php', $cache + 10); $this->doTestCacheStale(Fixtures\ControllerWithTrait::class, $cache); } /** * @group 62 */ public function testIgnoresStaleCacheWithTraitsThatUseOtherTraits(): void { $cache = time() - 10; touch(__DIR__ . '/Fixtures/ClassThatUsesTraitThatUsesAnotherTrait.php', $cache - 10); touch(__DIR__ . '/Fixtures/Traits/EmptyTrait.php', $cache + 10); $this->doTestCacheStale( Fixtures\ClassThatUsesTraitThatUsesAnotherTrait::class, $cache ); } /** * @group 62 */ public function testIgnoresStaleCacheWithInterfacesThatExtendOtherInterfaces(): void { $cache = time() - 10; touch(__DIR__ . '/Fixtures/InterfaceThatExtendsAnInterface.php', $cache - 10); touch(__DIR__ . '/Fixtures/EmptyInterface.php', $cache + 10); $this->doTestCacheStale( Fixtures\InterfaceThatExtendsAnInterface::class, $cache ); } /** * @group 62 * @group 105 */ public function testUsesFreshCacheWithTraitsThatUseOtherTraits(): void { $cacheTime = time(); touch(__DIR__ . '/Fixtures/ClassThatUsesTraitThatUsesAnotherTrait.php', $cacheTime - 10); touch(__DIR__ . '/Fixtures/Traits/EmptyTrait.php', $cacheTime - 10); $this->doTestCacheFresh( 'Doctrine\Tests\Common\Annotations\Fixtures\ClassThatUsesTraitThatUsesAnotherTrait', $cacheTime ); } /** * @group 62 */ public function testPurgeLoadedAnnotations(): void { $cache = time() - 10; touch(__DIR__ . '/Fixtures/ClassThatUsesTraitThatUsesAnotherTrait.php', $cache - 10); touch(__DIR__ . '/Fixtures/Traits/EmptyTrait.php', $cache + 10); $reader = $this->doTestCacheStale( Fixtures\ClassThatUsesTraitThatUsesAnotherTrait::class, $cache ); $classReader = new ReflectionClass(PsrCachedReader::class); $loadedAnnotationsProperty = $classReader->getProperty('loadedAnnotations'); if (PHP_VERSION_ID < 80100) { $loadedAnnotationsProperty->setAccessible(true); } $this->assertCount(1, $loadedAnnotationsProperty->getValue($reader)); $loadedFilemtimesProperty = $classReader->getProperty('loadedFilemtimes'); if (PHP_VERSION_ID < 80100) { $loadedFilemtimesProperty->setAccessible(true); } $this->assertCount(3, $loadedFilemtimesProperty->getValue($reader)); $reader->clearLoadedAnnotations(); $this->assertCount(0, $loadedAnnotationsProperty->getValue($reader)); $this->assertCount(0, $loadedFilemtimesProperty->getValue($reader)); } /** * As there is a cache on loadedAnnotations, we need to test two different * methods' annotations of the same file * * We test four things * 1. we load the file (and its filemtime) for method1's annotation with fresh cache * 2. we load the file for method2 with stale cache => but still no save, because seen as fresh * 3. we purge loaded annotations and filemtime * 4. same as 2, but this time without filemtime cache, so file seen as stale and new cache is saved * * @group 62 * @group 105 */ public function testAvoidCallingFilemtimeTooMuch(): void { $this->markTestSkipped('Skipped until further investigation'); $className = ClassThatUsesTraitThatUsesAnotherTraitWithMethods::class; $cacheTime = time() - 10; $cacheKeyMethod1 = rawurlencode($className . '#method1'); $cacheKeyMethod2 = rawurlencode($className . '#method2'); $route1 = new Route(); $route1->pattern = '/someprefix'; $route2 = new Route(); $route2->pattern = '/someotherprefix'; $cacheItem1 = $this->createCacheItem($cacheKeyMethod1, true, [$route1]); $timeCacheItem1 = $this->createCacheItem('[C]' . $cacheKeyMethod1, true, $cacheTime); $cacheItem2 = $this->createCacheItem($cacheKeyMethod2, true, [$route2]); $timeCacheItem2 = $this->createCacheItem('[C]' . $cacheKeyMethod2, true, $cacheTime); $cache = $this->createMock(CacheItemPoolInterface::class); $cache ->method('getItem') ->willReturnMap([ [$cacheKeyMethod1, $cacheItem1], [$cacheKeyMethod2, $cacheItem2], ['[C]' . $cacheKeyMethod1, $timeCacheItem1], ['[C]' . $cacheKeyMethod2, $timeCacheItem2], ]); $cache ->expects($this->exactly(2)) ->method('save') ->withConsecutive( [$timeCacheItem2], [$cacheItem2] ); $reader = new PsrCachedReader(new AnnotationReader(), $cache, true); touch(__DIR__ . '/Fixtures/ClassThatUsesTraitThatUsesAnotherTraitWithMethods.php', $cacheTime - 20); touch(__DIR__ . '/Fixtures/Traits/EmptyTrait.php', $cacheTime - 20); $this->assertEquals([$route1], $reader->getMethodAnnotations(new ReflectionMethod($className, 'method1'))); // only filemtime changes, but not cleared => no change touch(__DIR__ . '/Fixtures/ClassThatUsesTraitThatUsesAnotherTrait.php', $cacheTime + 5); touch(__DIR__ . '/Fixtures/Traits/EmptyTrait.php', $cacheTime + 5); $this->assertEquals([$route2], $reader->getMethodAnnotations(new ReflectionMethod($className, 'method2'))); $reader->clearLoadedAnnotations(); $this->assertEquals([$route2], $reader->getMethodAnnotations(new ReflectionMethod($className, 'method2'))); } public function testReaderIsNotHitIfCacheIsFresh(): void { $cache = new ArrayAdapter(); $readAnnotations = (new PsrCachedReader(new AnnotationReader(), $cache, true)) ->getClassAnnotations(new ReflectionClass(ClassWithClassAnnotationOnly::class)); $throwingReader = $this->createMock(Reader::class); $throwingReader->expects(self::never())->method(self::anything()); self::assertEquals( $readAnnotations, (new PsrCachedReader($throwingReader, $cache, true)) ->getClassAnnotations(new ReflectionClass(ClassWithClassAnnotationOnly::class)) ); } public function testReaderDoesNotCacheIfFileDoesNotExistSoLastModificationCannotBeDetermined(): void { $code = <<<'EOS' namespace Doctrine\Tests\Common\Annotations; /** * @\Doctrine\Tests\Common\Annotations\Fixtures\AnnotationTargetClass("Some data") */ class PsrCachedEvalClass { } EOS; eval($code); $reader = new PsrCachedReader(new AnnotationReader(), new ArrayAdapter(), true); // @phpstan-ignore class.notFound $readAnnotations = $reader->getClassAnnotations(new ReflectionClass(PsrCachedEvalClass::class)); self::assertCount(1, $readAnnotations); } protected function doTestCacheStale(string $className, int $lastCacheModification): PsrCachedReader { $cacheKey = rawurlencode($className); $route = new Route(); $route->pattern = '/someprefix'; $cacheItem = $this->createCacheItem($cacheKey, true, []); $timeCacheItem = $this->createCacheItem('[C]' . $cacheKey, true, $lastCacheModification); $cache = $this->createMock(CacheItemPoolInterface::class); $cache ->method('getItem') ->willReturnMap([ [$cacheKey, $cacheItem], ['[C]' . $cacheKey, $timeCacheItem], ]); $cache ->expects($this->exactly(2)) ->method('save') ->withConsecutive([$timeCacheItem], [$cacheItem]); $reader = new PsrCachedReader(new AnnotationReader(), $cache, true); self::assertEquals([$route], $reader->getClassAnnotations(new ReflectionClass($className))); return $reader; } protected function doTestCacheFresh(string $className, int $lastCacheModification): void { $cacheKey = rawurlencode($className); $route = new Route(); $route->pattern = '/someprefix'; $cacheItem = $this->createMock(CacheItemInterface::class); $cacheItem ->method('isHit') ->willReturn(true); $cacheItem ->method('get') ->willReturn([$route]); $timeCacheItem = $this->createMock(CacheItemInterface::class); $timeCacheItem ->method('isHit') ->willReturn(true); $timeCacheItem ->method('get') ->willReturn($lastCacheModification); $cache = $this->createMock(CacheItemPoolInterface::class); $cache ->method('getItem') ->willReturnMap([ [$cacheKey, $cacheItem], ['[C]' . $cacheKey, $timeCacheItem], ]); $cache->expects(self::never())->method('save'); $cache->expects(self::never())->method('commit'); $reader = new PsrCachedReader(new AnnotationReader(), $cache, true); $this->assertEquals([$route], $reader->getClassAnnotations(new ReflectionClass($className))); } protected function getReader(): Reader { $this->cache = new ArrayAdapter(); return new PsrCachedReader(new AnnotationReader(), $this->cache); } /** @param mixed $value */ private function createCacheItem(string $key, bool $isHit, $value = null): CacheItemInterface { return Closure::bind( static function (string $key, $value, bool $isHit): CacheItem { $item = new CacheItem(); $item->key = $key; $item->value = $value; $item->isHit = $isHit; return $item; }, null, CacheItem::class )($key, $value, $isHit); } } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/ReservedKeywordsClasses.php ================================================ getClassAnnotations($class); self::assertCount(1, $annots); self::assertInstanceOf(DCOM141Annotation::class, $annots[0]); self::assertEquals('SimpleXMLElement', $annots[0]->classPath); } public function testAnnotationNotPrefixed(): void { $class = new ReflectionClass(DCOM141ConsumerNotPrefixed::class); $reader = new AnnotationReader(); $annots = $reader->getClassAnnotations($class); self::assertCount(1, $annots); self::assertInstanceOf(DCOM141Annotation::class, $annots[0]); self::assertEquals('SimpleXMLElement', $annots[0]->classPath); } } /** * @Annotation */ class DCOM141Annotation { /** @var mixed */ public $classPath; } /** * @DCOM141Annotation(SimpleXMLElement::class) */ class DCOM141ConsumerPrefixed { } /** * @DCOM141Annotation(SimpleXMLElement::class) */ class DCOM141ConsumerNotPrefixed { } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM55Test.php ================================================ expectException(AnnotationException::class); $this->expectExceptionMessage( '[Semantical Error] The class "Doctrine\Tests\Common\Annotations\Fixtures\Controller"' . ' is not annotated with @Annotation. Are you sure this class can be used as annotation? If so, then you need to add @Annotation to the _class_ doc comment of' . ' "Doctrine\Tests\Common\Annotations\Fixtures\Controller". If it is indeed no annotation, then you need to add @IgnoreAnnotation("Controller")' . ' to the _class_ doc comment of class Doctrine\Tests\Common\Annotations\Ticket\Dummy.' ); $reader->getClassAnnotations($class); } public function testAnnotation(): void { $class = new ReflectionClass(__NAMESPACE__ . '\\DCOM55Consumer'); $reader = new AnnotationReader(); $annots = $reader->getClassAnnotations($class); self::assertCount(1, $annots); self::assertInstanceOf(__NAMESPACE__ . '\\DCOM55Annotation', $annots[0]); } public function testParseAnnotationDocblocks(): void { $class = new ReflectionClass(__NAMESPACE__ . '\\DCOM55Annotation'); $reader = new AnnotationReader(); $annots = $reader->getClassAnnotations($class); self::assertEmpty($annots); } } /** * @Controller */ class Dummy { } /** * @Annotation */ class DCOM55Annotation { } /** * @DCOM55Annotation */ class DCOM55Consumer { } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/Ticket/DCOM58Entity.php ================================================ getClassAnnotations(new ReflectionClass(__NAMESPACE__ . '\MappedClass')); $classAnnotations = array_combine( array_map('get_class', $result), $result ); self::assertArrayNotHasKey('', $classAnnotations, 'Class "xxx" is not a valid entity or mapped super class.'); } public function testIssueGlobalNamespace(): void { $docblock = '@Entity'; $parser = new DocParser(); $parser->setImports(['__NAMESPACE__' => 'Doctrine\Tests\Common\Annotations\Ticket\Doctrine\ORM\Mapping']); $annots = $parser->parse($docblock); self::assertCount(1, $annots); self::assertInstanceOf(Doctrine\ORM\Mapping\Entity::class, $annots[0]); } public function testIssueNamespaces(): void { $docblock = '@Entity'; $parser = new DocParser(); $parser->addNamespace('Doctrine\Tests\Common\Annotations\Ticket\Doctrine\ORM'); $annots = $parser->parse($docblock); self::assertCount(1, $annots); self::assertInstanceOf(Doctrine\ORM\Entity::class, $annots[0]); } public function testIssueMultipleNamespaces(): void { $docblock = '@Entity'; $parser = new DocParser(); $parser->addNamespace('Doctrine\Tests\Common\Annotations\Ticket\Doctrine\ORM\Mapping'); $parser->addNamespace('Doctrine\Tests\Common\Annotations\Ticket\Doctrine\ORM'); $annots = $parser->parse($docblock); self::assertCount(1, $annots); self::assertInstanceOf(Doctrine\ORM\Mapping\Entity::class, $annots[0]); } public function testIssueWithNamespacesOrImports(): void { $docblock = '@Entity'; $parser = new DocParser(); $annots = $parser->parse($docblock); self::assertCount(1, $annots); self::assertInstanceOf(\Entity::class, $annots[0]); } } /** * @Entity */ class MappedClass { } namespace Doctrine\Tests\Common\Annotations\Ticket\Doctrine\ORM\Mapping; /** * @Annotation */ class Entity { } namespace Doctrine\Tests\Common\Annotations\Ticket\Doctrine\ORM; /** * @Annotation */ class Entity { } ================================================ FILE: tests/Doctrine/Tests/Common/Annotations/TopLevelAnnotation.php ================================================