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

DoctrineHelper::executeQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

99
        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

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

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

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

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

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