Issues (291)

src/Utils/Annotation/AnnotationParser.php (4 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace TheCodingMachine\TDBM\Utils\Annotation;
6
7
use Doctrine\Common\Annotations\AnnotationRegistry;
8
use Doctrine\Common\Annotations\DocParser;
0 ignored issues
show
The type Doctrine\Common\Annotations\DocParser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Doctrine\DBAL\Schema\Column;
10
use Doctrine\DBAL\Schema\Table;
11
12
use function array_map;
13
use function explode;
14
use function implode;
15
16
/**
17
 * Parses annotations in database columns.
18
 */
19
class AnnotationParser
20
{
21
    /**
22
     * @var DocParser
23
     */
24
    private $docParser;
25
26
    /**
27
     * AnnotationParser constructor.
28
     * @param array<string, class-string> $annotations An array mapping the annotation name to the fully qualified class name
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string>.
Loading history...
29
     */
30
    public function __construct(array $annotations)
31
    {
32
        $this->docParser = new DocParser();
33
        $this->docParser->setImports(array_change_key_case($annotations, \CASE_LOWER));
34
    }
35
36
    /**
37
     * @param array<string,class-string> $additionalAnnotations An array associating the name of the annotation in DB comments to the name of a fully qualified Doctrine annotation class
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string,class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string,class-string>.
Loading history...
38
     */
39
    public static function buildWithDefaultAnnotations(array $additionalAnnotations): self
40
    {
41
        $defaultAnnotations = [
42
            'UUID' => UUID::class,
43
            'Autoincrement' => Autoincrement::class,
44
            'Bean' => Bean::class,
45
            'ProtectedGetter' => ProtectedGetter::class,
46
            'ProtectedSetter' => ProtectedSetter::class,
47
            'ProtectedOneToMany' => ProtectedOneToMany::class,
48
            'ReadOnly' => ReadOnlyColumn::class,
49
            'JsonKey' => JsonKey::class,
50
            'JsonIgnore' => JsonIgnore::class,
51
            'JsonInclude' => JsonInclude::class,
52
            'JsonRecursive' => JsonRecursive::class,
53
            'JsonCollection' => JsonCollection::class,
54
            'JsonFormat' => JsonFormat::class,
55
            'AddInterface' => AddInterface::class,
56
            'AddInterfaceOnDao' => AddInterfaceOnDao::class,
57
            'AddTrait' => AddTrait::class,
58
            'AddTraitOnDao' => AddTraitOnDao::class,
59
        ];
60
        $annotations = array_merge($defaultAnnotations, $additionalAnnotations);
61
62
        return new self($annotations);
63
    }
64
65
    /**
66
     * Parses the doc comment and initializes all the annotations.
67
     */
68
    private function parse(string $comment, string $context): Annotations
69
    {
70
        AnnotationRegistry::registerUniqueLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...:registerUniqueLoader() has been deprecated: This method is deprecated and will be removed in doctrine/annotations 2.0. Annotations will be autoloaded in 2.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerUniqueLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
71
72
        // compatibility with UUID annotation from TDBM 5.0
73
        $comment = \str_replace(['@UUID v1', '@UUID v4'], ['@UUID("v1")', '@UUID("v4")'], $comment);
74
75
        // Let's add * in front of the line (otherwise, parsing is failing)
76
        $lines = explode("\n", $comment);
77
        $lines = array_map(function (string $line) {
78
            return '* '.$line;
79
        }, $lines);
80
        $comment = implode("\n", $lines);
81
82
        $annotations = $this->docParser->parse($comment, $context);
83
84
        return new Annotations($annotations);
85
    }
86
87
    public function getTableAnnotations(Table $table): Annotations
88
    {
89
        $options = $table->getOptions();
90
        if (isset($options['comment'])) {
91
            return $this->parse($options['comment'], ' comment in table '.$table->getName());
92
        }
93
        return new Annotations([]);
94
    }
95
96
    public function getColumnAnnotations(Column $column, Table $table): Annotations
97
    {
98
        $comment = $column->getComment();
99
        if ($comment === null) {
100
            return new Annotations([]);
101
        }
102
        return $this->parse($comment, sprintf('comment of column %s in table %s', $column->getName(), $table->getName()));
103
    }
104
}
105