Passed
Pull Request — master (#124)
by
unknown
02:01
created

getEntityManagerFromOnFlushEventArgs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

80
            /** @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

80
            /** @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...
81
        }
82
    }
83
84
    /**
85
     * @param QueryBuilder|Statement $statement
86
     *
87
     * @throws \Doctrine\DBAL\Exception
88
     */
89
    public static function executeQuery($statement): void
90
    {
91
        if (method_exists($statement, 'executeQuery')) {
92
            $statement->executeQuery();
93
        } else {
94
            $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

94
            /** @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

94
            /** @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...
95
        }
96
    }
97
98
    public static function createSchemaManager(Connection $connection): AbstractSchemaManager
99
    {
100
        return method_exists($connection, 'createSchemaManager')
101
            ? $connection->createSchemaManager()
102
            : $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

102
            : /** @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...
103
    }
104
105
    public static function introspectSchema(AbstractSchemaManager $schemaManager): Schema
106
    {
107
        return method_exists($schemaManager, 'introspectSchema')
108
            ? $schemaManager->introspectSchema()
109
            : $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

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

128
        return /** @scrutinizer ignore-deprecated */ $fromSchema->getMigrateToSql($toSchema, $platform);
Loading history...
129
    }
130
131
    public static function createAttributeMetadataConfiguration(array $paths, bool $isDevMode = false): Configuration
132
    {
133
        if (class_exists(ORMSetup::class)) {
134
            return ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode);
135
        }
136
137
        return Setup::createAttributeMetadataConfiguration($paths, $isDevMode);
138
    }
139
140
    public static function createAnnotationMetadataConfiguration(array $paths, bool $isDevMode = false): Configuration
141
    {
142
        if (class_exists(ORMSetup::class)) {
143
            return ORMSetup::createAnnotationMetadataConfiguration($paths, $isDevMode);
144
        }
145
146
        return Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
147
    }
148
149
    public static function getEntityManagerFromOnFlushEventArgs(OnFlushEventArgs $args): EntityManagerInterface
150
    {
151
        return method_exists($args, '') ? $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

151
        return method_exists($args, '') ? $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...
152
    }
153
}
154