Resolve PHPStan level 6 findings

This commit is contained in:
2024-08-30 09:24:25 -04:00
parent 9e0e663811
commit 0a188a80c2
29 changed files with 185 additions and 112 deletions

View File

@@ -13,7 +13,7 @@ use Square\Pjson\JsonDataSerializable;
/**
* A serializable ID wrapper class
*/
class PjsonId implements JsonDataSerializable
final class PjsonId implements JsonDataSerializable
{
public function __construct(protected string $value) { }
@@ -22,6 +22,11 @@ class PjsonId implements JsonDataSerializable
return $this->value;
}
/**
* @param mixed $jd JSON data
* @param mixed[]|string $path path segments
* @return static
*/
public static function fromJsonData($jd, array|string $path = []): static
{
return new static($jd);

View File

@@ -34,7 +34,7 @@ class CustomTest extends TestCase
ThrowawayDb::destroy($this->dbName);
}
public function testRunQuerySucceedsWithAValidQuery()
public function testRunQuerySucceedsWithAValidQuery(): void
{
$stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []);
try {
@@ -44,7 +44,7 @@ class CustomTest extends TestCase
}
}
public function testRunQueryFailsWithAnInvalidQuery()
public function testRunQueryFailsWithAnInvalidQuery(): void
{
$this->expectException(DocumentException::class);
$stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []);
@@ -55,7 +55,7 @@ class CustomTest extends TestCase
}
}
public function testListSucceedsWhenDataIsFound()
public function testListSucceedsWhenDataIsFound(): void
{
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'The document list should not be null');
@@ -64,7 +64,7 @@ class CustomTest extends TestCase
$this->assertEquals(5, $count, 'There should have been 5 documents in the list');
}
public function testListSucceedsWhenNoDataIsFound()
public function testListSucceedsWhenNoDataIsFound(): void
{
$list = Custom::list(
Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE (data->>'num_value')::numeric > :value",
@@ -73,7 +73,7 @@ class CustomTest extends TestCase
$this->assertFalse($list->hasItems(), 'There should have been no documents in the list');
}
public function testArraySucceedsWhenDataIsFound()
public function testArraySucceedsWhenDataIsFound(): void
{
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [],
new DocumentMapper(TestDocument::class));
@@ -81,7 +81,7 @@ class CustomTest extends TestCase
$this->assertCount(2, $array, 'There should have been 2 documents in the array');
}
public function testArraySucceedsWhenNoDataIsFound()
public function testArraySucceedsWhenNoDataIsFound(): void
{
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value",
[':value' => 'not there'], new DocumentMapper(TestDocument::class));
@@ -104,14 +104,14 @@ class CustomTest extends TestCase
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
}
public function testNonQuerySucceedsWhenOperatingOnData()
public function testNonQuerySucceedsWhenOperatingOnData(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(0, $remaining, 'There should be no documents remaining in the table');
}
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause()
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE (data->>'num_value')::numeric > :value",
[':value' => 100]);
@@ -119,7 +119,7 @@ class CustomTest extends TestCase
$this->assertEquals(5, $remaining, 'There should be 5 documents remaining in the table');
}
public function testScalarSucceeds()
public function testScalarSucceeds(): void
{
$value = Custom::scalar("SELECT 5 AS it", [], new CountMapper());
$this->assertEquals(5, $value, 'The scalar value was not returned correctly');

View File

@@ -34,7 +34,7 @@ class CustomTest extends TestCase
ThrowawayDb::destroy($this->dbName);
}
public function testRunQuerySucceedsWithAValidQuery()
public function testRunQuerySucceedsWithAValidQuery(): void
{
$stmt = &Custom::runQuery('SELECT data FROM ' . ThrowawayDb::TABLE . ' LIMIT 1', []);
try {
@@ -44,7 +44,7 @@ class CustomTest extends TestCase
}
}
public function testRunQueryFailsWithAnInvalidQuery()
public function testRunQueryFailsWithAnInvalidQuery(): void
{
$this->expectException(DocumentException::class);
$stmt = &Custom::runQuery('GRAB stuff FROM over_there UNTIL done', []);
@@ -55,7 +55,7 @@ class CustomTest extends TestCase
}
}
public function testListSucceedsWhenDataIsFound()
public function testListSucceedsWhenDataIsFound(): void
{
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE), [], new DocumentMapper(TestDocument::class));
$this->assertNotNull($list, 'The document list should not be null');
@@ -64,7 +64,7 @@ class CustomTest extends TestCase
$this->assertEquals(5, $count, 'There should have been 5 documents in the list');
}
public function testListSucceedsWhenNoDataIsFound()
public function testListSucceedsWhenNoDataIsFound(): void
{
$list = Custom::list(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'num_value' > :value",
[':value' => 100], new DocumentMapper(TestDocument::class));
@@ -72,7 +72,7 @@ class CustomTest extends TestCase
$this->assertFalse($list->hasItems(), 'There should have been no documents in the list');
}
public function testArraySucceedsWhenDataIsFound()
public function testArraySucceedsWhenDataIsFound(): void
{
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'sub' IS NOT NULL", [],
new DocumentMapper(TestDocument::class));
@@ -80,7 +80,7 @@ class CustomTest extends TestCase
$this->assertCount(2, $array, 'There should have been 2 documents in the array');
}
public function testArraySucceedsWhenNoDataIsFound()
public function testArraySucceedsWhenNoDataIsFound(): void
{
$array = Custom::array(Query::selectFromTable(ThrowawayDb::TABLE) . " WHERE data->>'value' = :value",
[':value' => 'not there'], new DocumentMapper(TestDocument::class));
@@ -103,21 +103,21 @@ class CustomTest extends TestCase
$this->assertTrue($doc->isNone(), 'There should not have been a document returned');
}
public function testNonQuerySucceedsWhenOperatingOnData()
public function testNonQuerySucceedsWhenOperatingOnData(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE, []);
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(0, $remaining, 'There should be no documents remaining in the table');
}
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause()
public function testNonQuerySucceedsWhenNoDataMatchesWhereClause(): void
{
Custom::nonQuery('DELETE FROM ' . ThrowawayDb::TABLE . " WHERE data->>'num_value' > :value", [':value' => 100]);
$remaining = Count::all(ThrowawayDb::TABLE);
$this->assertEquals(5, $remaining, 'There should be 5 documents remaining in the table');
}
public function testScalarSucceeds()
public function testScalarSucceeds(): void
{
$value = Custom::scalar("SELECT 5 AS it", [], new CountMapper());
$this->assertEquals(5, $value, 'The scalar value was not returned correctly');

View File

@@ -25,7 +25,7 @@ class ConfigurationTest extends TestCase
}
#[TestDox('ID field change succeeds')]
public function testIdFieldChangeSucceeds()
public function testIdFieldChangeSucceeds(): void
{
try {
Configuration::$idField = 'EyeDee';

View File

@@ -18,21 +18,21 @@ use PHPUnit\Framework\TestCase;
#[TestDox('String Mapper (Unit tests)')]
class StringMapperTest extends TestCase
{
public function testMapSucceedsWhenFieldIsPresentAndString()
public function testMapSucceedsWhenFieldIsPresentAndString(): void
{
$result = ['test_field' => 'test_value'];
$mapper = new StringMapper('test_field');
$this->assertEquals('test_value', $mapper->map($result), 'String value not returned correctly');
}
public function testMapSucceedsWhenFieldIsPresentAndNotString()
public function testMapSucceedsWhenFieldIsPresentAndNotString(): void
{
$result = ['a_number' => 6.7];
$mapper = new StringMapper('a_number');
$this->assertEquals('6.7', $mapper->map($result), 'Number value not returned correctly');
}
public function testMapSucceedsWhenFieldIsNotPresent()
public function testMapSucceedsWhenFieldIsNotPresent(): void
{
$mapper = new StringMapper('something_else');
$this->assertNull($mapper->map([]), 'Missing value not returned correctly');

View File

@@ -48,7 +48,7 @@ class RemoveFieldsTest extends TestCase
}
#[TestDox('By ID succeeds for PostgreSQL')]
public function testByIdSucceedsForPostgreSQL()
public function testByIdSucceedsForPostgreSQL(): void
{
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals("UPDATE churro SET data = data - :bite::text[] WHERE data->>'id' = :id",
@@ -56,7 +56,7 @@ class RemoveFieldsTest extends TestCase
}
#[TestDox('By ID succeeds for SQLite')]
public function testByIdSucceedsForSQLite()
public function testByIdSucceedsForSQLite(): void
{
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals("UPDATE quesadilla SET data = json_remove(data, :bite0) WHERE data->>'id' = :id",
@@ -72,7 +72,7 @@ class RemoveFieldsTest extends TestCase
}
#[TestDox('By fields succeeds for PostgreSQL')]
public function testByFieldsSucceedsForPostgreSQL()
public function testByFieldsSucceedsForPostgreSQL(): void
{
Configuration::overrideMode(Mode::PgSQL);
$this->assertEquals("UPDATE enchilada SET data = data - :sauce::text[] WHERE data->>'cheese' = :queso",
@@ -82,7 +82,7 @@ class RemoveFieldsTest extends TestCase
}
#[TestDox('By fields succeeds for SQLite')]
public function testByFieldsSucceedsForSQLite()
public function testByFieldsSucceedsForSQLite(): void
{
Configuration::overrideMode(Mode::SQLite);
$this->assertEquals(

View File

@@ -207,7 +207,7 @@ class QueryTest extends TestCase
Query::save('test_tbl'), 'INSERT ON CONFLICT statement not constructed correctly');
}
public function testUpdateSucceeds()
public function testUpdateSucceeds(): void
{
$this->assertEquals("UPDATE testing SET data = :data WHERE data->>'id' = :id", Query::update('testing'),
'UPDATE statement not constructed correctly');