Skip to content

Commit 78da839

Browse files
authored
Merge pull request #193 from simon-mundy/exception/pr-4-table-gateway
Route the TableGateway and RowGateway throw sites through named constructors
2 parents 1570435 + 6491d9b commit 78da839

31 files changed

Lines changed: 940 additions & 491 deletions

‎analyzer-baseline.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,8 +1976,8 @@ count = 14
19761976

19771977
[[issues]]
19781978
file = "src/RowGateway/AbstractRowGateway.php"
1979-
code = "mixed-operand"
1980-
message = "Invalid middle operand: type `mixed` cannot be reliably used in string concatenation."
1979+
code = "mixed-argument"
1980+
message = 'Invalid argument type for argument #1 of `PhpDb\RowGateway\Exception\RuntimeException::forMissingPrimaryKeyData`: expected `string`, but found `mixed`.'
19811981
count = 1
19821982

19831983
[[issues]]

‎lint-baseline.toml‎

Lines changed: 24 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,36 +3360,6 @@ code = "assertion-style"
33603360
message = "Inconsistent assertions style."
33613361
count = 48
33623362

3363-
[[issues]]
3364-
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
3365-
code = "method-name"
3366-
message = "Method name `test__getThrowsExceptionForInvalidColumn` should be in camel case."
3367-
count = 1
3368-
3369-
[[issues]]
3370-
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
3371-
code = "method-name"
3372-
message = "Method name `test__get` should be in camel case."
3373-
count = 1
3374-
3375-
[[issues]]
3376-
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
3377-
code = "method-name"
3378-
message = "Method name `test__isset` should be in camel case."
3379-
count = 1
3380-
3381-
[[issues]]
3382-
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
3383-
code = "method-name"
3384-
message = "Method name `test__set` should be in camel case."
3385-
count = 1
3386-
3387-
[[issues]]
3388-
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
3389-
code = "method-name"
3390-
message = "Method name `test__unset` should be in camel case."
3391-
count = 1
3392-
33933363
[[issues]]
33943364
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
33953365
code = "no-isset"
@@ -3531,31 +3501,31 @@ count = 1
35313501
[[issues]]
35323502
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
35333503
code = "prefer-test-attribute"
3534-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__getThrowsExceptionForInvalidColumn`."
3504+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicGetThrowsForUnknownColumn`."
35353505
count = 1
35363506

35373507
[[issues]]
35383508
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
35393509
code = "prefer-test-attribute"
3540-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__get`."
3510+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicGetReturnsTheColumnValue`."
35413511
count = 1
35423512

35433513
[[issues]]
35443514
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
35453515
code = "prefer-test-attribute"
3546-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__isset`."
3516+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicIssetReportsWhetherAColumnIsSet`."
35473517
count = 1
35483518

35493519
[[issues]]
35503520
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
35513521
code = "prefer-test-attribute"
3552-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__set`."
3522+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicSetAssignsTheColumnValue`."
35533523
count = 1
35543524

35553525
[[issues]]
35563526
file = "test/unit/RowGateway/AbstractRowGatewayTest.php"
35573527
code = "prefer-test-attribute"
3558-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__unset`."
3528+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicUnsetClearsTheColumnValue`."
35593529
count = 1
35603530

35613531
[[issues]]
@@ -8034,84 +8004,6 @@ code = "assertion-style"
80348004
message = "Inconsistent assertions style."
80358005
count = 47
80368006

8037-
[[issues]]
8038-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8039-
code = "method-name"
8040-
message = "Method name `test__callThrowsExceptionForInvalidMethod` should be in camel case."
8041-
count = 1
8042-
8043-
[[issues]]
8044-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8045-
code = "method-name"
8046-
message = "Method name `test__callWithFeatureSetMagicCall` should be in camel case."
8047-
count = 1
8048-
8049-
[[issues]]
8050-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8051-
code = "method-name"
8052-
message = "Method name `test__cloneWithAliasedTableIdentifier` should be in camel case."
8053-
count = 1
8054-
8055-
[[issues]]
8056-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8057-
code = "method-name"
8058-
message = "Method name `test__cloneWithTableIdentifier` should be in camel case."
8059-
count = 1
8060-
8061-
[[issues]]
8062-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8063-
code = "method-name"
8064-
message = "Method name `test__clone` should be in camel case."
8065-
count = 1
8066-
8067-
[[issues]]
8068-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8069-
code = "method-name"
8070-
message = "Method name `test__getAdapter` should be in camel case."
8071-
count = 1
8072-
8073-
[[issues]]
8074-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8075-
code = "method-name"
8076-
message = "Method name `test__getLastInsertValue` should be in camel case."
8077-
count = 1
8078-
8079-
[[issues]]
8080-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8081-
code = "method-name"
8082-
message = "Method name `test__getTable` should be in camel case."
8083-
count = 1
8084-
8085-
[[issues]]
8086-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8087-
code = "method-name"
8088-
message = "Method name `test__getThrowsExceptionForInvalidProperty` should be in camel case."
8089-
count = 1
8090-
8091-
[[issues]]
8092-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8093-
code = "method-name"
8094-
message = "Method name `test__getWithFeatureSetMagicGet` should be in camel case."
8095-
count = 1
8096-
8097-
[[issues]]
8098-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8099-
code = "method-name"
8100-
message = "Method name `test__get` should be in camel case."
8101-
count = 1
8102-
8103-
[[issues]]
8104-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8105-
code = "method-name"
8106-
message = "Method name `test__setThrowsExceptionForInvalidProperty` should be in camel case."
8107-
count = 1
8108-
8109-
[[issues]]
8110-
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
8111-
code = "method-name"
8112-
message = "Method name `test__setWithFeatureSetMagicSet` should be in camel case."
8113-
count = 1
8114-
81158007
[[issues]]
81168008
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
81178009
code = "prefer-test-attribute"
@@ -8301,79 +8193,79 @@ count = 1
83018193
[[issues]]
83028194
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83038195
code = "prefer-test-attribute"
8304-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__callThrowsExceptionForInvalidMethod`."
8196+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicCallThrowsForUnknownMethod`."
83058197
count = 1
83068198

