Passed
Push — master ( ec8004...0edb33 )
by y
02:18
created
src/DB/Schema.php 1 patch
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -124,15 +124,15 @@  discard block
 block discarded – undo
124 124
      */
125 125
     const T_CONST_NAMES = [
126 126
         'bool' => 'T_BOOL',
127
-        'boolean' => 'T_BOOL',      // gettype()
127
+        'boolean' => 'T_BOOL', // gettype()
128 128
         'DateTime' => 'T_DATETIME', // dehydrated (see Record)
129
-        'double' => 'T_BLOB',       // gettype()
129
+        'double' => 'T_BLOB', // gettype()
130 130
         'float' => 'T_FLOAT',
131 131
         'int' => 'T_INT',
132
-        'integer' => 'T_INT',       // gettype()
132
+        'integer' => 'T_INT', // gettype()
133 133
         'string' => 'T_STRING',
134
-        'String' => 'T_TEXT',       // @var String
135
-        'STRING' => 'T_BLOB',       // @var STRING
134
+        'String' => 'T_TEXT', // @var String
135
+        'STRING' => 'T_BLOB', // @var STRING
136 136
     ];
137 137
 
138 138
     /**
@@ -143,16 +143,16 @@  discard block
 block discarded – undo
143 143
         // bool
144 144
         'BOOLEAN' => 'bool',
145 145
         // int
146
-        'BIGINT' => 'int',  // mysql
146
+        'BIGINT' => 'int', // mysql
147 147
         'INTEGER' => 'int', // sqlite (must be this type to allow AUTOINCREMENT)
148 148
         // float
149 149
         'DOUBLE PRECISION' => 'float',
150 150
         // string <= 255
151 151
         'VARCHAR(255)' => 'string',
152 152
         // string <= 64k
153
-        'TEXT' => 'String',     // @var String
153
+        'TEXT' => 'String', // @var String
154 154
         // string > 64k
155
-        'BLOB' => 'STRING',     // @var STRING (sqlite)
155
+        'BLOB' => 'STRING', // @var STRING (sqlite)
156 156
         'LONGBLOB' => 'STRING', // @var STRING (mysql)
157 157
         // DateTime
158 158
         'DATETIME' => 'DateTime',
@@ -220,7 +220,7 @@  discard block
 block discarded – undo
220 220
     /**
221 221
      * @param DB $db
222 222
      */
