From 0a188a80c230beb9e980251b0340671e0b97f411 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 30 Aug 2024 09:24:25 -0400 Subject: [PATCH] Resolve PHPStan level 6 findings --- composer.json | 3 +- composer.lock | 60 ++++++++++++++++++++- src/Configuration.php | 2 +- src/Count.php | 4 +- src/Custom.php | 12 ++--- src/Definition.php | 2 +- src/Delete.php | 4 +- src/Document.php | 6 +-- src/DocumentList.php | 8 +-- src/Exists.php | 2 +- src/Field.php | 58 ++++++++++---------- src/Find.php | 8 +-- src/Mapper/ArrayMapper.php | 7 ++- src/Mapper/CountMapper.php | 2 + src/Mapper/DocumentMapper.php | 2 +- src/Mapper/ExistsMapper.php | 2 + src/Mapper/Mapper.php | 2 +- src/Parameters.php | 14 ++--- src/Patch.php | 12 ++--- src/Query/Definition.php | 2 +- src/Query/RemoveFields.php | 12 ++--- src/RemoveFields.php | 12 ++--- tests/PjsonId.php | 7 ++- tests/integration/postgresql/CustomTest.php | 18 +++---- tests/integration/sqlite/CustomTest.php | 18 +++---- tests/unit/ConfigurationTest.php | 2 +- tests/unit/Mapper/StringMapperTest.php | 6 +-- tests/unit/Query/RemoveFieldsTest.php | 8 +-- tests/unit/QueryTest.php | 2 +- 29 files changed, 185 insertions(+), 112 deletions(-) diff --git a/composer.json b/composer.json index 26ecd61..133d40b 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ }, "require-dev": { "phpunit/phpunit": "^11", - "square/pjson": "^0.5.0" + "square/pjson": "^0.5.0", + "phpstan/phpstan": "^1.12" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 9943487..25418e4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f3351c794dd1261ee4bf18380f58463d", + "content-hash": "7f9c6e825e6f0968b2dedefec770f558", "packages": [ { "name": "bit-badger/inspired-by-fsharp", @@ -340,6 +340,64 @@ }, "time": "2022-02-21T01:04:05+00:00" }, + { + "name": "phpstan/phpstan", + "version": "1.12.0", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "384af967d35b2162f69526c7276acadce534d0e1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/384af967d35b2162f69526c7276acadce534d0e1", + "reference": "384af967d35b2162f69526c7276acadce534d0e1", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + } + ], + "time": "2024-08-27T09:18:05+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "11.0.5", diff --git a/src/Configuration.php b/src/Configuration.php index 97bdc87..2c996f1 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -34,7 +34,7 @@ class Configuration /** @var string|null The password to use to establish a data connection (use env PDO_DOC_PASSWORD if possible) */ public static ?string $password = null; - /** @var array|null Options to use for connections (driver-specific) */ + /** @var mixed[]|null Options to use for connections (driver-specific) */ public static ?array $options = null; /** @var Option The mode in which the library is operating */ diff --git a/src/Count.php b/src/Count.php index 23cf2e5..27e43f4 100644 --- a/src/Count.php +++ b/src/Count.php @@ -31,7 +31,7 @@ class Count * Count matching documents using a comparison on JSON fields * * @param string $tableName The name of the table in which documents should be counted - * @param array|Field[] $fields The field comparison to match + * @param Field[] $fields The field comparison to match * @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All) * @return int The count of documents matching the field comparison * @throws DocumentException If one is encountered @@ -47,7 +47,7 @@ class Count * Count matching documents using a JSON containment query (`@>`; PostgreSQL only) * * @param string $tableName The name of the table in which documents should be counted - * @param array|object $criteria The criteria for the JSON containment query + * @param mixed[]|object $criteria The criteria for the JSON containment query * @return int The number of documents matching the JSON containment query * @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs */ diff --git a/src/Custom.php b/src/Custom.php index e01147c..d7f4fac 100644 --- a/src/Custom.php +++ b/src/Custom.php @@ -23,7 +23,7 @@ class Custom * Prepare a query for execution and run it * * @param string $query The query to be run - * @param array $parameters The parameters for the query + * @param array $parameters The parameters for the query * @return PDOStatement The result of executing the query * @throws DocumentException If the query execution is unsuccessful */ @@ -67,7 +67,7 @@ class Custom * * @template TDoc The domain type of the document to retrieve * @param string $query The query to be executed - * @param array $parameters Parameters to use in executing the query + * @param array $parameters Parameters to use in executing the query * @param Mapper $mapper Mapper to deserialize the result * @return DocumentList The items matching the query * @throws DocumentException If any is encountered @@ -82,7 +82,7 @@ class Custom * * @template TDoc The domain type of the document to retrieve * @param string $query The query to be executed - * @param array $parameters Parameters to use in executing the query + * @param array $parameters Parameters to use in executing the query * @param Mapper $mapper Mapper to deserialize the result * @return TDoc[] The items matching the query * @throws DocumentException If any is encountered @@ -97,7 +97,7 @@ class Custom * * @template TDoc The domain type of the document to retrieve * @param string $query The query to be executed (will have "LIMIT 1" appended) - * @param array $parameters Parameters to use in executing the query + * @param array $parameters Parameters to use in executing the query * @param Mapper $mapper Mapper to deserialize the result * @return Option A `Some` instance if the item is found, `None` otherwise * @throws DocumentException If any is encountered @@ -116,7 +116,7 @@ class Custom * Execute a query that does not return a value * * @param string $query The query to execute - * @param array $parameters Parameters to use in executing the query + * @param array $parameters Parameters to use in executing the query * @throws DocumentException If any is encountered */ public static function nonQuery(string $query, array $parameters): void @@ -133,7 +133,7 @@ class Custom * * @template T The scalar type to return * @param string $query The query to retrieve the value - * @param array $parameters Parameters to use in executing the query + * @param array $parameters Parameters to use in executing the query * @param Mapper $mapper The mapper to obtain the result * @return mixed|false|T The scalar value if found, false if not * @throws DocumentException If any is encountered diff --git a/src/Definition.php b/src/Definition.php index 72b975d..0839c6d 100644 --- a/src/Definition.php +++ b/src/Definition.php @@ -30,7 +30,7 @@ class Definition * * @param string $tableName The name of the table which should be indexed * @param string $indexName The name of the index - * @param array $fields Fields which should be a part of this index + * @param string[] $fields Fields which should be a part of this index * @throws DocumentException If any is encountered */ public static function ensureFieldIndex(string $tableName, string $indexName, array $fields): void diff --git a/src/Delete.php b/src/Delete.php index b5eba2d..003f05a 100644 --- a/src/Delete.php +++ b/src/Delete.php @@ -29,7 +29,7 @@ class Delete * Delete documents by matching a comparison on JSON fields * * @param string $tableName The table from which documents should be deleted - * @param array|Field[] $fields The field comparison to match + * @param Field[] $fields The field comparison to match * @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All) * @throws DocumentException If any is encountered */ @@ -44,7 +44,7 @@ class Delete * Delete documents matching a JSON containment query (`@>`; PostgreSQL only) * * @param string $tableName The table from which documents should be deleted - * @param array|object $criteria The JSON containment query values + * @param mixed[]|object $criteria The JSON containment query values * @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs */ public static function byContains(string $tableName, array|object $criteria): void diff --git a/src/Document.php b/src/Document.php index 83c5ae2..e7c8ff7 100644 --- a/src/Document.php +++ b/src/Document.php @@ -17,7 +17,7 @@ class Document * Insert a new document * * @param string $tableName The name of the table into which the document should be inserted - * @param array|object $document The document to be inserted + * @param mixed[]|object $document The document to be inserted * @throws DocumentException If any is encountered */ public static function insert(string $tableName, array|object $document): void @@ -47,7 +47,7 @@ class Document * Save a document, inserting it if it does not exist and updating it if it does (AKA "upsert") * * @param string $tableName The name of the table to which the document should be saved - * @param array|object $document The document to be saved + * @param mixed[]|object $document The document to be saved * @throws DocumentException If any is encountered */ public static function save(string $tableName, array|object $document): void @@ -60,7 +60,7 @@ class Document * * @param string $tableName The table in which the document should be updated * @param mixed $docId The ID of the document to be updated - * @param array|object $document The document to be updated + * @param mixed[]|object $document The document to be updated * @throws DocumentException If any is encountered */ public static function update(string $tableName, mixed $docId, array|object $document): void diff --git a/src/DocumentList.php b/src/DocumentList.php index c72ea7f..9fe3ec8 100644 --- a/src/DocumentList.php +++ b/src/DocumentList.php @@ -133,14 +133,14 @@ class DocumentList * Construct a new document list * * @param string $query The query to run to retrieve results - * @param array $parameters An associative array of parameters for the query + * @param array $parameters An associative array of parameters for the query * @param Mapper $mapper A mapper to deserialize JSON documents - * @return static The document list instance + * @return self The document list instance * @throws DocumentException If any is encountered */ - public static function create(string $query, array $parameters, Mapper $mapper): static + public static function create(string $query, array $parameters, Mapper $mapper): self { $stmt = &Custom::runQuery($query, $parameters); - return new static($stmt, $mapper); + return new self($stmt, $mapper); } } diff --git a/src/Exists.php b/src/Exists.php index 3eac9c3..dc7f6f3 100644 --- a/src/Exists.php +++ b/src/Exists.php @@ -48,7 +48,7 @@ class Exists * Determine if documents exist by a JSON containment query (`@>`; PostgreSQL only) * * @param string $tableName The name of the table in which document existence should be determined - * @param array|object $criteria The criteria for the JSON containment query + * @param mixed[]|object $criteria The criteria for the JSON containment query * @return bool True if any documents match the JSON containment query, false if not * @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs */ diff --git a/src/Field.php b/src/Field.php index b8accb2..70b7383 100644 --- a/src/Field.php +++ b/src/Field.php @@ -33,8 +33,8 @@ class Field /** * Append the parameter name and value to the given associative array * - * @param array $existing The existing parameters - * @return array The given parameter array with this field's name and value appended + * @param array $existing The existing parameters + * @return array The given parameter array with this field's name and value(s) appended */ public function appendParameter(array $existing): array { @@ -88,11 +88,11 @@ class Field * @param string $fieldName The name of the field against which the value will be compared * @param mixed $value The value for which equality will be checked * @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank) - * @return static The field with the requested criterion + * @return self The field with the requested criterion */ - public static function EQ(string $fieldName, mixed $value, string $paramName = ''): static + public static function EQ(string $fieldName, mixed $value, string $paramName = ''): self { - return new static($fieldName, Op::EQ, $value, $paramName); + return new self($fieldName, Op::EQ, $value, $paramName); } /** @@ -101,11 +101,11 @@ class Field * @param string $fieldName The name of the field against which the value will be compared * @param mixed $value The value for the greater than comparison * @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank) - * @return static The field with the requested criterion + * @return self The field with the requested criterion */ - public static function GT(string $fieldName, mixed $value, string $paramName = ''): static + public static function GT(string $fieldName, mixed $value, string $paramName = ''): self { - return new static($fieldName, Op::GT, $value, $paramName); + return new self($fieldName, Op::GT, $value, $paramName); } /** @@ -114,11 +114,11 @@ class Field * @param string $fieldName The name of the field against which the value will be compared * @param mixed $value The value for the greater than or equal to comparison * @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank) - * @return static The field with the requested criterion + * @return self The field with the requested criterion */ - public static function GE(string $fieldName, mixed $value, string $paramName = ''): static + public static function GE(string $fieldName, mixed $value, string $paramName = ''): self { - return new static($fieldName, Op::GE, $value, $paramName); + return new self($fieldName, Op::GE, $value, $paramName); } /** @@ -127,11 +127,11 @@ class Field * @param string $fieldName The name of the field against which the value will be compared * @param mixed $value The value for the less than comparison * @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank) - * @return static The field with the requested criterion + * @return self The field with the requested criterion */ - public static function LT(string $fieldName, mixed $value, string $paramName = ''): static + public static function LT(string $fieldName, mixed $value, string $paramName = ''): self { - return new static($fieldName, Op::LT, $value, $paramName); + return new self($fieldName, Op::LT, $value, $paramName); } /** @@ -140,11 +140,11 @@ class Field * @param string $fieldName The name of the field against which the value will be compared * @param mixed $value The value for the less than or equal to comparison * @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank) - * @return static The field with the requested criterion + * @return self The field with the requested criterion */ - public static function LE(string $fieldName, mixed $value, string $paramName = ''): static + public static function LE(string $fieldName, mixed $value, string $paramName = ''): self { - return new static($fieldName, Op::LE, $value, $paramName); + return new self($fieldName, Op::LE, $value, $paramName); } /** @@ -153,11 +153,11 @@ class Field * @param string $fieldName The name of the field against which the value will be compared * @param mixed $value The value for the not equals comparison * @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank) - * @return static The field with the requested criterion + * @return self The field with the requested criterion */ - public static function NE(string $fieldName, mixed $value, string $paramName = ''): static + public static function NE(string $fieldName, mixed $value, string $paramName = ''): self { - return new static($fieldName, Op::NE, $value, $paramName); + return new self($fieldName, Op::NE, $value, $paramName); } /** @@ -167,32 +167,32 @@ class Field * @param mixed $minValue The lower value for range * @param mixed $maxValue The upper value for the range * @param string $paramName The name of the parameter to which this should be bound (optional; generated if blank) - * @return static The field with the requested criterion + * @return self The field with the requested criterion */ - public static function BT(string $fieldName, mixed $minValue, mixed $maxValue, string $paramName = ''): static + public static function BT(string $fieldName, mixed $minValue, mixed $maxValue, string $paramName = ''): self { - return new static($fieldName, Op::BT, [$minValue, $maxValue], $paramName); + return new self($fieldName, Op::BT, [$minValue, $maxValue], $paramName); } /** * Create an exists (IS NOT NULL) field criterion * * @param string $fieldName The name of the field for which existence will be checked - * @return static The field with the requested criterion + * @return self The field with the requested criterion */ - public static function EX(string $fieldName): static + public static function EX(string $fieldName): self { - return new static($fieldName, Op::EX, '', ''); + return new self($fieldName, Op::EX, '', ''); } /** * Create a not exists (IS NULL) field criterion * * @param string $fieldName The name of the field for which non-existence will be checked - * @return static The field with the requested criterion + * @return self The field with the requested criterion */ - public static function NEX(string $fieldName): static + public static function NEX(string $fieldName): self { - return new static($fieldName, Op::NEX, '', ''); + return new self($fieldName, Op::NEX, '', ''); } } diff --git a/src/Find.php b/src/Find.php index c39c414..f42e42b 100644 --- a/src/Find.php +++ b/src/Find.php @@ -51,7 +51,7 @@ class Find * * @template TDoc The type of document to be retrieved * @param string $tableName The table from which documents should be retrieved - * @param array|Field[] $fields The field comparison to match + * @param Field[] $fields The field comparison to match * @param class-string $className The name of the class to be retrieved * @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All) * @return DocumentList A list of documents matching the given field comparison @@ -70,7 +70,7 @@ class Find * * @template TDoc The type of document to be retrieved * @param string $tableName The name of the table from which documents should be retrieved - * @param array|object $criteria The criteria for the JSON containment query + * @param mixed[]|object $criteria The criteria for the JSON containment query * @param class-string $className The name of the class to be retrieved * @return DocumentList A list of documents matching the JSON containment query * @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs @@ -101,7 +101,7 @@ class Find * * @template TDoc The type of document to be retrieved * @param string $tableName The table from which the document should be retrieved - * @param array|Field[] $fields The field comparison to match + * @param Field[] $fields The field comparison to match * @param class-string $className The name of the class to be retrieved * @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All) * @return Option A `Some` instance with the first document if any matches are found, `None` otherwise @@ -120,7 +120,7 @@ class Find * * @template TDoc The type of document to be retrieved * @param string $tableName The name of the table from which documents should be retrieved - * @param array|object $criteria The criteria for the JSON containment query + * @param mixed[]|object $criteria The criteria for the JSON containment query * @param class-string $className The name of the class to be retrieved * @return Option A `Some` instance with the first document if any matches are found, `None` otherwise * @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs diff --git a/src/Mapper/ArrayMapper.php b/src/Mapper/ArrayMapper.php index baaf9c1..29d4be1 100644 --- a/src/Mapper/ArrayMapper.php +++ b/src/Mapper/ArrayMapper.php @@ -10,10 +10,15 @@ namespace BitBadger\PDODocument\Mapper; /** * A mapper that returns the associative array from the database + * + * @implements Mapper> */ class ArrayMapper implements Mapper { - /** @inheritDoc */ + /** + * @inheritDoc + * @return array The array given as the parameter + */ public function map(array $result): array { return $result; diff --git a/src/Mapper/CountMapper.php b/src/Mapper/CountMapper.php index 91695d3..7a60019 100644 --- a/src/Mapper/CountMapper.php +++ b/src/Mapper/CountMapper.php @@ -10,6 +10,8 @@ namespace BitBadger\PDODocument\Mapper; /** * A mapper that returns the integer value of the first item in the results + * + * @implements Mapper */ class CountMapper implements Mapper { diff --git a/src/Mapper/DocumentMapper.php b/src/Mapper/DocumentMapper.php index 9bf4859..80e34ef 100644 --- a/src/Mapper/DocumentMapper.php +++ b/src/Mapper/DocumentMapper.php @@ -31,7 +31,7 @@ class DocumentMapper implements Mapper /** * Map a result to a domain class instance * - * @param array $result An associative array representing a single database result + * @param array $result An associative array representing a single database result * @return TDoc The document, deserialized from its JSON representation * @throws DocumentException If the JSON cannot be deserialized */ diff --git a/src/Mapper/ExistsMapper.php b/src/Mapper/ExistsMapper.php index 56f6a26..42aee1f 100644 --- a/src/Mapper/ExistsMapper.php +++ b/src/Mapper/ExistsMapper.php @@ -13,6 +13,8 @@ use Exception; /** * Map an EXISTS result to a boolean value + * + * @implements Mapper */ class ExistsMapper implements Mapper { diff --git a/src/Mapper/Mapper.php b/src/Mapper/Mapper.php index d0af9ca..6c93de1 100644 --- a/src/Mapper/Mapper.php +++ b/src/Mapper/Mapper.php @@ -18,7 +18,7 @@ interface Mapper /** * Map a result to the specified type * - * @param array $result An associative array representing a single database result + * @param array $result An associative array representing a single database result * @return T The item mapped from the given result */ public function map(array $result): mixed; diff --git a/src/Parameters.php b/src/Parameters.php index 6dc21e9..f30e046 100644 --- a/src/Parameters.php +++ b/src/Parameters.php @@ -19,19 +19,19 @@ class Parameters * Create an ID parameter (name ":id", key will be treated as a string) * * @param mixed $key The key representing the ID of the document - * @return array|string[] An associative array with an "@id" parameter/value pair + * @return array An associative array with an "@id" parameter/value pair */ public static function id(mixed $key): array { - return [':id' => is_int($key) || is_string($key) ? $key : "$key"]; + return [':id' => ((is_int($key) || is_string($key)) ? $key : "$key")]; } /** * Create a parameter with a JSON value * * @param string $name The name of the JSON parameter - * @param object|array $document The value that should be passed as a JSON string - * @return array An associative array with the named parameter/value pair + * @param mixed[]|object $document The value that should be passed as a JSON string + * @return array An associative array with the named parameter/value pair */ public static function json(string $name, object|array $document): array { @@ -74,8 +74,8 @@ class Parameters * Add field parameters to the given set of parameters * * @param Field[] $fields The fields being compared in the query - * @param array $parameters An associative array of parameters to which the fields should be added - * @return array An associative array of parameter names and values with the fields added + * @param array $parameters An associative array of parameters to which the fields should be added + * @return array An associative array of parameter names and values with the fields added */ public static function addFields(array $fields, array $parameters): array { @@ -87,7 +87,7 @@ class Parameters * * @param string $paramName The name of the parameter for the field names * @param string[] $fieldNames The names of the fields for the parameter - * @return array An associative array of parameter/value pairs for the field names + * @return array An associative array of parameter/value pairs for the field names * @throws Exception If the database mode has not been set */ public static function fieldNames(string $paramName, array $fieldNames): array diff --git a/src/Patch.php b/src/Patch.php index 6e862a8..a28506a 100644 --- a/src/Patch.php +++ b/src/Patch.php @@ -18,7 +18,7 @@ class Patch * * @param string $tableName The table in which the document should be patched * @param mixed $docId The ID of the document to be patched - * @param array|object $patch The object with which the document should be patched (will be JSON-encoded) + * @param mixed[]|object $patch The object with which the document should be patched (will be JSON-encoded) * @throws DocumentException If any is encountered (database mode must be set) */ public static function byId(string $tableName, mixed $docId, array|object $patch): void @@ -31,8 +31,8 @@ class Patch * Patch documents using a comparison on JSON fields * * @param string $tableName The table in which documents should be patched - * @param array|Field[] $fields The field comparison to match - * @param array|object $patch The object with which the documents should be patched (will be JSON-encoded) + * @param Field[] $fields The field comparison to match + * @param mixed[]|object $patch The object with which the documents should be patched (will be JSON-encoded) * @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All) * @throws DocumentException If any is encountered */ @@ -48,8 +48,8 @@ class Patch * Patch documents using a JSON containment query (`@>`; PostgreSQL only) * * @param string $tableName The table in which documents should be patched - * @param array|object $criteria The JSON containment query values to match - * @param array|object $patch The object with which the documents should be patched (will be JSON-encoded) + * @param mixed[]|object $criteria The JSON containment query values to match + * @param mixed[]|object $patch The object with which the documents should be patched (will be JSON-encoded) * @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs */ public static function byContains(string $tableName, array|object $criteria, array|object $patch): void @@ -63,7 +63,7 @@ class Patch * * @param string $tableName The table in which documents should be patched * @param string $path The JSON Path match string - * @param array|object $patch The object with which the documents should be patched (will be JSON-encoded) + * @param mixed[]|object $patch The object with which the documents should be patched (will be JSON-encoded) * @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs */ public static function byJsonPath(string $tableName, string $path, array|object $patch): void diff --git a/src/Query/Definition.php b/src/Query/Definition.php index 1f1f953..b221ad1 100644 --- a/src/Query/Definition.php +++ b/src/Query/Definition.php @@ -49,7 +49,7 @@ class Definition * * @param string $tableName The name of the table which should be indexed * @param string $indexName The name of the index to create - * @param array $fields An array of fields to be indexed; may contain direction (ex. 'salary DESC') + * @param string[] $fields An array of fields to be indexed; may contain direction (ex. 'salary DESC') * @return string The CREATE INDEX statement to ensure the index exists */ public static function ensureIndexOn(string $tableName, string $indexName, array $fields): string diff --git a/src/Query/RemoveFields.php b/src/Query/RemoveFields.php index 16bded7..b167c71 100644 --- a/src/Query/RemoveFields.php +++ b/src/Query/RemoveFields.php @@ -24,7 +24,7 @@ class RemoveFields * Create an UPDATE statement to remove fields from a JSON document * * @param string $tableName The name of the table in which documents should be manipulated - * @param array $parameters The parameter list for the query + * @param array $parameters The parameter list for the query * @param string $whereClause The body of the WHERE clause for the update * @return string The UPDATE statement to remove fields from a JSON document * @throws Exception If the database mode has not been set @@ -43,7 +43,7 @@ class RemoveFields * Query to remove fields from a document by the document's ID * * @param string $tableName The name of the table in which the document should be manipulated - * @param array $parameters The parameter list for the query + * @param array $parameters The parameter list for the query * @param mixed $docId The ID of the document from which fields should be removed (optional; string ID assumed) * @return string The UPDATE statement to remove fields from a document by its ID * @throws DocumentException If the database mode has not been set @@ -57,8 +57,8 @@ class RemoveFields * Query to remove fields from documents via a comparison on JSON fields within the document * * @param string $tableName The name of the table in which documents should be manipulated - * @param array|Field[] $fields The field comparison to match - * @param array $parameters The parameter list for the query + * @param Field[] $fields The field comparison to match + * @param array $parameters The parameter list for the query * @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All) * @return string The UPDATE statement to remove fields from documents via field comparison * @throws DocumentException If the database mode has not been set @@ -73,7 +73,7 @@ class RemoveFields * Query to remove fields from documents via a JSON containment query (PostgreSQL only) * * @param string $tableName The name of the table in which documents should be manipulated - * @param array $parameters The parameter list for the query + * @param array $parameters The parameter list for the query * @return string The UPDATE statement to remove fields from documents via a JSON containment query * @throws DocumentException If the database mode is not PostgreSQL */ @@ -86,7 +86,7 @@ class RemoveFields * Query to remove fields from documents via a JSON Path match query (PostgreSQL only) * * @param string $tableName The name of the table in which documents should be manipulated - * @param array $parameters The parameter list for the query + * @param array $parameters The parameter list for the query * @return string The UPDATE statement to remove fields from documents via a JSON Path match * @throws DocumentException */ diff --git a/src/RemoveFields.php b/src/RemoveFields.php index d719937..5651904 100644 --- a/src/RemoveFields.php +++ b/src/RemoveFields.php @@ -18,7 +18,7 @@ class RemoveFields * * @param string $tableName The table in which the document should have fields removed * @param mixed $docId The ID of the document from which fields should be removed - * @param array|string[] $fieldNames The names of the fields to be removed + * @param string[] $fieldNames The names of the fields to be removed * @throws DocumentException If any is encountered */ public static function byId(string $tableName, mixed $docId, array $fieldNames): void @@ -32,8 +32,8 @@ class RemoveFields * Remove fields from documents via a comparison on a JSON field in the document * * @param string $tableName The table in which documents should have fields removed - * @param array|Field[] $fields The field comparison to match - * @param array|string[] $fieldNames The names of the fields to be removed + * @param Field[] $fields The field comparison to match + * @param string[] $fieldNames The names of the fields to be removed * @param FieldMatch|null $match How to handle multiple conditions (optional; defaults to All) * @throws DocumentException If any is encountered */ @@ -50,8 +50,8 @@ class RemoveFields * Remove fields from documents via a JSON containment query (`@>`; PostgreSQL only) * * @param string $tableName The table in which documents should have fields removed - * @param array|object $criteria The JSON containment query values - * @param array|string[] $fieldNames The names of the fields to be removed + * @param mixed[]|object $criteria The JSON containment query values + * @param string[] $fieldNames The names of the fields to be removed * @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs */ public static function byContains(string $tableName, array|object $criteria, array $fieldNames): void @@ -66,7 +66,7 @@ class RemoveFields * * @param string $tableName The table in which documents should have fields removed * @param string $path The JSON Path match string - * @param array|string[] $fieldNames The names of the fields to be removed + * @param string[] $fieldNames The names of the fields to be removed * @throws DocumentException If the database mode is not PostgreSQL, or if an error occurs */ public static function byJsonPath(string $tableName, string $path, array $fieldNames): void diff --git a/tests/PjsonId.php b/tests/PjsonId.php index 0e5de4e..6e9430b 100644 --- a/tests/PjsonId.php +++ b/tests/PjsonId.php @@ -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); diff --git a/tests/integration/postgresql/CustomTest.php b/tests/integration/postgresql/CustomTest.php index 52f5c36..6bab006 100644 --- a/tests/integration/postgresql/CustomTest.php +++ b/tests/integration/postgresql/CustomTest.php @@ -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'); diff --git a/tests/integration/sqlite/CustomTest.php b/tests/integration/sqlite/CustomTest.php index e4bc343..a34b2ad 100644 --- a/tests/integration/sqlite/CustomTest.php +++ b/tests/integration/sqlite/CustomTest.php @@ -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'); diff --git a/tests/unit/ConfigurationTest.php b/tests/unit/ConfigurationTest.php index e32b48a..938fcdc 100644 --- a/tests/unit/ConfigurationTest.php +++ b/tests/unit/ConfigurationTest.php @@ -25,7 +25,7 @@ class ConfigurationTest extends TestCase } #[TestDox('ID field change succeeds')] - public function testIdFieldChangeSucceeds() + public function testIdFieldChangeSucceeds(): void { try { Configuration::$idField = 'EyeDee'; diff --git a/tests/unit/Mapper/StringMapperTest.php b/tests/unit/Mapper/StringMapperTest.php index a0805cc..e264a04 100644 --- a/tests/unit/Mapper/StringMapperTest.php +++ b/tests/unit/Mapper/StringMapperTest.php @@ -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'); diff --git a/tests/unit/Query/RemoveFieldsTest.php b/tests/unit/Query/RemoveFieldsTest.php index 2ab9da2..7ae6ccd 100644 --- a/tests/unit/Query/RemoveFieldsTest.php +++ b/tests/unit/Query/RemoveFieldsTest.php @@ -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( diff --git a/tests/unit/QueryTest.php b/tests/unit/QueryTest.php index f81b2f1..5d93fba 100644 --- a/tests/unit/QueryTest.php +++ b/tests/unit/QueryTest.php @@ -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');