ManyToOneBench   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 32
eloc 71
c 1
b 0
f 0
dl 0
loc 147
rs 9.84

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getTdbmService() 0 3 1
A getCache() 0 6 2
A getConfiguration() 0 6 2
B getConnection() 0 9 7
A initSchema() 0 34 4
A generateDaosAndBeans() 0 11 1
A recursiveDelete() 0 13 4
B initDatabase() 0 15 8
A benchManyToOne() 0 6 2
A createConfiguration() 0 5 1
1
<?php
2
3
namespace TheCodingMachine\TDBM\Performance;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\Common\Cache\VoidCache;
7
use Doctrine\DBAL\Connection;
8
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
9
use PhpBench\Benchmark\Metadata\Annotations\Iterations;
10
use TheCodingMachine\FluidSchema\TdbmFluidSchema;
11
use TheCodingMachine\TDBM\Configuration;
12
use TheCodingMachine\TDBM\ConfigurationInterface;
13
use TheCodingMachine\TDBM\ConnectionFactory;
14
use TheCodingMachine\TDBM\DummyGeneratorListener;
15
use TheCodingMachine\TDBM\SchemaLockFileDumper;
16
use TheCodingMachine\TDBM\TDBMAbstractServiceTest;
17
use TheCodingMachine\TDBM\TDBMSchemaAnalyzer;
18
use TheCodingMachine\TDBM\TDBMService;
19
use TheCodingMachine\TDBM\Test\Dao\UserDao;
0 ignored issues
show
Bug introduced by
The type TheCodingMachine\TDBM\Test\Dao\UserDao 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...
20
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
21
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator;
22
23
use function dirname;
24
use function getenv;
25
use function glob;
26
use function is_dir;
27
use function is_file;
28
use function rmdir;
29
use function rtrim;
30
use function unlink;
31
32
/**
33
 * @BeforeClassMethods({"initDatabase"})
34
 */
35
class ManyToOneBench
36
{
37
    public static function initDatabase(): void
38
    {
39
        $dbConnection = ConnectionFactory::resetDatabase(
40
            getenv('DB_DRIVER') ?: null,
0 ignored issues
show
Bug introduced by
It seems like getenv('DB_DRIVER') ?: null can also be of type null; however, parameter $dbDriver of TheCodingMachine\TDBM\Co...actory::resetDatabase() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
            /** @scrutinizer ignore-type */ getenv('DB_DRIVER') ?: null,
Loading history...
41
            getenv('DB_HOST') ?: null,
42
            getenv('DB_PORT') ?: null,
43
            getenv('DB_USERNAME') ?: null,
44
            getenv('DB_ADMIN_USERNAME') ?: null,
45
            getenv('DB_PASSWORD') ?: null,
46
            getenv('DB_NAME') ?: null
47
        );
48
49
        self::initSchema($dbConnection);
50
51
        self::generateDaosAndBeans($dbConnection);
52
    }
53
54
    private static function initSchema(Connection $connection): void
55
    {
56
        $fromSchema = $connection->getSchemaManager()->createSchema();
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

56
        $fromSchema = /** @scrutinizer ignore-deprecated */ $connection->getSchemaManager()->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...
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

56
        $fromSchema = /** @scrutinizer ignore-deprecated */ $connection->getSchemaManager()->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...
57
        $toSchema = clone $fromSchema;
58
59
        $db = new TdbmFluidSchema($toSchema, new \TheCodingMachine\FluidSchema\DefaultNamingStrategy($connection->getDatabasePlatform()));
60
61
        $db->table('countries')
62
            ->column('id')->integer()->primaryKey()
63
            ->column('label')->string(255)->unique();
64
65
        $db->table('users')
66
            ->column('id')->integer()->primaryKey()
67
            ->column('name')->string(255)
68
            ->column('country_id')->references('countries');
69
70
        $sqlStmts = $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform());
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Schema\Schema::getMigrateFromSql() 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

70
        $sqlStmts = /** @scrutinizer ignore-deprecated */ $toSchema->getMigrateFromSql($fromSchema, $connection->getDatabasePlatform());
Loading history...
71
72
        foreach ($sqlStmts as $sqlStmt) {
73
            $connection->exec($sqlStmt);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Connection::exec() has been deprecated: please use {@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

73
            /** @scrutinizer ignore-deprecated */ $connection->exec($sqlStmt);

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...
74
        }
75
76
        for ($i = 1; $i < 200; $i++) {
77
            TDBMAbstractServiceTest::insert($connection, 'countries', [
78
                'id' => $i,
79
                'label' => 'Country '.$i,
80
            ]);
81
        }
82
83
        for ($i = 1; $i < 1000; $i++) {
84
            TDBMAbstractServiceTest::insert($connection, 'users', [
85
                'id' => $i,
86
                'name' => 'User '.$i,
87
                'country_id' => ($i % 199) + 1,
88
            ]);
89
        }
90
    }
91
92
    private static function generateDaosAndBeans(Connection $connection): void
93
    {
94
        $schemaManager = $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

94
        $schemaManager = /** @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...
95
        $schemaAnalyzer = new SchemaAnalyzer($schemaManager);
96
        $schemaLockFileDumper = new SchemaLockFileDumper($connection, new ArrayCache(), Configuration::getDefaultLockFilePath());
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

96
        $schemaLockFileDumper = new SchemaLockFileDumper($connection, /** @scrutinizer ignore-deprecated */ new ArrayCache(), Configuration::getDefaultLockFilePath());
Loading history...
97
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($connection, new ArrayCache(), $schemaAnalyzer, $schemaLockFileDumper);
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

97
        $tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($connection, /** @scrutinizer ignore-deprecated */ new ArrayCache(), $schemaAnalyzer, $schemaLockFileDumper);
Loading history...
98
        $tdbmDaoGenerator = new TDBMDaoGenerator(self::createConfiguration(), $tdbmSchemaAnalyzer);
99
        $rootPath = __DIR__ . '/../';
0 ignored issues
show
Unused Code introduced by
The assignment to $rootPath is dead and can be removed.
Loading history...
100
        self::recursiveDelete(__DIR__. '/../../src/Test/Dao/');
101
102
        $tdbmDaoGenerator->generateAllDaosAndBeans();
103
    }
