Passed
Push — master ( 2861bd...85f9ff )
by Damien
02:01
created

DoctrineHelper   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
eloc 48
c 1
b 0
f 0
dl 0
loc 141
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A createAttributeMetadataConfiguration() 0 7 2
A getRealClassName() 0 20 5
A getMigrateToSql() 0 12 2
A getDoctrineType() 0 9 2
A createAnnotationMetadataConfiguration() 0 9 2
A createSchemaManager() 0 5 2
A getEntityManagerFromOnFlushEventArgs() 0 3 2
A executeQuery() 0 7 2
A executeStatement() 0 6 2
A introspectSchema() 0 5 2
A getVendorDir() 0 7 1
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
     * @return string
37
     *
38
     * credits
39
     * https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php
40
     */
41
    public static function getRealClassName($subject): string
42
    {
43
        $subject = \is_object($subject) ? \get_class($subject) : $subject;
44
45
        // __CG__: Doctrine Common Marker for Proxy (ODM < 2.0 and ORM < 3.0)
46
        // __PM__: Ocramius Proxy Manager (ODM >= 2.0)
47
        $positionCg = mb_strrpos($subject, '\\__CG__\\');
48
        $positionPm = mb_strrpos($subject, '\\__PM__\\');
49
        if (false === $positionCg && false === $positionPm) {
50
            return $subject;
51
        }
52
        if (false !== $positionCg) {
53
            return mb_substr($subject, $positionCg + 8);
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
     * @param QueryBuilder|Statement $statement
77
     *
78
     * @throws \Doctrine\DBAL\Exception
79
     */
80
    public static function executeStatement($statement): void
81
    {
82
        if (method_exists($statement, 'executeStatement')) {
83
            $statement->executeStatement();
84
        } else {
85
            $statement->execute();
0 ignored issues
show
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

85
            /** @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\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

85
            /** @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...
86
        }
87
    }
88
89
    /**
90
     * @param QueryBuilder|Statement $statement
91
     *
92
     * @return int|Result|string
93
     *
94
     * @throws \Doctrine\DBAL\Exception
95
     */
96
    public static function executeQuery($statement)
97
    {
98
        if (method_exists($statement, 'executeQuery')) {
99
            return $statement->executeQuery();
100
        }
101
102
        return $statement->execute();
0 ignored issues
show
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

102
        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\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

102
        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...
103
    }
104
105
    public static function createSchemaManager(Connection $connection): AbstractSchemaManager
106
    {
107
        return method_exists($connection, 'createSchemaManager')
108
            ? $connection->createSchemaManager()
109
            : $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

109
            : /** @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...
110
    }
111
112
    public static function introspectSchema(AbstractSchemaManager $schemaManager): Schema
113
    {
114
        return method_exists($schemaManager, 'introspectSchema')
115
            ? $schemaManager->introspectSchema()
116
            : $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

116
            : /** @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...
117
    }
118
119
    /**
120
     * @return array<string>
121
     *
122
     * @throws \Doctrine\DBAL\Exception
123
     */
124
    public static function getMigrateToSql(Connection $connection, Schema $fromSchema, Schema $toSchema): array
125
    {
126
        $schemaComparator = new Comparator();
127
        $platform = $connection->getDatabasePlatform();
128
129
        if (method_exists($platform, 'getAlterSchemaSQL')) {
130
            return $platform->getAlterSchemaSQL(
131
                $schemaComparator->compareSchemas($fromSchema, $toSchema)
132
            );
133
        }
134
135
        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

135
        return /** @scrutinizer ignore-deprecated */ $fromSchema->getMigrateToSql($toSchema, $platform);
Loading history...
136
    }
137
138
    public static function createAttributeMetadataConfiguration(array $paths, bool $isDevMode = false): Configuration
139
    {
140
        if (class_exists(ORMSetup::class)) {
141
            return ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
142
        }
143
144
        return Setup::createAttributeMetadataConfiguration($paths, $isDevMode);
145
    }
146
147
    public static function createAnnotationMetadataConfiguration(array $paths, bool $isDevMode = false): Configuration
148
    {
149
        if (class_exists(ORMSetup::class)) {
150
            require_once self::getVendorDir().'/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php';
151
152
            return ORMSetup::createAnnotationMetadataConfiguration($paths, $isDevMode);
153
        }
154
155
        return Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
156
    }
157
158
    public static function getEntityManagerFromOnFlushEventArgs(OnFlushEventArgs $args): EntityManagerInterface
159
    {
160
        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

160
        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...
161
    }
162
163
    public static function getVendorDir(): string
164
    {
165
        $reflection = new ReflectionClass(ClassLoader::class);
166
        $filename = $reflection->getFileName();
167
        \assert(\is_string($filename));
168
169
        return \dirname($filename, 2);
170
    }
171
}
172