DoctrineHelper::createSchemaManager()   A
last analyzed

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\Schema\AbstractSchemaManager;
9
use Doctrine\DBAL\Schema\Comparator;
10
use Doctrine\DBAL\Schema\Schema;
11
use Doctrine\DBAL\Types\Types;
12
use InvalidArgumentException;
13
14
/**
15
 * @see \DH\Auditor\Tests\Provider\Doctrine\Persistence\Helper\DoctrineHelperTest
16
 *
17
 * @internal
18
 */
19
final class DoctrineHelper
20
{
21
    /**
22
     * Gets the real class name of a class name that could be a proxy.
23
     *
24
     * @param object|string $subject
25
     *
26
     * credits
27
     * https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php
28
     */
29
    public static function getRealClassName(object|string $subject): string
30
    {
31
        $subject = \is_object($subject) ? $subject::class : $subject;
32
33
        // __CG__: Doctrine Common Marker for Proxy (ODM < 2.0 and ORM < 3.0)
34
        // __PM__: Ocramius Proxy Manager (ODM >= 2.0)
35
        $positionCg = mb_strrpos($subject, '\\__CG__\\');
36
        $positionPm = mb_strrpos($subject, '\\__PM__\\');
37
        if (false === $positionCg && false === $positionPm) {
38
            return $subject;
39
        }
40
41
        if (false !== $positionCg) {
42
            return mb_substr($subject, $positionCg + 8);
43
        }
44
45
        $className = ltrim($subject, '\\');
46
47
        return mb_substr(
48
            $className,
49
            8 + $positionPm,
50
            mb_strrpos($className, '\\') - ($positionPm + 8)
51
        );
52
    }
53
54
    public static function getDoctrineType(string $type): string
55
    {
56
        if (!\defined(Types::class.'::'.$type)) {
57
            throw new InvalidArgumentException(sprintf('Undefined Doctrine type "%s"', $type));
58
        }
59
60
        \assert(\is_string(\constant(Types::class.'::'.$type)));
61
62
        return \constant(Types::class.'::'.$type);
63
    }
64
65
    /**
66
     * TODO: remove this method when we drop support of doctrine/dbal 2.13.x.
67
     *
68
     * @throws \Doctrine\DBAL\Exception
69
     */
70
    public static function createSchemaManager(Connection $connection): AbstractSchemaManager
71
    {
72
        return method_exists($connection, 'createSchemaManager')
73
            ? $connection->createSchemaManager()
74
            : $connection->getSchemaManager(); // @phpstan-ignore-line
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

74
            : /** @scrutinizer ignore-deprecated */ $connection->getSchemaManager(); // @phpstan-ignore-line

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...
75
    }
76
77
    /**
78
     * TODO: remove this method when we drop support of doctrine/dbal 2.13.x.
79
     *
80
     * @throws \Doctrine\DBAL\Exception
81
     */
82
    public static function introspectSchema(AbstractSchemaManager $schemaManager): Schema
83
    {
84
        return method_exists($schemaManager, 'introspectSchema')
85
            ? $schemaManager->introspectSchema()
86
            : $schemaManager->createSchema(); // @phpstan-ignore-line
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

86
            : /** @scrutinizer ignore-deprecated */ $schemaManager->createSchema(); // @phpstan-ignore-line

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...
87
    }
88
89
    /**
90
     * TODO: remove this method when we drop support of doctrine/dbal 2.13.x.
91
     *
92
     * @return array<string>
93
     *
94
     * @throws \Doctrine\DBAL\Exception
95
     */
96
    public static function getMigrateToSql(Connection $connection, Schema $fromSchema, Schema $toSchema): array
97
    {
98
        $schemaComparator = new Comparator();
99
        $platform = $connection->getDatabasePlatform();
100
101
        if (method_exists($platform, 'getAlterSchemaSQL')) {
102
            return $platform->getAlterSchemaSQL(
103
                $schemaComparator->compareSchemas($fromSchema, $toSchema)
104
            );
105
        }
106
107
        return $fromSchema->getMigrateToSql($toSchema, $platform); // @phpstan-ignore-line
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

107
        return /** @scrutinizer ignore-deprecated */ $fromSchema->getMigrateToSql($toSchema, $platform); // @phpstan-ignore-line
Loading history...
108
    }
109
}
110