104
105
    /**
106
     * Delete a file or recursively delete a directory.
107
     *
108
     * @param string $str Path to file or directory
109
     * @return bool
110
     */
111
    private static function recursiveDelete(string $str): bool
112
    {
113
        if (is_file($str)) {
114
            return @unlink($str);
115
        } elseif (is_dir($str)) {
116
            $scan = glob(rtrim($str, '/') . '/*');
117
            foreach ($scan as $index => $path) {
118
                self::recursiveDelete($path);
119
            }
120
121
            return @rmdir($str);
122
        }
123
        return false;
124
    }
125
126
    private static function getConnection(): Connection
127
    {
128
        return ConnectionFactory::createConnection(
129
            getenv('DB_DRIVER') ?: null,
0 ignored issues
show
Bug introduced by
It seems like getenv('DB_DRIVER') ?: null can also be of type null; however, parameter $dbDriver of TheCodingMachine\TDBM\Co...ory::createConnection() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
            /** @scrutinizer ignore-type */ getenv('DB_DRIVER') ?: null,
Loading history...
130
            getenv('DB_HOST') ?: null,
131
            getenv('DB_PORT') ?: null,
132
            getenv('DB_USERNAME') ?: null,
133
            getenv('DB_PASSWORD') ?: null,
134
            getenv('DB_NAME') ?: null
135
        );
136
    }
137
138
    protected function getTdbmService(): TDBMService
139
    {
140
        return new TDBMService($this->getConfiguration());
141
    }
142
143
    private static $cache;
144
145
    protected static function getCache(): ArrayCache
146
    {
147
        if (self::$cache === null) {
148
            self::$cache = new ArrayCache();
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\Common\Cache\ArrayCache has been deprecated: Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
            self::$cache = /** @scrutinizer ignore-deprecated */ new ArrayCache();
Loading history...
149
        }
150
        return self::$cache;
151
    }
152
153
    private static function createConfiguration(): ConfigurationInterface
154
    {
155
        $configuration = new Configuration('TheCodingMachine\\TDBM\\Test\\Dao\\Bean', 'TheCodingMachine\\TDBM\\Test\\Dao', self::getConnection(), null, self::getCache(), null, null, []);
156
        $configuration->setPathFinder(new PathFinder(null, dirname(__DIR__, 5)));
157
        return $configuration;
158
    }
159
160
    /**
161
     * @var ConfigurationInterface
162
     */
163
    private $configuration;
164
165
    protected function getConfiguration(): ConfigurationInterface
166
    {
167
        if ($this->configuration === null) {
168
            return self::createConfiguration();
169
        }
170
        return $this->configuration;
171
    }
172
173
    /**
174
     * @Iterations(10)
175
     */
176
    public function benchManyToOne(): void
177
    {
178
        $tdbmService = $this->getTdbmService();
179
        $userDao = new UserDao($tdbmService);
180
        foreach ($userDao->findAll() as $user) {
181
            $label = $user->getCountry()->getLabel();
0 ignored issues
show
Unused Code introduced by
The assignment to $label is dead and can be removed.
Loading history...
182
        }
183
    }
184
}
185