From 7b2a11792debc421d84d6de1eb17f4e8ccf8504e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 14 Jan 2025 01:55:57 +0000 Subject: [PATCH] Apply fixes from StyleCI --- src/Schemas/ArraySchema.php | 4 +++- src/Schemas/BaseSchema.php | 6 ++--- src/Schemas/NumericSchema.php | 8 +++---- src/Schemas/ObjectSchema.php | 3 ++- src/Schemas/StringSchema.php | 2 +- src/Validator.php | 1 - tests/Schemas/ArraySchemaTest.php | 26 +++++++++++----------- tests/Schemas/BaseSchemaTest.php | 3 ++- tests/Schemas/NumericSchemaTest.php | 34 ++++++++++++++--------------- tests/Schemas/ObjectSchemaTest.php | 12 +++++----- tests/Schemas/StringSchemaTest.php | 4 ++-- tests/ValidatorTest.php | 2 +- 12 files changed, 54 insertions(+), 51 deletions(-) diff --git a/src/Schemas/ArraySchema.php b/src/Schemas/ArraySchema.php index 089dc35..e452ff7 100644 --- a/src/Schemas/ArraySchema.php +++ b/src/Schemas/ArraySchema.php @@ -16,12 +16,14 @@ public function __construct( /** * @inheritDoc + * * @throws Exception */ protected function validateSchema(mixed $value): void { if (!is_array($value)) { - $this->error($this->customMessages['type'] ?? "Provided value must be an array"); + $this->error($this->customMessages['type'] ?? 'Provided value must be an array'); + return; } diff --git a/src/Schemas/BaseSchema.php b/src/Schemas/BaseSchema.php index 25a07d3..0a4fbaa 100644 --- a/src/Schemas/BaseSchema.php +++ b/src/Schemas/BaseSchema.php @@ -58,7 +58,7 @@ public function optional(): static return $this; } - public function refine(callable $callback, $customMessage = "Custom validation error"): static + public function refine(callable $callback, $customMessage = 'Custom validation error'): static { $this->customMessages['refine'] = $customMessage; $this->callback = $callback(...); @@ -100,7 +100,7 @@ public function validate(mixed $value): bool * * @return void */ - protected final function error(string $message): void + final protected function error(string $message): void { $this->errorMessages[] = $message; } @@ -110,7 +110,7 @@ protected final function error(string $message): void * * @return void */ - protected final function setErrorMessages(array $errors): void + final protected function setErrorMessages(array $errors): void { $this->errorMessages = $errors; } diff --git a/src/Schemas/NumericSchema.php b/src/Schemas/NumericSchema.php index 917da3f..e9b66aa 100644 --- a/src/Schemas/NumericSchema.php +++ b/src/Schemas/NumericSchema.php @@ -17,7 +17,7 @@ class NumericSchema extends BaseSchema /** * @return $this */ - public function integer(string $customMessage = "Value must be an integer"): static + public function integer(string $customMessage = 'Value must be an integer'): static { $this->integerOnly = true; $this->customMessages['integerOnly'] = $customMessage; @@ -28,7 +28,7 @@ public function integer(string $customMessage = "Value must be an integer"): sta /** * @return $this */ - public function float(string $customMessage = "Value must be a float"): static + public function float(string $customMessage = 'Value must be a float'): static { $this->floatOnly = true; $this->customMessages['floatOnly'] = $customMessage; @@ -39,7 +39,7 @@ public function float(string $customMessage = "Value must be a float"): static /** * @return $this */ - public function positive(string $customMessage = "Value must be positive"): static + public function positive(string $customMessage = 'Value must be positive'): static { $this->positiveOnly = true; $this->customMessages['positiveOnly'] = $customMessage; @@ -53,7 +53,7 @@ public function positive(string $customMessage = "Value must be positive"): stat protected function validateSchema(mixed $value): void { if (!is_numeric($value)) { - $this->error($this->customMessages['type'] ?? "Value must be a number"); + $this->error($this->customMessages['type'] ?? 'Value must be a number'); } if (!is_null($this->min) && $value < $this->min) { diff --git a/src/Schemas/ObjectSchema.php b/src/Schemas/ObjectSchema.php index 0d161a7..4e9b1ac 100644 --- a/src/Schemas/ObjectSchema.php +++ b/src/Schemas/ObjectSchema.php @@ -9,7 +9,8 @@ class ObjectSchema extends ArraySchema protected function validateSchema(mixed $value): void { if (!is_object($value)) { - $this->error($this->customMessages['type'] ?? "Provided value must be an object"); + $this->error($this->customMessages['type'] ?? 'Provided value must be an object'); + return; } diff --git a/src/Schemas/StringSchema.php b/src/Schemas/StringSchema.php index 19cfbdf..be60c21 100644 --- a/src/Schemas/StringSchema.php +++ b/src/Schemas/StringSchema.php @@ -19,7 +19,7 @@ class StringSchema extends BaseSchema protected function validateSchema(mixed $value): void { if (!is_string($value)) { - $this->error($this->customMessages['type'] ?? "Value must be a string"); + $this->error($this->customMessages['type'] ?? 'Value must be a string'); } if (!is_null($this->min)) { diff --git a/src/Validator.php b/src/Validator.php index efcc896..22a868a 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -3,7 +3,6 @@ namespace Webdevcave\SchemaValidator; use Webdevcave\SchemaValidator\Schemas\ArraySchema; -use Webdevcave\SchemaValidator\Schemas\BaseSchema; use Webdevcave\SchemaValidator\Schemas\NumericSchema; use Webdevcave\SchemaValidator\Schemas\ObjectSchema; use Webdevcave\SchemaValidator\Schemas\StringSchema; diff --git a/tests/Schemas/ArraySchemaTest.php b/tests/Schemas/ArraySchemaTest.php index b1c78a4..c01c236 100644 --- a/tests/Schemas/ArraySchemaTest.php +++ b/tests/Schemas/ArraySchemaTest.php @@ -22,24 +22,24 @@ public function testTypeCheck(): void { $schema = Validator::array(); - $this->assertFalse($schema->validate(1), "Array schema should not validate integers"); - $this->assertFalse($schema->validate(1.1), "Array schema should not validate floats"); - $this->assertFalse($schema->validate('str'), "Array schema should not validate strings"); - $this->assertFalse($schema->validate(new stdClass()), "Array schema should not validate objects"); + $this->assertFalse($schema->validate(1), 'Array schema should not validate integers'); + $this->assertFalse($schema->validate(1.1), 'Array schema should not validate floats'); + $this->assertFalse($schema->validate('str'), 'Array schema should not validate strings'); + $this->assertFalse($schema->validate(new stdClass()), 'Array schema should not validate objects'); - $this->assertTrue($schema->validate([1, 2, 3]), "Array schema should validate arrays"); + $this->assertTrue($schema->validate([1, 2, 3]), 'Array schema should validate arrays'); } public function testDataValidation(): void { $positiveIntCheckMessage = 'must be positive'; - $schema = Validator::array(['name' => Validator::string()->min(3), 'age' => Validator::numeric()->integer()->positive($positiveIntCheckMessage),]); + $schema = Validator::array(['name' => Validator::string()->min(3), 'age' => Validator::numeric()->integer()->positive($positiveIntCheckMessage)]); - $validData = ['name' => 'Carlos', 'age' => 35,]; - $this->assertTrue($schema->validate($validData), "Should return true when dataset is valid"); + $validData = ['name' => 'Carlos', 'age' => 35]; + $this->assertTrue($schema->validate($validData), 'Should return true when dataset is valid'); - $invalidData = ['name' => 'John', 'age' => -10,]; - $this->assertFalse($schema->validate($invalidData), "Should return false when dataset is not valid"); + $invalidData = ['name' => 'John', 'age' => -10]; + $this->assertFalse($schema->validate($invalidData), 'Should return false when dataset is not valid'); $this->assertEquals(['age' => [$positiveIntCheckMessage]], $schema->errorMessages(), 'Error messages doesn\'t match'); } @@ -49,10 +49,10 @@ public function testWildcardIndexValidation() $schema = Validator::array(['*' => Validator::numeric()->integer($errorMessage)]); $validData = [1, 2, 3]; - $this->assertTrue($schema->validate($validData), "Error evaluating wildcard index with valid data"); + $this->assertTrue($schema->validate($validData), 'Error evaluating wildcard index with valid data'); $invalidData = [null]; - $this->assertFalse($schema->validate($invalidData), "Error evaluating wildcard index with invalid data"); + $this->assertFalse($schema->validate($invalidData), 'Error evaluating wildcard index with invalid data'); } public function testOptionalCheckShouldPassOnEmptyArray(): void @@ -74,6 +74,6 @@ public function testDoubleRuleValidation(): void $data = [ 'name' => str_repeat('a', 10), ]; - $this->assertTrue($schema->validate($data), "Array schema should accept double rule"); + $this->assertTrue($schema->validate($data), 'Array schema should accept double rule'); } } diff --git a/tests/Schemas/BaseSchemaTest.php b/tests/Schemas/BaseSchemaTest.php index 44467e4..465f23e 100644 --- a/tests/Schemas/BaseSchemaTest.php +++ b/tests/Schemas/BaseSchemaTest.php @@ -43,7 +43,8 @@ public function testSetGetRule(): void $this->schema->equals($value); $this->assertEquals( - $value, $this->schema->equals(), + $value, + $this->schema->equals(), 'Schema should be able to set and get a rule value' ); } diff --git a/tests/Schemas/NumericSchemaTest.php b/tests/Schemas/NumericSchemaTest.php index d897a3a..65f2c11 100644 --- a/tests/Schemas/NumericSchemaTest.php +++ b/tests/Schemas/NumericSchemaTest.php @@ -19,9 +19,9 @@ public function testMinValue(): void $schema = new NumericSchema(); $schema->min(10); - $this->assertTrue($schema->validate(10), "Failed asserting equals than minimum value"); - $this->assertTrue($schema->validate(11), "Failed asserting number greater than minimum value"); - $this->assertFalse($schema->validate(9), "Failed asserting number lower than minimum value"); + $this->assertTrue($schema->validate(10), 'Failed asserting equals than minimum value'); + $this->assertTrue($schema->validate(11), 'Failed asserting number greater than minimum value'); + $this->assertFalse($schema->validate(9), 'Failed asserting number lower than minimum value'); } public function testMaxValue(): void @@ -29,9 +29,9 @@ public function testMaxValue(): void $schema = new NumericSchema(); $schema->max(10); - $this->assertTrue($schema->validate(10), "Failed asserting equals than maximum value"); - $this->assertFalse($schema->validate(11), "Failed asserting number greater than maximum value"); - $this->assertTrue($schema->validate(9), "Failed asserting number lower than maximum value"); + $this->assertTrue($schema->validate(10), 'Failed asserting equals than maximum value'); + $this->assertFalse($schema->validate(11), 'Failed asserting number greater than maximum value'); + $this->assertTrue($schema->validate(9), 'Failed asserting number lower than maximum value'); } public function testRequirePositive(): void @@ -39,20 +39,20 @@ public function testRequirePositive(): void $schema = new NumericSchema(); $schema->positive(); - $this->assertTrue($schema->validate(1), "Failed asserting positive value"); - $this->assertFalse($schema->validate(-1), "Failed asserting negative value (positive required)"); + $this->assertTrue($schema->validate(1), 'Failed asserting positive value'); + $this->assertFalse($schema->validate(-1), 'Failed asserting negative value (positive required)'); } public function testTypeValidation(): void { $schema = new NumericSchema(); - $this->assertFalse($schema->validate('str'), "Numeric schema should not validate strings"); - $this->assertFalse($schema->validate(new stdClass()), "Numeric schema should not validate objects"); - $this->assertFalse($schema->validate([1,2,3]), "Numeric schema should validate arrays"); + $this->assertFalse($schema->validate('str'), 'Numeric schema should not validate strings'); + $this->assertFalse($schema->validate(new stdClass()), 'Numeric schema should not validate objects'); + $this->assertFalse($schema->validate([1, 2, 3]), 'Numeric schema should validate arrays'); - $this->assertTrue($schema->validate(1), "Numeric schema should validate integers"); - $this->assertTrue($schema->validate(1.1), "Numeric schema should validate floats"); + $this->assertTrue($schema->validate(1), 'Numeric schema should validate integers'); + $this->assertTrue($schema->validate(1.1), 'Numeric schema should validate floats'); } public function testExclusiveIntegerValidation(): void @@ -60,8 +60,8 @@ public function testExclusiveIntegerValidation(): void $schema = new NumericSchema(); $schema->integer(); - $this->assertTrue($schema->validate(1), "Numeric schema should validate integers (int only)"); - $this->assertFalse($schema->validate(1.1), "Numeric schema should not validate floats (int only)"); + $this->assertTrue($schema->validate(1), 'Numeric schema should validate integers (int only)'); + $this->assertFalse($schema->validate(1.1), 'Numeric schema should not validate floats (int only)'); } public function testExclusiveFloatValidation(): void @@ -69,7 +69,7 @@ public function testExclusiveFloatValidation(): void $schema = new NumericSchema(); $schema->float(); - $this->assertFalse($schema->validate(1), "Numeric schema should not validate integers (floats only)"); - $this->assertTrue($schema->validate(1.1), "Numeric schema should validate floats (floats only)"); + $this->assertFalse($schema->validate(1), 'Numeric schema should not validate integers (floats only)'); + $this->assertTrue($schema->validate(1.1), 'Numeric schema should validate floats (floats only)'); } } diff --git a/tests/Schemas/ObjectSchemaTest.php b/tests/Schemas/ObjectSchemaTest.php index 94f7e4d..829b039 100644 --- a/tests/Schemas/ObjectSchemaTest.php +++ b/tests/Schemas/ObjectSchemaTest.php @@ -3,11 +3,11 @@ namespace Webdevcave\SchemaValidator\Tests\Schemas; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; use stdClass; use Webdevcave\SchemaValidator\Schemas\ArraySchema; use Webdevcave\SchemaValidator\Schemas\BaseSchema; use Webdevcave\SchemaValidator\Schemas\ObjectSchema; -use PHPUnit\Framework\TestCase; use Webdevcave\SchemaValidator\Validator; #[CoversClass(ObjectSchema::class)] @@ -20,11 +20,11 @@ public function testTypeCheck(): void { $schema = Validator::object(); - $this->assertFalse($schema->validate(1), "Array schema should not validate integers"); - $this->assertFalse($schema->validate(1.1), "Array schema should not validate floats"); - $this->assertFalse($schema->validate('str'), "Array schema should not validate strings"); - $this->assertFalse($schema->validate([1, 2, 3]), "Array schema should not validate arrays"); + $this->assertFalse($schema->validate(1), 'Array schema should not validate integers'); + $this->assertFalse($schema->validate(1.1), 'Array schema should not validate floats'); + $this->assertFalse($schema->validate('str'), 'Array schema should not validate strings'); + $this->assertFalse($schema->validate([1, 2, 3]), 'Array schema should not validate arrays'); - $this->assertTrue($schema->validate(new stdClass()), "Array schema should validate object"); + $this->assertTrue($schema->validate(new stdClass()), 'Array schema should validate object'); } } diff --git a/tests/Schemas/StringSchemaTest.php b/tests/Schemas/StringSchemaTest.php index 7be2dfc..8a417a4 100644 --- a/tests/Schemas/StringSchemaTest.php +++ b/tests/Schemas/StringSchemaTest.php @@ -3,10 +3,10 @@ namespace Webdevcave\SchemaValidator\Tests\Schemas; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; use stdClass; use Webdevcave\SchemaValidator\Schemas\BaseSchema; use Webdevcave\SchemaValidator\Schemas\StringSchema; -use PHPUnit\Framework\TestCase; use Webdevcave\SchemaValidator\Validator; #[CoversClass(Validator::class)] @@ -21,7 +21,7 @@ public function testMustValidateOnlyStrings(): void $this->assertFalse($schema->validate(1), 'Should not validate integers'); $this->assertFalse($schema->validate(1.2), 'Should not validate floats'); $this->assertFalse($schema->validate([]), 'Should not validate arrays'); - $this->assertFalse($schema->validate(new stdClass), 'Should not validate objects'); + $this->assertFalse($schema->validate(new stdClass()), 'Should not validate objects'); $this->assertFalse($schema->validate(null), 'Should not validate null'); $this->assertTrue($schema->validate('my string'), 'Should validate strings'); } diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index ba26e57..a7f1f4d 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -3,12 +3,12 @@ namespace Webdevcave\SchemaValidator\Tests; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\TestCase; use Webdevcave\SchemaValidator\Schemas\ArraySchema; use Webdevcave\SchemaValidator\Schemas\NumericSchema; use Webdevcave\SchemaValidator\Schemas\ObjectSchema; use Webdevcave\SchemaValidator\Schemas\StringSchema; use Webdevcave\SchemaValidator\Validator; -use PHPUnit\Framework\TestCase; #[CoversClass(Validator::class)] #[CoversClass(StringSchema::class)]