Skip to content

Commit 5c899b7

Browse files
committed
Performance: slightly improve ForeignKey, DbRow, InnerResultIterator
CodeStyle check
1 parent 5ead00b commit 5c899b7

27 files changed

+103
-72
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ script:
7777
else
7878
./vendor/bin/phpunit $PHPUNITFILE;
7979
fi
80+
- composer cscheck
8081
- composer phpstan
8182
- composer require-checker
8283
after_script:

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
"phpstan": "php -d memory_limit=3G vendor/bin/phpstan analyse src -c phpstan.neon --level=7 --no-progress -vvv",
7373
"require-checker": "composer-require-checker check --config-file=composer-require-checker.json",
7474
"test": "phpunit",
75-
"ci": ["@phpstan", "@test", "@require-checker"],
75+
"csfix": "php-cs-fixer fix src/ && php-cs-fixer fix tests/",
76+
"cscheck": "php-cs-fixer fix src/ --dry-run --stop-on-violation && php-cs-fixer fix tests/ --dry-run --stop-on-violation ",
77+
"ci": ["@cscheck", "@phpstan", "@test", "@require-checker"],
7678
"post-install-cmd": ["@composer bin all install --ansi"],
7779
"post-update-cmd": ["@composer bin all update --ansi"]
7880
},

src/DbRow.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,16 @@ public function getRef(string $foreignKeyName) : ?AbstractTDBMObject
296296
$values[] = $this->dbRow[$column];
297297
}
298298

299-
$filter = SafeFunctions::arrayCombine($fk->getUnquotedForeignColumns(), $values);
299+
$foreignColumns = $fk->getUnquotedForeignColumns();
300+
$foreignTableName = $fk->getForeignTableName();
301+
302+
$filter = SafeFunctions::arrayCombine($foreignColumns, $values);
300303

301304
// If the foreign key points to the primary key, let's use findObjectByPk
302-
if ($this->tdbmService->getPrimaryKeyColumns($fk->getForeignTableName()) === $fk->getUnquotedForeignColumns()) {
303-
return $this->tdbmService->findObjectByPk($fk->getForeignTableName(), $filter, [], true);
305+
if ($this->tdbmService->getPrimaryKeyColumns($foreignTableName) === $foreignColumns) {
306+
return $this->tdbmService->findObjectByPk($foreignTableName, $filter, [], true);
304307
} else {
305-
return $this->tdbmService->findObject($fk->getForeignTableName(), $filter);
308+
return $this->tdbmService->findObject($foreignTableName, $filter);
306309
}
307310
}
308311
}

