Passed
Push — master ( 2d4cfa...2ed3af )
by Damien
02:11
created

DoctrineHelper::getRealClassName()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 20
c 0
b 0
f 0
rs 9.5555
cc 5
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Auditor\Provider\Doctrine\Persistence\Helper;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Query\QueryBuilder;
9
use Doctrine\DBAL\Result;
10
use Doctrine\DBAL\Schema\AbstractSchemaManager;
11
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...
12
use Doctrine\DBAL\Schema\Schema;
13
use Doctrine\DBAL\Statement;
14
use Doctrine\DBAL\Types\Types;
15
use Doctrine\ORM\Configuration;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Doctrine\ORM\Event\OnFlushEventArgs;
18
use Doctrine\ORM\ORMSetup;
19
use Doctrine\ORM\Tools\Setup;
20
use InvalidArgumentException;
21
22
/**
23
 * @see \DH\Auditor\Tests\Provider\Doctrine\Persistence\Helper\DoctrineHelperTest
24
 *
25
 * @internal
26
 */
27
final class DoctrineHelper
28
{
29
    /**
30
     * Gets the real class name of a class name that could be a proxy.
31
     *
32
     * @param object|string $subject
33
     *
34
     * @return string
35
     *
36
     * credits
37
     * https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php
38
     */
39
    public static function getRealClassName($subject): string
40
    {
41
        $subject = \is_object($subject) ? \get_class($subject) : $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
        if (false !== $positionCg) {
51
            return mb_substr($subject, $positionCg + 8);
52
        }
53
        $className = ltrim($subject, '\\');
54
55
        return mb_substr(
56
            $className,
57
            8 + $positionPm,
58
            mb_strrpos($className, '\\') - ($positionPm + 8)
59
        );
60
    }
61
62
    public static function getDoctrineType(string $type): string
63
    {
64
        if (!\defined(Types::class.'::'.$type)) {
65
            throw new InvalidArgumentException(sprintf('Undefined Doctrine type "%s"', $type));
66
        }
67
68
        \assert(\is_string(\constant(Types::class.'::'.$type)));
69
70
        return \constant(Types::class.'::'.$type);
71
    }
72
73
    /**
74
     * @param QueryBuilder|Statement $statement
75
     *
76
     * @throws \Doctrine\DBAL\Exception
77
     */
78
    public static function executeStatement($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
     * @param QueryBuilder|Statement $statement
89
     *
90
     * @return int|Result|string
91
     *
92
     * @throws \Doctrine\DBAL\Exception
93
     */
94
    public static function executeQuery($statement)
95
    {
96
        if (method_exists($statement, 'executeQuery')) {
97
            return $statement->executeQuery();
98
        }
99
100
        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

100
        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

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

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

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

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

158
        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...
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...
159
    }
160
}
161