83078199
[[issues]]
83088200
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83098201
code = "prefer-test-attribute"
8310-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__callWithFeatureSetMagicCall`."
8202+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicCallDelegatesToTheFeatureSet`."
83118203
count = 1
83128204

83138205
[[issues]]
83148206
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83158207
code = "prefer-test-attribute"
8316-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__cloneWithAliasedTableIdentifier`."
8208+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testCloneWithAnAliasedTableIdentifier`."
83178209
count = 1
83188210

83198211
[[issues]]
83208212
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83218213
code = "prefer-test-attribute"
8322-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__cloneWithTableIdentifier`."
8214+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testCloneWithATableIdentifier`."
83238215
count = 1
83248216

83258217
[[issues]]
83268218
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83278219
code = "prefer-test-attribute"
8328-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__clone`."
8220+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testCloneKeepsTheAdapter`."
83298221
count = 1
83308222

83318223
[[issues]]
83328224
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83338225
code = "prefer-test-attribute"
8334-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__getAdapter`."
8226+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicGetReturnsTheAdapter`."
83358227
count = 1
83368228

83378229
[[issues]]
83388230
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83398231
code = "prefer-test-attribute"
8340-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__getLastInsertValue`."
8232+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicGetReturnsTheLastInsertValue`."
83418233
count = 1
83428234

83438235
[[issues]]
83448236
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83458237
code = "prefer-test-attribute"
8346-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__getTable`."
8238+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicGetReturnsTheTable`."
83478239
count = 1
83488240

83498241
[[issues]]
83508242
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83518243
code = "prefer-test-attribute"
8352-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__getThrowsExceptionForInvalidProperty`."
8244+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicGetThrowsForUnknownProperty`."
83538245
count = 1
83548246

83558247
[[issues]]
83568248
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83578249
code = "prefer-test-attribute"
8358-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__getWithFeatureSetMagicGet`."
8250+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicGetDelegatesToTheFeatureSet`."
83598251
count = 1
83608252

83618253
[[issues]]
83628254
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83638255
code = "prefer-test-attribute"
8364-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__get`."
8256+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicGetReturnsTheLastInsertValueAndAdapter`."
83658257
count = 1
83668258

83678259
[[issues]]
83688260
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83698261
code = "prefer-test-attribute"
8370-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__setThrowsExceptionForInvalidProperty`."
8262+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicSetThrowsForUnknownProperty`."
83718263
count = 1
83728264

83738265
[[issues]]
83748266
file = "test/unit/TableGateway/AbstractTableGatewayTest.php"
83758267
code = "prefer-test-attribute"
8376-
message = "Use `#[Test]` attribute instead of `test` prefix on method `test__setWithFeatureSetMagicSet`."
8268+
message = "Use `#[Test]` attribute instead of `test` prefix on method `testMagicSetDelegatesToTheFeatureSet`."
83778269
count = 1
83788270

83798271
[[issues]]
@@ -8471,6 +8363,12 @@ file = "test/unit/TableGateway/TableGatewayTest.php"
84718363
code = "strict-assertions"
84728364
message = "Use strict assertions in PHPUnit tests."
84738365
count = 2
8366+
[[issues]]
8367+
file = "src/TableGateway/AbstractTableGateway.php"
8368+
code = "halstead"
8369+
message = "Method has a high halstead difficulty"
8370+
count = 1
8371+
84748372
[[issues]]
84758373
file = "src/Sql/AbstractSql.php"
84768374
code = "halstead"

