Doctrine Annotations
Docblock Annotations Parser library (extracted from Doctrine Common).
Documentation
See the doctrine-project website.
Contributing
When making a pull request, make sure your changes follow the Coding Standard Guidelines.
Category: PHP / Miscellaneous |
Watchers: 39 |
Star: 6.3k |
Fork: 220 |
Last update: Nov 2, 2021 |
Docblock Annotations Parser library (extracted from Doctrine Common).
See the doctrine-project website.
When making a pull request, make sure your changes follow the Coding Standard Guidelines.
Doctrine\Annotations
(Removing Common
)hoa/compiler
instead of doctrine/lexer
( see : grammar )AnnotationRegistry
and all autoload magicAttribute/Attributes
annotationsSimpleAnnotationReader
FileCacheReader
IndexedReader
TODO:
Local reviews (checkout + run locally):
Fixes #379
This PR enables annotations to provide a constructor to be called with named arguments without requiring to implement a marker interface.
When I use the new PHP7 grouped use statements feature, Doctrine doesn't use the namespace to import the annotation.
For example, this works:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
class MyController
{
/**
* @Route("/example")
* @Method("GET")
*/
public function exampleAction() {...}
}
But changing the namespaces to this fails:
use Sensio\Bundle\FrameworkExtraBundle\Configuration\{
Route, Method
}
I'm using Doctrine Annotations 1.2.7
Drupal developers are much to used to the PHP array syntax which allows a dangling comma:
array(
'foo' => 'bar',
);
Doctrine doesn't allow for this and this causes a lot of grief. This PR fixes this. It seems {}
arrays already support this.
This stopped working on php 7.4, for example on DocParser class See https://twitter.com/nikita_ppv/status/1148931277216800769 for a reference
Don't know if I do not understand correctly how annotation parameters work, but given the following annotation definition:
/**
* Class Parameter
*
* @Annotation
* @Target("METHOD")
*/
class Parameter
{
/**
* @var string
*/
public $name;
/**
* @var array<Symfony\Component\Validator\Constraint>
*/
public $constraints = array();
/**
* @var string
*/
public $validationGroup;
/**
* @param array $data
*/
public function __construct(array $data)
{
// constructor logic
}
}
Why doesn't the following work:
@Direct\Parameter("a", { @Assert\NotNull() }, "myGroup")
I'm getting a Expected Value, got 'myGroup' at position 70 in method...
exception.
This however works:
@Direct\Parameter("a", { @Assert\NotNull() }, validationGroup="myGroup")
as does this:
@Direct\Parameter(name="a", constraints={ @Assert\NotNull() }, validationGroup="myGroup")
and this:
@Direct\Parameter("a", { @Assert\NotNull() })
When an annotation class is instantiated and values are passed to the constructor, TypeErrors and ArgumentCountErrors can be thrown. This change catches those exceptions and shows an AnnotationException containing the annotation name and context.
Resolves: #437
I ran into this error:
In OneToMany.php line 45:
[TypeError]
Argument 3 passed to Doctrine\ORM\Mapping\OneToMany::__construct() must be of the type array or null,
string given, called in vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php on line 971
Exception trace:
at vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/OneToMany.php:45
Doctrine\ORM\Mapping\OneToMany->__construct() at vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php:971
Doctrine\Common\Annotations\DocParser->Annotation() at vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php:719
Doctrine\Common\Annotations\DocParser->Annotations() at vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php:376
Doctrine\Common\Annotations\DocParser->parse() at vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationReader.php:178
Doctrine\Common\Annotations\AnnotationReader->getPropertyAnnotations() at vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/PsrCachedReader.php:155
I traced calls and variables back to a @OneToMany
annotation on a property of an entity containing a misconfiguration and fixed it:
/**
* @var Collection<int, Page>|null
- * @ORM\OneToMany(targetEntity="App\Entity\Page", mappedBy="user", cascade="persist")
+ * @ORM\OneToMany(targetEntity="App\Entity\Page", mappedBy="user", cascade={"persist"})
*/
private ?Collection $pages;
It's not clear from the error neither the trace (coming from a query being executed and objects being hydrated) on which entity and which property this occurred.
I looked into \Doctrine\Common\Annotations\DocParser
and there are many checks being done and many places where there might be a \Doctrine\Common\Annotations\AnnotationException
being thrown with clear messages.
Should we catch TypeError
s upon the annotation instantiation here and here and throw an AnnotationException
with a proper message containing the annotation and property on which it occurred?
doctrine/annotations
handles reading annotations from eval’ed code (as in mock objects) just fine, except that it attempts to determine the modification time via ReflectionClass::getFile()
which will return the file and line where the eval()
happened. This change adds fix and test for the PsrCachedReader
and also fixes CachedReader
but without a tests since it’s deprecated.
Fixes #186
Protobuf protoc compiler generates classes with @type annotation, and parser throw exception on this annotation.
Use case:
Framework: Symfony Component: symfony/messenger
Dispatch protobuf-generated message class through messenger and get exception on message validation step (when validator parse class through doctrine/annotation)
The lib is used within TYPO3 for annotation evaluation. We stumbled over a case that caused the lib to not parse the annotation at all:
class ThingModel
{
...
/** @var ThingModel|null|bool */
protected $related;
...
}
The response of the annotation reader is that there are zero annotations present for $related
.
I played around with several variations and it turned out that have two of the three types above as annotation works flawlessly, but having all three (or any other three) types causes the reader to hick-up upon this annotation.
Maybe the parser can be improved to handle such a case?
Hey there!
I'd like to report a security issue but cannot find contact instructions on your repository.
If not a hassle, might you kindly add a SECURITY.md
file with an email, or another contact method? GitHub recommends this best practice to ensure security issues are responsibly disclosed, and it would serve as a simple instruction for security researchers in the future.
Thank you for your consideration, and I look forward to hearing from you!
(cc @huntr-helper)
Total issues resolved: 0
Total pull requests resolved: 2
Total contributors: 2
422: Add psalm annotations to ignored annotation name thanks to @azjezz
Feature release (minor)
noRector
annotation. thanks to @Aerendir1.11.x bugfix release (patch)