223
-    public function __construct (DB $db) {
223
+    public function __construct(DB $db) {
224 224
         $this->db = $db;
225 225
         $this->colDefs ??= self::COLUMN_DEFINITIONS[$db->getDriver()];
226 226
     }
@@ -233,7 +233,7 @@  discard block
 block discarded – undo
233 233
      * @param int $type
234 234
      * @return $this
235 235
      */
236
-    public function addColumn (string $table, string $column, int $type = self::T_STRING_NULL) {
236
+    public function addColumn(string $table, string $column, int $type = self::T_STRING_NULL) {
237 237
         $type = $this->colDefs[$type & self::T_MASK];
238 238
         $this->db->exec("ALTER TABLE {$table} ADD COLUMN {$column} {$type}");
239 239
         unset($this->tables[$table]);
@@ -259,7 +259,7 @@  discard block
 block discarded – undo
259 259
      * @param array[] $constraints `[ <TABLE_CONST> => spec ]`
260 260
      * @return $this
261 261
      */
262
-    public function createTable (string $table, array $columns, array $constraints = []) {
262
+    public function createTable(string $table, array $columns, array $constraints = []) {
263 263
         $defs = $this->toColumnDefinitions($columns);
264 264
 
265 265
         /** @var string[] $pk */
@@ -295,7 +295,7 @@  discard block
 block discarded – undo
295 295
      * @param string $column
296 296
      * @return $this
297 297
      */
298
-    public function dropColumn (string $table, string $column) {
298
+    public function dropColumn(string $table, string $column) {
299 299
         $this->db->exec("ALTER TABLE {$table} DROP COLUMN {$column}");
300 300
         unset($this->tables[$table]);
301 301
         return $this;
@@ -306,7 +306,7 @@  discard block
 block discarded – undo
306 306
      *
307 307
      * @param string $table
308 308
      */
309
-    public function dropTable (string $table): void {
309
+    public function dropTable(string $table): void {
310 310
         $this->db->exec("DROP TABLE IF EXISTS {$table}");
311 311
         unset($this->tables[$table]);
312 312
     }
@@ -325,7 +325,7 @@  discard block
 block discarded – undo
325 325
      * @param string $column
326 326
      * @return array[] Keyed by name.
327 327
      */
328
-    public function getColumnInfo (string $table): array {
328
+    public function getColumnInfo(string $table): array {
329 329
         if ($this->db->isSQLite()) {
330 330
             $info = $this->db->query("PRAGMA table_info({$table})")->fetchAll();
331 331
             return array_combine(array_column($info, 'name'), array_map(fn(array $each) => [
@@ -345,7 +345,7 @@  discard block
 block discarded – undo
345 345
     /**
346 346
      * @return DB
347 347
      */
348
-    public function getDb () {
348
+    public function getDb() {
349 349
         return $this->db;
350 350
     }
351 351
 
@@ -353,7 +353,7 @@  discard block
 block discarded – undo
353 353
      * @param string $name
354 354
      * @return null|Table
355 355
      */
356
-    public function getTable (string $name) {
356
+    public function getTable(string $name) {
357 357
         if (!isset($this->tables[$name])) {
358 358
             if ($this->db->isSQLite()) {
359 359
                 $info = $this->db->query("PRAGMA table_info({$name})")->fetchAll();
@@ -376,7 +376,7 @@  discard block
 block discarded – undo
376 376
      * @param string $table
377 377
      * @return bool
378 378
      */
379
-    final public function offsetExists ($table): bool {
379
+    final public function offsetExists($table): bool {
380 380
         return (bool)$this->offsetGet($table);
381 381
     }
382 382
 
@@ -386,7 +386,7 @@  discard block
 block discarded – undo
386 386
      * @param string $table
387 387
      * @return null|Table
388 388
      */
389
-    public function offsetGet ($table) {
389
+    public function offsetGet($table) {
390 390
         return $this->getTable($table);
391 391
     }
392 392
 
@@ -395,7 +395,7 @@  discard block
 block discarded – undo
395 395
      * @param $value
396 396
      * @throws LogicException
397 397
      */
398
-    final public function offsetSet ($offset, $value) {
398
+    final public function offsetSet($offset, $value) {
399 399
         throw new LogicException('The schema cannot be altered this way.');
400 400
     }
401 401
 
@@ -403,7 +403,7 @@  discard block
 block discarded – undo
403 403
      * @param $offset
404 404
      * @throws LogicException
405 405
      */
406
-    final public function offsetUnset ($offset) {
406
+    final public function offsetUnset($offset) {
407 407
         throw new LogicException('The schema cannot be altered this way.');
408 408
     }
409 409
 
@@ -415,7 +415,7 @@  discard block
 block discarded – undo
415 415
      * @param string $newName
416 416
      * @return $this
417 417
      */
418
-    public function renameColumn (string $table, string $oldName, string $newName) {
418
+    public function renameColumn(string $table, string $oldName, string $newName) {
419 419
         $this->db->exec("ALTER TABLE {$table} RENAME COLUMN {$oldName} TO {$newName}");
420 420
         unset($this->tables[$table]);
421 421
         return $this;
@@ -428,7 +428,7 @@  discard block
 block discarded – undo
428 428
      * @param string $newName
429 429
      * @return $this
430 430
      */
431
-    public function renameTable (string $oldName, string $newName) {
431
+    public function renameTable(string $oldName, string $newName) {
432 432
         $this->db->exec("ALTER TABLE {$oldName} RENAME TO {$newName}");
433 433
         unset($this->tables[$oldName]);
434 434
         return $this;
@@ -440,7 +440,7 @@  discard block
 block discarded – undo
440 440
      * @param int[] $types
441 441
      * @return int[]
442 442
      */
443
-    protected function sortColumns (array $types): array {
443
+    protected function sortColumns(array $types): array {
444 444
         uksort($types, function(string $a, string $b) use ($types) {
445 445
             // descending type constant, ascending name
446 446
             return $types[$b] <=> $types[$a] ?: $a <=> $b;
@@ -452,7 +452,7 @@  discard block
 block discarded – undo
452 452
      * @param int[] $columns `[ name => <I_CONST> | <T_CONST> ]`
453 453
      * @return string[]
454 454
      */
455
-    protected function toColumnDefinitions (array $columns): array {
455
+    protected function toColumnDefinitions(array $columns): array {
456 456
         assert(count($columns) > 0);
457 457
         $columns = $this->sortColumns($columns);
458 458
         $defs = [];
@@ -478,7 +478,7 @@  discard block
 block discarded – undo
478 478
      * @param Column $foreign
479 479
      * @return string
480 480
      */
481
-    protected function toForeignKeyConstraint (string $table, string $local, int $type, Column $foreign): string {
481
+    protected function toForeignKeyConstraint(string $table, string $local, int $type, Column $foreign): string {
482 482
         return sprintf(
483 483
             'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s) ON UPDATE CASCADE ON DELETE %s',
484 484
             $this->toForeignKeyConstraint_name($table, $local),
@@ -496,7 +496,7 @@  discard block
 block discarded – undo
496 496
      * @param string $column
497 497
      * @return string
498 498
      */
499
-    protected function toForeignKeyConstraint_name (string $table, string $column): string {
499
+    protected function toForeignKeyConstraint_name(string $table, string $column): string {
500 500
         return 'FK_' . $table . '__' . $column;
501 501
     }
502 502
 
@@ -505,7 +505,7 @@  discard block
 block discarded – undo
505 505
      * @param string[] $columns
506 506
      * @return string
507 507
      */
508
-    protected function toPrimaryKeyConstraint (string $table, array $columns): string {
508
+    protected function toPrimaryKeyConstraint(string $table, array $columns): string {
509 509
         return sprintf(
510 510
             'CONSTRAINT %s PRIMARY KEY (%s)',
511 511
             $this->toPrimaryKeyConstraint_name($table, $columns),
@@ -520,7 +520,7 @@  discard block
 block discarded – undo
520 520
      * @param string[] $columns
521 521
      * @return string
522 522
      */
523
-    protected function toPrimaryKeyConstraint_name (string $table, array $columns): string {
523
+    protected function toPrimaryKeyConstraint_name(string $table, array $columns): string {
524 524
         sort($columns, SORT_STRING);
525 525
         return 'PK_' . $table . '__' . implode('__', $columns);
526 526
     }
@@ -530,7 +530,7 @@  discard block
 block discarded – undo
530 530
      * @param string[] $columns
531 531
      * @return string
532 532
      */
533
-    protected function toUniqueKeyConstraint (string $table, array $columns): string {
533
+    protected function toUniqueKeyConstraint(string $table, array $columns): string {
534 534
         return sprintf(
535 535
             'CONSTRAINT %s UNIQUE (%s)',
536 536
             $this->toUniqueKeyConstraint_name($table, $columns),
@@ -545,7 +545,7 @@  discard block
 block discarded – undo
545 545
      * @param string[] $columns
546 546
      * @return string
547 547
      */
548
-    protected function toUniqueKeyConstraint_name (string $table, array $columns): string {
548
+    protected function toUniqueKeyConstraint_name(string $table, array $columns): string {
549 549
         sort($columns, SORT_STRING);
550 550
         return 'UQ_' . $table . '__' . implode('__', $columns);
551 551
     }
Please login to merge, or discard this patch.
src/DB/Migrator.php 1 patch
Spacing   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
      * @param DB $db
35 35
      * @param string $dir
36 36
      */
37
-    public function __construct (DB $db, string $dir) {
37
+    public function __construct(DB $db, string $dir) {
38 38
         $this->db = $db;
39 39
         $this->dir = $dir;
40 40
     }
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
      * @param string $to Migration sequence identifier, or `null` to step down once.
46 46
      * @return null|string The resulting current sequence identifier.
47 47
      */
48
-    public function down (string $to = null): ?string {
48
+    public function down(string $to = null): ?string {
49 49
         return $this->db->transact(function() use ($to) {
50 50
             $current = $this->getCurrent();
51 51
             // walk newest to oldest
@@ -72,14 +72,14 @@  discard block
 block discarded – undo
72 72
      *
73 73
      * @return null|string
74 74
      */
75
-    public function getCurrent (): ?string {
75
+    public function getCurrent(): ?string {
76 76
         return $this->getTable()['sequence']['max'];
77 77
     }
78 78
 
79 79
     /**
80 80
      * @return string
81 81
      */
82
-    final public function getDir (): string {
82
+    final public function getDir(): string {
83 83
         return $this->dir;
84 84
     }
85 85
 
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
      * @param array $spec
88 88
      * @return MigrationInterface
89 89
      */
90
-    protected function getMigration (string $file) {
90
+    protected function getMigration(string $file) {
91 91
         $migration = include "{$file}";
92 92
         assert($migration instanceof MigrationInterface);
93 93
         return $migration;
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
      *
99 99
      * @return Table
100 100
      */
101
-    public function getTable () {
101
+    public function getTable() {
102 102
         return $this->table ??= ($this->db['__migrations__'] ??
103 103
             $this->db->getSchema()->createTable('__migrations__', [
104 104
                 'sequence' => Schema::T_STRING | Schema::I_PRIMARY
@@ -111,7 +111,7 @@  discard block
 block discarded – undo
111 111
      *
112 112
      * @return string[] [ sequence => file ]
113 113
      */
114
-    protected function glob () {
114
+    protected function glob() {
115 115
         $files = [];
116 116
         foreach (glob("{$this->dir}/*.php") as $file) {
117 117
             $files[basename($file, '.php')] = $file;
@@ -125,7 +125,7 @@  discard block
 block discarded – undo
125 125
      * @param null|string $to Migration sequence identifier, or `null` for all upgrades.
126 126
      * @return null|string The resulting current sequence identifier.
127 127
      */
128
-    public function up (string $to = null): ?string {
128
+    public function up(string $to = null): ?string {
129 129
         return $this->db->transact(function() use ($to) {
130 130
             $current = $this->getCurrent();
131 131
             // walk oldest to newest
Please login to merge, or discard this patch.
src/DB/Record.php 1 patch
Spacing   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -117,7 +117,7 @@  discard block
 block discarded – undo
117 117
      * @param string|EntityInterface $class
118 118
      * @return Record
119 119
      */
120
-    public static function fromClass (DB $db, $class) {
120
+    public static function fromClass(DB $db, $class) {
121 121
         $rClass = new ReflectionClass($class);
122 122
         assert($rClass->isInstantiable());
123 123
         $columns = [];
@@ -146,7 +146,7 @@  discard block
 block discarded – undo
146 146
      * @param string[] $columns Property names.
147 147
      * @param EAV[] $eav Keyed by property name.
148 148
      */
149
-    public function __construct (DB $db, EntityInterface $proto, string $table, array $columns, array $eav = []) {
149
+    public function __construct(DB $db, EntityInterface $proto, string $table, array $columns, array $eav = []) {
150 150
         parent::__construct($db, $table, $columns);
151 151
         $this->proto = $proto;
152 152
         $this->utc = new DateTimeZone('UTC');
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
      * @param Statement $statement
207 207
      * @return EntityInterface[] Keyed by ID
208 208
      */
209
-    public function fetchAll (Statement $statement): array {
209
+    public function fetchAll(Statement $statement): array {
210 210
         return iterator_to_array($this->fetchEach($statement));
211 211
     }
212 212
 
@@ -217,7 +217,7 @@  discard block
 block discarded – undo
217 217
      * @param Statement $statement
218 218
      * @return Generator|EntityInterface[] Keyed by ID
219 219
      */
220
-    public function fetchEach (Statement $statement) {
220
+    public function fetchEach(Statement $statement) {
221 221
         do {
222 222
             $entities = [];
223 223
             for ($i = 0; $i < 256 and false !== $row = $statement->fetch(); $i++) {
@@ -239,7 +239,7 @@  discard block
 block discarded – undo
239 239
      * @param array[] $eavMatch `[eav property => attribute => value]`
240 240
      * @return Select|EntityInterface[]
241 241
      */
242
-    public function findAll (array $match, array $eavMatch = []) {
242
+    public function findAll(array $match, array $eavMatch = []) {
243 243
         $select = $this->loadAll();
244 244
         foreach ($match as $a => $b) {
245 245
             $select->where($this->db->match($this[$a] ?? $a, $b));
@@ -258,21 +258,21 @@  discard block
 block discarded – undo
258 258
      * @param array $eavMatch `[eav property => attribute => value]`
259 259
      * @return null|EntityInterface
260 260
      */
261
-    public function findFirst (array $match, array $eavMatch = []) {
261
+    public function findFirst(array $match, array $eavMatch = []) {
262 262
         return $this->findAll($match, $eavMatch)->limit(1)->getFirst();
263 263
     }
264 264
 
265 265
     /**
266 266
      * @return string
267 267
      */
268
-    final public function getClass (): string {
268
+    final public function getClass(): string {
269 269
         return get_class($this->proto);
270 270
     }
271 271
 
272 272
     /**
273 273
      * @return EAV[]
274 274
      */
275
-    public function getEav () {
275
+    public function getEav() {
276 276
         return $this->eav;
277 277
     }
278 278
 
@@ -281,14 +281,14 @@  discard block
 block discarded – undo
281 281
      *
282 282
      * @return string[]
283 283
      */
284
-    final public function getProperties (): array {
284
+    final public function getProperties(): array {
285 285
         return array_keys($this->properties);
286 286
     }
287 287
 
288 288
     /**
289 289
      * @return EntityInterface
290 290
      */
291
-    public function getProto () {
291
+    public function getProto() {
292 292
         return $this->proto;
293 293
     }
294 294
 
@@ -300,7 +300,7 @@  discard block
 block discarded – undo
300 300
      * @param string $property
301 301
      * @return string
302 302
      */
303
-    final public function getType (string $property): string {
303
+    final public function getType(string $property): string {
304 304
         return $this->types[$property];
305 305
     }
306 306
 
@@ -311,7 +311,7 @@  discard block
 block discarded – undo
311 311
      *
312 312
      * @return string[]
313 313
      */
314
-    final public function getTypes (): array {
314
+    final public function getTypes(): array {
315 315
         return $this->types;
316 316
     }
317 317
 
@@ -319,7 +319,7 @@  discard block
 block discarded – undo
319 319
      * @param EntityInterface $entity
320 320
      * @return array
321 321
      */
322
-    protected function getValues (EntityInterface $entity): array {
322
+    protected function getValues(EntityInterface $entity): array {
323 323
         $values = [];
324 324
         foreach (array_keys($this->columns) as $name) {
325 325
             $value = $this->properties[$name]->getValue($entity);
@@ -343,7 +343,7 @@  discard block
 block discarded – undo
343 343
      * @param array|object $hydrated
344 344
      * @return scalar
345 345
      */
346
-    protected function getValues_dehydrate (string $to, string $from, $hydrated) {
346
+    protected function getValues_dehydrate(string $to, string $from, $hydrated) {
347 347
         unset($from); // we don't need it here but it's given for posterity
348 348
         switch ($to) {
349 349
             case 'DateTime':
@@ -358,7 +358,7 @@  discard block
 block discarded – undo
358 358
      * @param string $property
359 359
      * @return bool
360 360
      */
361
-    final public function isNullable (string $property): bool {
361
+    final public function isNullable(string $property): bool {
362 362
         return $this->nullable[$property];
363 363
     }
364 364
 
@@ -368,7 +368,7 @@  discard block
 block discarded – undo
368 368
      * @param int $id
369 369
      * @return null|EntityInterface
370 370
      */
371
-    public function load (int $id) {
371
+    public function load(int $id) {
372 372
         $statement = $this->cache(__FUNCTION__, function() {
373 373
             return $this->select()->where('id = ?')->prepare();
374 374
         });
@@ -388,7 +388,7 @@  discard block
 block discarded – undo
388 388
      *
389 389
      * @return Select|EntityInterface[]
390 390
      */
391
-    public function loadAll () {
391
+    public function loadAll() {
392 392
         return $this->select()->setFetcher(function(Statement $statement) {
393 393
             yield from $this->fetchEach($statement);
394 394
         });
@@ -399,7 +399,7 @@  discard block
 block discarded – undo
399 399
      *
400 400
      * @param EntityInterface[] $entities
401 401
      */
402
-    protected function loadEav (array $entities): void {
402
+    protected function loadEav(array $entities): void {
403 403
         $ids = array_keys($entities);
404 404
         foreach ($this->eav as $name => $eav) {
405 405
             foreach ($eav->loadAll($ids) as $id => $values) {
@@ -414,7 +414,7 @@  discard block
 block discarded – undo
414 414
      * @param EntityInterface $entity
415 415
      * @return int ID
416 416
      */
417
-    public function save (EntityInterface $entity): int {
417
+    public function save(EntityInterface $entity): int {
418 418
         if (!$entity->getId()) {
419 419
             $this->saveInsert($entity);
420 420
         }
@@ -428,7 +428,7 @@  discard block
 block discarded – undo
428 428
     /**
429 429
      * @param EntityInterface $entity
430 430
      */
431
-    protected function saveEav (EntityInterface $entity): void {
431
+    protected function saveEav(EntityInterface $entity): void {
432 432
         $id = $entity->getId();
433 433
         foreach ($this->eav as $name => $eav) {
434 434
             // may be null to skip
@@ -444,7 +444,7 @@  discard block
 block discarded – undo
444 444
      *
445 445
      * @param EntityInterface $entity
446 446
      */
447
-    protected function saveInsert (EntityInterface $entity): void {
447
+    protected function saveInsert(EntityInterface $entity): void {
448 448
         $statement = $this->cache(__FUNCTION__, function() {
449 449
             $slots = $this->db->slots(array_keys($this->columns));
450 450
             unset($slots['id']);
@@ -463,7 +463,7 @@  discard block
 block discarded – undo
463 463
      *
464 464
      * @param EntityInterface $entity
465 465
      */
466
-    protected function saveUpdate (EntityInterface $entity): void {
466
+    protected function saveUpdate(EntityInterface $entity): void {
467 467
         $statement = $this->cache(__FUNCTION__, function() {
468 468
             $slots = $this->db->slotsEqual(array_keys($this->columns));
469 469
             unset($slots['id']);
@@ -478,7 +478,7 @@  discard block
 block discarded – undo
478 478
      * @param EntityInterface $proto
479 479
      * @return $this
480 480
      */
481
-    public function setProto (EntityInterface $proto) {
481
+    public function setProto(EntityInterface $proto) {
482 482
         $this->proto = $proto;
483 483
         return $this;
484 484
     }
@@ -490,7 +490,7 @@  discard block
 block discarded – undo
490 490
      * @param mixed $value
491 491
      * @return mixed
492 492
      */
493
-    protected function setType (string $property, $value) {
493
+    protected function setType(string $property, $value) {
494 494
         if (isset($value)) {
495 495
             // complex?
496 496
             if (isset($this->hydration[$property])) {
@@ -514,7 +514,7 @@  discard block
 block discarded – undo
514 514
      * @param scalar $dehydrated
515 515
      * @return array|object
516 516
      */
517
-    protected function setType_hydrate (string $to, string $from, $dehydrated) {
517
+    protected function setType_hydrate(string $to, string $from, $dehydrated) {
518 518
         switch ($from) {
519 519
             case 'DateTime':
520 520
                 /**
@@ -537,7 +537,7 @@  discard block
 block discarded – undo
537 537
      * @param EntityInterface $entity
538 538
      * @param array $values
539 539
      */
540
-    protected function setValues (EntityInterface $entity, array $values): void {
540
+    protected function setValues(EntityInterface $entity, array $values): void {
541 541
         foreach ($values as $name => $value) {
542 542
             if (isset($this->properties[$name])) {
543 543
                 $this->properties[$name]->setValue($entity, $this->setType($name, $value));
Please login to merge, or discard this patch.