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

DoctrineHelper::createSchemaManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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

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

97
        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

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

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

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

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

155
        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...
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...
156
    }
157
}
158