Passed
Push — cleanup ( c6a00d )
by Damien
02:31
created

createAnnotationMetadataConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Auditor\Provider\Doctrine\Persistence\Helper;
6
7
use Composer\Autoload\ClassLoader;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\DBAL\Query\QueryBuilder;
10
use Doctrine\DBAL\Result;
11
use Doctrine\DBAL\Schema\AbstractSchemaManager;
12
use Doctrine\DBAL\Schema\Comparator;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Schema\Comparator 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...
13
use Doctrine\DBAL\Schema\Schema;
14
use Doctrine\DBAL\Statement;
15
use Doctrine\DBAL\Types\Types;
16
use Doctrine\ORM\Configuration;
17
use Doctrine\ORM\EntityManagerInterface;
18
use Doctrine\ORM\Event\OnFlushEventArgs;
19
use Doctrine\ORM\ORMSetup;
20
use Doctrine\ORM\Tools\Setup;
21
use InvalidArgumentException;
22
use ReflectionClass;
23
24
/**
25
 * @see \DH\Auditor\Tests\Provider\Doctrine\Persistence\Helper\DoctrineHelperTest
26
 *
27
 * @internal
28
 */
29
final class DoctrineHelper
30
{
31
    /**
32
     * Gets the real class name of a class name that could be a proxy.
33
     *
34
     * @param object|string $subject
35
     *
36
     * credits
37
     * https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php
38
     */
39
    public static function getRealClassName(object|string $subject): string
40
    {
41
        $subject = \is_object($subject) ? $subject::class : $subject;
42
43
        // __CG__: Doctrine Common Marker for Proxy (ODM < 2.0 and ORM < 3.0)
44
        // __PM__: Ocramius Proxy Manager (ODM >= 2.0)
45
        $positionCg = mb_strrpos($subject, '\\__CG__\\');
46
        $positionPm = mb_strrpos($subject, '\\__PM__\\');
47
        if (false === $positionCg && false === $positionPm) {
48
            return $subject;
49
        }
50
51
        if (false !== $positionCg) {
52
            return mb_substr($subject, $positionCg + 8);
53
        }
54
55
        $className = ltrim($subject, '\\');
56
57
        return mb_substr(
58
            $className,
59
            8 + $positionPm,
60
            mb_strrpos($className, '\\') - ($positionPm + 8)
61
        );
62
    }
63
64
    public static function getDoctrineType(string $type): string
65
    {
66
        if (!\defined(Types::class.'::'.$type)) {
67
            throw new InvalidArgumentException(sprintf('Undefined Doctrine type "%s"', $type));
68
        }
69
70
        \assert(\is_string(\constant(Types::class.'::'.$type)));
71
72
        return \constant(Types::class.'::'.$type);
73
    }
74
75
    /**
76
     * @throws \Doctrine\DBAL\Exception
77
     */
78
    public static function executeStatement(QueryBuilder|Statement $statement): void
79
    {
80
        if (method_exists($statement, 'executeStatement')) {
81
            $statement->executeStatement();
82
        } else {
83
            $statement->execute();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Query\QueryBuilder::execute() has been deprecated: Use {@see executeQuery()} or {@see executeStatement()} instead. ( Ignorable by Annotation )

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

83
            /** @scrutinizer ignore-deprecated */ $statement->execute();

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...
Deprecated Code introduced by
The function Doctrine\DBAL\Statement::execute() has been deprecated: Statement::execute() is deprecated, use Statement::executeQuery() or executeStatement() instead ( Ignorable by Annotation )

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

83
            /** @scrutinizer ignore-deprecated */ $statement->execute();

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...
84
        }
85
    }
86
87
    /**
88
     * @throws \Doctrine\DBAL\Exception
89
     */
90
    public static function executeQuery(QueryBuilder|Statement $statement): int|Result|string
91
    {
92
        if (method_exists($statement, 'executeQuery')) {
93
            return $statement->executeQuery();
94
        }
95
96
        return $statement->execute();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Query\QueryBuilder::execute() has been deprecated: Use {@see executeQuery()} or {@see executeStatement()} instead. ( Ignorable by Annotation )

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

96
        return /** @scrutinizer ignore-deprecated */ $statement->execute();

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...
Deprecated Code introduced by
The function Doctrine\DBAL\Statement::execute() has been deprecated: Statement::execute() is deprecated, use Statement::executeQuery() or executeStatement() instead ( Ignorable by Annotation )

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

96
        return /** @scrutinizer ignore-deprecated */ $statement->execute();

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...
97
    }
98
99
    public static function createSchemaManager(Connection $connection): AbstractSchemaManager
100
    {
101
        return method_exists($connection, 'createSchemaManager')
102
            ? $connection->createSchemaManager()
103
            : $connection->getSchemaManager();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::getSchemaManager() has been deprecated: Use {@see createSchemaManager()} instead. ( Ignorable by Annotation )

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

103
            : /** @scrutinizer ignore-deprecated */ $connection->getSchemaManager();

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...
104
    }
105
106
    public static function introspectSchema(AbstractSchemaManager $schemaManager): Schema
107
    {
108
        return method_exists($schemaManager, 'introspectSchema')
109
            ? $schemaManager->introspectSchema()
110
            : $schemaManager->createSchema();
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Abs...Manager::createSchema() has been deprecated: Use {@link introspectSchema()} instead. ( Ignorable by Annotation )

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

110
            : /** @scrutinizer ignore-deprecated */ $schemaManager->createSchema();

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...
111
    }
112
113
    /**
114
     * @return array<string>
115
     *
116
     * @throws \Doctrine\DBAL\Exception
117
     */
118
    public static function getMigrateToSql(Connection $connection, Schema $fromSchema, Schema $toSchema): array
119
    {
120
        $schemaComparator = new Comparator();
121
        $platform = $connection->getDatabasePlatform();
122
123
        if (method_exists($platform, 'getAlterSchemaSQL')) {
124
            return $platform->getAlterSchemaSQL(
125
                $schemaComparator->compareSchemas($fromSchema, $toSchema)
126
            );
127
        }
128
129
        return $fromSchema->getMigrateToSql($toSchema, $platform);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Schema::getMigrateToSql() has been deprecated. ( Ignorable by Annotation )

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

129
        return /** @scrutinizer ignore-deprecated */ $fromSchema->getMigrateToSql($toSchema, $platform);
Loading history...
130
    }
131
132
    public static function createAttributeMetadataConfiguration(array $paths, bool $isDevMode = false): Configuration
133
    {
134
        if (class_exists(ORMSetup::class)) {
135
            return ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
136
        }
137
138
        return Setup::createAttributeMetadataConfiguration($paths, $isDevMode);
139
    }
140
141
    public static function getEntityManagerFromOnFlushEventArgs(OnFlushEventArgs $args): EntityManagerInterface
142
    {
143
        return method_exists($args, 'getObjectManager') ? $args->getObjectManager() : $args->getEntityManager();
0 ignored issues
show
Bug Best Practice introduced by
The expression return method_exists($ar...rgs->getEntityManager() could return the type Doctrine\Persistence\ObjectManager which includes types incompatible with the type-hinted return Doctrine\ORM\EntityManagerInterface. Consider adding an additional type-check to rule them out.
Loading history...
Deprecated Code introduced by
The function Doctrine\ORM\Event\OnFlu...rgs::getEntityManager() has been deprecated: 2.13. Use {@see getObjectManager} instead. ( Ignorable by Annotation )

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

143
        return method_exists($args, 'getObjectManager') ? $args->getObjectManager() : /** @scrutinizer ignore-deprecated */ $args->getEntityManager();

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...
144
    }
145
146
    public static function getVendorDir(): string
147
    {
148
        $reflection = new ReflectionClass(ClassLoader::class);
149
        $filename = $reflection->getFileName();
150
        \assert(\is_string($filename));
151
152
        return \dirname($filename, 2);
153
    }
154
}
155