src/InnerResultIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function key()
177177
*/
178178
public function next()
179179
{
180-
$row = $this->statement->fetch(\PDO::FETCH_BOTH);
180+
$row = $this->statement->fetch(\PDO::FETCH_ASSOC);
181181
if ($row) {
182182

183183
// array<tablegroup, array<table, array<column, value>>>

src/QueryFactory/FindObjectsQueryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function __construct(string $mainTable, array $additionalTablesFetch, $fi
3333

3434
protected function compute(): void
3535
{
36-
$key = 'FindObjectsQueryFactory_'.$this->mainTable.'__'.implode('_/_',$this->additionalTablesFetch).'__'.$this->filterString.'__'.$this->orderBy;
36+
$key = 'FindObjectsQueryFactory_'.$this->mainTable.'__'.implode('_/_', $this->additionalTablesFetch).'__'.$this->filterString.'__'.$this->orderBy;
3737
if ($this->cache->contains($key)) {
3838
[
3939
$this->magicSql,

src/SafeFunctions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TheCodingMachine\TDBM;
55

6-
76
use function array_combine;
87
use function error_get_last;
98
use RuntimeException;
@@ -25,4 +24,4 @@ public static function arrayCombine(array $keys, array $values): array
2524
}
2625
return $array;
2726
}
28-
}
27+
}

src/Schema/ForeignKey.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TheCodingMachine\TDBM\Schema;
55

6-
76
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
87

98
class ForeignKey
@@ -12,17 +11,22 @@ class ForeignKey
1211
public const LOCAL_COLUMNS = 'localColumns';
1312
public const FOREIGN_COLUMNS = 'foreignColumns';
1413

15-
/**
16-
* @var array<string, string|array<string>>
17-
*/
18-
private $foreignKey;
14+
/** @var string */
15+
private $foreignTable;
16+
/** @var string[] */
17+
private $localColumns;
18+
/** @var string[] */
19+
private $foreignColumns;
20+
1921

2022
/**
2123
* @param array<string, string|array<string>> $foreignKey
2224
*/
2325
public function __construct(array $foreignKey)
2426
{
25-
$this->foreignKey = $foreignKey;
27+
$this->foreignTable = $foreignKey[self::FOREIGN_TABLE];
28+
$this->localColumns = $foreignKey[self::LOCAL_COLUMNS];
29+
$this->foreignColumns = $foreignKey[self::FOREIGN_COLUMNS];
2630
}
2731

2832
public static function createFromFk(ForeignKeyConstraint $fk): self
@@ -39,24 +43,28 @@ public static function createFromFk(ForeignKeyConstraint $fk): self
3943
*/
4044
public function getUnquotedLocalColumns(): array
4145
{
42-
return $this->foreignKey[self::LOCAL_COLUMNS];
46+
return $this->localColumns;
4347
}
4448

4549
/**
4650
* @return array<string>
4751
*/
4852
public function getUnquotedForeignColumns(): array
4953
{
50-
return $this->foreignKey[self::FOREIGN_COLUMNS];
54+
return $this->foreignColumns;
5155
}
5256

5357
public function getForeignTableName(): string
5458
{
55-
return $this->foreignKey[self::FOREIGN_TABLE];
59+
return $this->foreignTable;
5660
}
5761

62+
private $cacheKey;
5863
public function getCacheKey(): string
5964
{
60-
return 'from__'.implode(',', $this->getUnquotedLocalColumns()) . '__to__table__' . $this->getForeignTableName() . '__columns__' . implode(',', $this->getUnquotedForeignColumns());
65+
if ($this->cacheKey === null) {
66+
$this->cacheKey = 'from__' . implode(',', $this->getUnquotedLocalColumns()) . '__to__table__' . $this->getForeignTableName() . '__columns__' . implode(',', $this->getUnquotedForeignColumns());
67+
}
68+
return $this->cacheKey;
6169
}
6270
}

src/Schema/ForeignKeys.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace TheCodingMachine\TDBM\Schema;
55

6-
76
class ForeignKeys
87
{
98
/**
@@ -32,5 +31,4 @@ public function getForeignKey(string $fkName): ForeignKey
3231
}
3332
return $this->foreignKey[$fkName];
3433
}
35-
3634
}

src/Utils/Annotation/AnnotationParser.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ private function parse(string $comment, string $context): Annotations
7070

7171
// Let's add * in front of the line (otherwise, parsing is failing)
7272
$lines = explode("\n", $comment);
73-
$lines = array_map(function(string $line) { return '* '.$line; }, $lines);
73+
$lines = array_map(function (string $line) {
74+
return '* '.$line;
75+
}, $lines);
7476
$comment = implode("\n", $lines);
7577

7678
$annotations = $this->docParser->parse($comment, $context);

src/Utils/BaseCodeGeneratorListener.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*/
1717
class BaseCodeGeneratorListener implements CodeGeneratorListenerInterface
1818
{
19-
2019
public function onBaseBeanGenerated(FileGenerator $fileGenerator, BeanDescriptor $beanDescriptor, ConfigurationInterface $configuration): ?FileGenerator
2120
{
2221
return $fileGenerator;

0 commit comments

Comments
 (0)