From 14872f06bcbdac9c955b73f7b6702638e1fc8bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Pudil?= <me@jiripudil.cz> Date: Wed, 12 Feb 2025 13:46:00 +0100 Subject: [PATCH] rename (get|find)OneBy to (get|find)UniqueBy so that the intended use is clearer --- README.md | 6 +++--- src/Scaffolding/TableImplementation.php | 8 ++++---- tests/ConfigTableTest.phpt | 12 ++++++------ tests/Fixtures/ConfigTable.php | 8 ++++---- tests/Fixtures/GeneratedTable.php | 8 ++++---- tests/Fixtures/PackagesTable.php | 8 ++++---- tests/Fixtures/TestsTable.php | 8 ++++---- tests/TableTest.php | 2 +- 8 files changed, 30 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 643e84b..7b8c3b6 100644 --- a/README.md +++ b/README.md @@ -95,11 +95,11 @@ To retrieve a list of records that match given criteria, you can use the `findBy $rows = $table->findBy($conditions, $orderBy, $paginator); ``` -There are also two pairs of helper methods to retrieve a *single* record that matches given criteria: `getOneBy()` and `findOneBy()` look for a unique record and throw an exception when the query yields more than one result. In addition, `getOneBy()` fails if no record is found, whereas `findOneBy()` returns null in such case. +There are also two pairs of helper methods to retrieve a *single* record that matches given criteria: `getUniqueBy()` and `findUniqueBy()` look for a unique record and throw an exception when the query yields more than one result. In addition, `getUniqueBy()` fails if no record is found, whereas `findUniqueBy()` returns null in such case. ```php -$row = $table->getOneBy($conditions); -$rowOrNull = $table->findOneBy($conditions); +$row = $table->getUniqueBy($conditions); +$rowOrNull = $table->findUniqueBy($conditions); ``` And `getFirstBy()` and `findFirstBy()` return the first record that matches given criteria, regardless of whether there are more of them in the table. diff --git a/src/Scaffolding/TableImplementation.php b/src/Scaffolding/TableImplementation.php index 5243912..44c9461 100644 --- a/src/Scaffolding/TableImplementation.php +++ b/src/Scaffolding/TableImplementation.php @@ -148,7 +148,7 @@ final class TableImplementation implements Capability $namespace->addUse(TooManyRowsFound::class); - $classType->addMethod('getOneBy') + $classType->addMethod('getUniqueBy') ->setParameters([ (new Code\Parameter('conditions'))->setType(Condition::class . '|array'), ]) @@ -160,7 +160,7 @@ final class TableImplementation implements Capability ->addBody('\assert($row instanceof ?);', [new Code\Literal($namespace->simplifyName($this->rowClass))]) ->addBody('return $row;'); - $classType->addMethod('findOneBy') + $classType->addMethod('findUniqueBy') ->setParameters([ (new Code\Parameter('conditions'))->setType(Condition::class . '|array'), ]) @@ -209,9 +209,9 @@ final class TableImplementation implements Capability ->addComment('@param Condition|Condition[] $conditions') ->addComment('@return ' . $namespace->simplifyName($this->rowClass)) ->addComment('@throws RowNotFound') - ->addAttribute(\Deprecated::class, ['Use getOneBy() instead.']) + ->addAttribute(\Deprecated::class, ['Use getUniqueBy() instead.']) ->setReturnType($this->rowClass) - ->addBody('return $this->getOneBy($conditions);'); + ->addBody('return $this->getUniqueBy($conditions);'); $newMethod = $classType->addMethod('new') diff --git a/tests/ConfigTableTest.phpt b/tests/ConfigTableTest.phpt index a705a0d..3b783a3 100644 --- a/tests/ConfigTableTest.phpt +++ b/tests/ConfigTableTest.phpt @@ -22,17 +22,17 @@ $table = new ConfigTable( TestFixtures::createTypeResolver($connection), ); -$row = $table->getOneBy($table->key()->is('key1')); +$row = $table->getUniqueBy($table->key()->is('key1')); Assert::same('same value', $row->getValue()); -Assert::throws(fn() => $table->getOneBy($table->key()->is('key4')), RowNotFound::class); -Assert::throws(fn() => $table->getOneBy($table->value()->is('same value')), TooManyRowsFound::class); +Assert::throws(fn() => $table->getUniqueBy($table->key()->is('key4')), RowNotFound::class); +Assert::throws(fn() => $table->getUniqueBy($table->value()->is('same value')), TooManyRowsFound::class); -$row = $table->findOneBy($table->key()->is('key1')); +$row = $table->findUniqueBy($table->key()->is('key1')); Assert::same('same value', $row->getValue()); -Assert::null($table->findOneBy($table->key()->is('key4'))); -Assert::throws(fn() => $table->findOneBy($table->value()->is('same value')), TooManyRowsFound::class); +Assert::null($table->findUniqueBy($table->key()->is('key4'))); +Assert::throws(fn() => $table->findUniqueBy($table->value()->is('same value')), TooManyRowsFound::class); $row = $table->getFirstBy($table->key()->is('key1')); Assert::same('same value', $row->getValue()); diff --git a/tests/Fixtures/ConfigTable.php b/tests/Fixtures/ConfigTable.php index 660d80f..8ab3b8b 100644 --- a/tests/Fixtures/ConfigTable.php +++ b/tests/Fixtures/ConfigTable.php @@ -127,7 +127,7 @@ final class ConfigTable implements Table * @return ConfigRow * @throws RowNotFound */ - public function getOneBy(Condition|array $conditions): ConfigRow + public function getUniqueBy(Condition|array $conditions): ConfigRow { $row = $this->tableManager->findOneBy($this, $conditions, required: true, unique: true); \assert($row instanceof ConfigRow); @@ -140,7 +140,7 @@ final class ConfigTable implements Table * @return ConfigRow|null * @throws RowNotFound */ - public function findOneBy(Condition|array $conditions): ?ConfigRow + public function findUniqueBy(Condition|array $conditions): ?ConfigRow { $row = $this->tableManager->findOneBy($this, $conditions, required: false, unique: true); \assert($row instanceof ConfigRow || $row === null); @@ -180,10 +180,10 @@ final class ConfigTable implements Table * @return ConfigRow * @throws RowNotFound */ - #[\Deprecated('Use getOneBy() instead.')] + #[\Deprecated('Use getUniqueBy() instead.')] public function getBy(Condition|array $conditions): ConfigRow { - return $this->getOneBy($conditions); + return $this->getUniqueBy($conditions); } diff --git a/tests/Fixtures/GeneratedTable.php b/tests/Fixtures/GeneratedTable.php index ee08344..71a7011 100644 --- a/tests/Fixtures/GeneratedTable.php +++ b/tests/Fixtures/GeneratedTable.php @@ -127,7 +127,7 @@ final class GeneratedTable implements Table * @return GeneratedRow * @throws RowNotFound */ - public function getOneBy(Condition|array $conditions): GeneratedRow + public function getUniqueBy(Condition|array $conditions): GeneratedRow { $row = $this->tableManager->findOneBy($this, $conditions, required: true, unique: true); \assert($row instanceof GeneratedRow); @@ -140,7 +140,7 @@ final class GeneratedTable implements Table * @return GeneratedRow|null * @throws RowNotFound */ - public function findOneBy(Condition|array $conditions): ?GeneratedRow + public function findUniqueBy(Condition|array $conditions): ?GeneratedRow { $row = $this->tableManager->findOneBy($this, $conditions, required: false, unique: true); \assert($row instanceof GeneratedRow || $row === null); @@ -180,10 +180,10 @@ final class GeneratedTable implements Table * @return GeneratedRow * @throws RowNotFound */ - #[\Deprecated('Use getOneBy() instead.')] + #[\Deprecated('Use getUniqueBy() instead.')] public function getBy(Condition|array $conditions): GeneratedRow { - return $this->getOneBy($conditions); + return $this->getUniqueBy($conditions); } diff --git a/tests/Fixtures/PackagesTable.php b/tests/Fixtures/PackagesTable.php index cd53a91..c60e1eb 100644 --- a/tests/Fixtures/PackagesTable.php +++ b/tests/Fixtures/PackagesTable.php @@ -127,7 +127,7 @@ final class PackagesTable implements Table * @return PackageRow * @throws RowNotFound */ - public function getOneBy(Condition|array $conditions): PackageRow + public function getUniqueBy(Condition|array $conditions): PackageRow { $row = $this->tableManager->findOneBy($this, $conditions, required: true, unique: true); \assert($row instanceof PackageRow); @@ -140,7 +140,7 @@ final class PackagesTable implements Table * @return PackageRow|null * @throws RowNotFound */ - public function findOneBy(Condition|array $conditions): ?PackageRow + public function findUniqueBy(Condition|array $conditions): ?PackageRow { $row = $this->tableManager->findOneBy($this, $conditions, required: false, unique: true); \assert($row instanceof PackageRow || $row === null); @@ -180,10 +180,10 @@ final class PackagesTable implements Table * @return PackageRow * @throws RowNotFound */ - #[\Deprecated('Use getOneBy() instead.')] + #[\Deprecated('Use getUniqueBy() instead.')] public function getBy(Condition|array $conditions): PackageRow { - return $this->getOneBy($conditions); + return $this->getUniqueBy($conditions); } diff --git a/tests/Fixtures/TestsTable.php b/tests/Fixtures/TestsTable.php index 66a5e64..a5660bc 100644 --- a/tests/Fixtures/TestsTable.php +++ b/tests/Fixtures/TestsTable.php @@ -127,7 +127,7 @@ final class TestsTable implements Table * @return TestRow * @throws RowNotFound */ - public function getOneBy(Condition|array $conditions): TestRow + public function getUniqueBy(Condition|array $conditions): TestRow { $row = $this->tableManager->findOneBy($this, $conditions, required: true, unique: true); \assert($row instanceof TestRow); @@ -140,7 +140,7 @@ final class TestsTable implements Table * @return TestRow|null * @throws RowNotFound */ - public function findOneBy(Condition|array $conditions): ?TestRow + public function findUniqueBy(Condition|array $conditions): ?TestRow { $row = $this->tableManager->findOneBy($this, $conditions, required: false, unique: true); \assert($row instanceof TestRow || $row === null); @@ -180,10 +180,10 @@ final class TestsTable implements Table * @return TestRow * @throws RowNotFound */ - #[\Deprecated('Use getOneBy() instead.')] + #[\Deprecated('Use getUniqueBy() instead.')] public function getBy(Condition|array $conditions): TestRow { - return $this->getOneBy($conditions); + return $this->getUniqueBy($conditions); } diff --git a/tests/TableTest.php b/tests/TableTest.php index aaad186..791cbee 100644 --- a/tests/TableTest.php +++ b/tests/TableTest.php @@ -112,7 +112,7 @@ $nullDetails = $table->findBy($table->details()->is(null)); Assert::count(1, $nullDetails); Assert::same(0, $nullDetails[0]->getScore()); -$unique = $table->getOneBy($table->score()->is(42)); +$unique = $table->getUniqueBy($table->score()->is(42)); Assert::same(42, $unique->getScore()); $table->update($table->edit( -- GitLab