‎mago.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ baseline = "lint-baseline.toml"
1616
# condition, per the convention in docs/book/exceptions.md. The method count tracks the
1717
# number of distinct failures a component can report, so the maintainability signal does
1818
# not apply. "test/" repeats the phpdb-qa-tools default, which this key replaces.
19-
too-many-methods = { exclude = ["test/", "src/Sql/Exception/"] }
19+
too-many-methods = { exclude = ["test/", "src/Sql/Exception/", "src/TableGateway/Exception/"] }
2020

2121
[analyzer]
2222
baseline = "analyzer-baseline.toml"

‎src/RowGateway/AbstractRowGateway.php‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ public function initialize(): void
9393
$this->featureSet->apply('preInitialize', []);
9494

9595
if ($this->table === null) {
96-
throw new Exception\RuntimeException('This row object does not have a valid table set.');
96+
throw Exception\RuntimeException::forMissingTable();
9797
}
9898

9999
if ($this->primaryKeyColumn === null) {
100-
throw new Exception\RuntimeException('This row object does not have a primary key column set.');
100+
throw Exception\RuntimeException::forMissingPrimaryKeyColumn();
101101
}
102102

103103
if ($this->sql === null) {
104-
throw new Exception\RuntimeException('This row object does not have a Sql object set.');
104+
throw Exception\RuntimeException::forMissingSqlObject();
105105
}
106106

107107
$this->featureSet->apply('postInitialize', []);
@@ -259,9 +259,7 @@ protected function processPrimaryKeyData(): void
259259
$this->primaryKeyData = [];
260260
foreach ($this->primaryKeyColumn as $column) {
261261
if (! isset($this->data[$column])) {
262-
throw new Exception\RuntimeException(
263-
'While processing primary key data, a known key ' . $column . ' was not found in the data array',
264-
);
262+
throw Exception\RuntimeException::forMissingPrimaryKeyData($column);
265263
}
266264
$this->primaryKeyData[$column] = $this->data[$column];
267265
}
@@ -275,7 +273,7 @@ public function __get(string $name): mixed
275273
if (array_key_exists($name, $this->data)) {
276274
return $this->data[$name];
277275
}
278-
throw new Exception\InvalidArgumentException('Not a valid column in this row: ' . $name);
276+
throw Exception\InvalidArgumentException::forInvalidColumn($name);
279277
}
280278

281279
public function __isset(string $name): bool

‎src/RowGateway/Exception/InvalidArgumentException.php‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,23 @@
66

77
use PhpDb\Exception;
88

9-
class InvalidArgumentException extends Exception\InvalidArgumentException {}
9+
use function sprintf;
10+
11+
class InvalidArgumentException extends Exception\InvalidArgumentException
12+
{
13+
final public const string INVALID_COLUMN = 'Not a valid column in this row: %s';
14+
15+
final public const string SQL_TABLE_MISMATCH =
16+
'The Sql object provided does not have a table'
17+
. ' that matches this row object';
18+
19+
public static function forInvalidColumn(string $name): self
20+
{
21+
return new self(sprintf(self::INVALID_COLUMN, $name));
22+
}
23+
24+
public static function forSqlTableMismatch(): self
25+
{
26+
return new self(self::SQL_TABLE_MISMATCH);
27+
}
28+
}

‎src/RowGateway/Exception/RuntimeException.php‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,46 @@
66

77
use PhpDb\Exception;
88

9-
class RuntimeException extends Exception\RuntimeException {}
9+
use function sprintf;
10+
11+
class RuntimeException extends Exception\RuntimeException
12+
{
13+
final public const string MISSING_PRIMARY_KEY_COLUMN =
14+
'This row object does not have'
15+
. ' a primary key column set.';
16+
17+
final public const string MISSING_PRIMARY_KEY_DATA =
18+
'While processing primary key data, a known key %s'
19+
. ' was not found in the data array';
20+
21+
final public const string MISSING_SQL_OBJECT = 'This row object does not have a Sql object set.';
22+
23+
final public const string MISSING_TABLE = 'This row object does not have a valid table set.';
24+
25+
final public const string UNCALLABLE_METHOD = 'This method is not intended to be called on this object.';
26+
27+
public static function forMissingPrimaryKeyColumn(): self
28+
{
29+
return new self(self::MISSING_PRIMARY_KEY_COLUMN);
30+
}
31+
32+
public static function forMissingPrimaryKeyData(string $column): self
33+
{
34+
return new self(sprintf(self::MISSING_PRIMARY_KEY_DATA, $column));
35+
}
36+
37+
public static function forMissingSqlObject(): self
38+
{
39+
return new self(self::MISSING_SQL_OBJECT);
40+
}
41+
42+
public static function forMissingTable(): self
43+
{
44+
return new self(self::MISSING_TABLE);
45+
}
46+
47+
public static function forUncallableMethod(): self
48+
{
49+
return new self(self::UNCALLABLE_METHOD);
50+
}
51+
}

0 commit comments

Comments
 (0)