From 0685ce6e44e2d0dfdde704f1b3b17b3ba1e86a7d Mon Sep 17 00:00:00 2001 From: Daan Hage Date: Mon, 2 Mar 2020 22:29:55 +0100 Subject: [PATCH] php-cs-fixer (#22) * php-cs-fixer * Remove php-cs-fixer cache file; add it to the gitignore Co-authored-by: drtheuns --- .gitignore | 3 ++- composer.json | 1 + src/Apitizer/ExceptionStrategy/Ignore.php | 3 +-- src/Apitizer/ExceptionStrategy/Raise.php | 3 +-- .../Exceptions/DefinitionException.php | 16 ++++++------- src/Apitizer/Parser/Context.php | 3 ++- src/Apitizer/QueryBuilder.php | 13 +++++++---- src/Apitizer/Rendering/BasicRenderer.php | 11 +++++---- src/Apitizer/Support/DefinitionHelper.php | 23 ++++++++++--------- src/Apitizer/Support/FetchSpecFactory.php | 11 ++++++--- .../Types/Concerns/FetchesValueFromRow.php | 2 +- src/Apitizer/Validation/Rules.php | 3 ++- .../Commands/ValidateSchemaCommandTest.php | 4 +++- tests/Feature/QueryBuilder/PaginationTest.php | 3 ++- tests/Feature/QueryBuilder/SelectTest.php | 2 +- tests/Feature/Support/SchemaValidatorTest.php | 4 +++- tests/Unit/Types/ApidocTest.php | 8 +++++-- tests/Unit/Validation/RulesTest.php | 9 +++++--- 18 files changed, 75 insertions(+), 47 deletions(-) diff --git a/.gitignore b/.gitignore index e6346eb..0af2889 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,5 @@ Homestead.yaml Homestead.json /.vagrant .phpunit.result.cache -coverage/ \ No newline at end of file +coverage/ +.php_cs.cache \ No newline at end of file diff --git a/composer.json b/composer.json index 84bb905..8b3b204 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,7 @@ } }, "scripts": { + "fix": "php-cs-fixer fix ./src && php-cs-fixer fix ./tests", "test": "./vendor/bin/phpunit", "coverage": "./vendor/bin/phpunit --coverage-html coverage", "analyse": "./vendor/bin/phpstan analyse" diff --git a/src/Apitizer/ExceptionStrategy/Ignore.php b/src/Apitizer/ExceptionStrategy/Ignore.php index fffa278..2d4e9d9 100644 --- a/src/Apitizer/ExceptionStrategy/Ignore.php +++ b/src/Apitizer/ExceptionStrategy/Ignore.php @@ -10,8 +10,7 @@ class Ignore implements Strategy public function handle( QueryBuilder $queryBuilder, ApitizerException $apitizerException - ): void - { + ): void { // } } diff --git a/src/Apitizer/ExceptionStrategy/Raise.php b/src/Apitizer/ExceptionStrategy/Raise.php index 1eca945..3501513 100644 --- a/src/Apitizer/ExceptionStrategy/Raise.php +++ b/src/Apitizer/ExceptionStrategy/Raise.php @@ -10,8 +10,7 @@ class Raise implements Strategy public function handle( QueryBuilder $queryBuilder, ApitizerException $apitizerException - ): void - { + ): void { throw $apitizerException; } } diff --git a/src/Apitizer/Exceptions/DefinitionException.php b/src/Apitizer/Exceptions/DefinitionException.php index 630e672..7d64610 100644 --- a/src/Apitizer/Exceptions/DefinitionException.php +++ b/src/Apitizer/Exceptions/DefinitionException.php @@ -47,7 +47,7 @@ public function __construct( * @param string $key * @param mixed $given */ - static function builderClassExpected(QueryBuilder $queryBuilder, string $key, $given): self + public static function builderClassExpected(QueryBuilder $queryBuilder, string $key, $given): self { $class = get_class($queryBuilder); $message = "Expected association by [$key] on [$class] to be a " @@ -56,7 +56,7 @@ static function builderClassExpected(QueryBuilder $queryBuilder, string $key, $g return new static($message, $queryBuilder, 'association'); } - static function associationDoesNotExist(QueryBuilder $queryBuilder, Association $associaton): self + public static function associationDoesNotExist(QueryBuilder $queryBuilder, Association $associaton): self { $name = $associaton->getName(); $class = get_class($queryBuilder); @@ -74,7 +74,7 @@ static function associationDoesNotExist(QueryBuilder $queryBuilder, Association * @param string $name * @param mixed $given */ - static function fieldDefinitionExpected(QueryBuilder $queryBuilder, string $name, $given): self + public static function fieldDefinitionExpected(QueryBuilder $queryBuilder, string $name, $given): self { $class = get_class($queryBuilder); $type = is_object($given) ? get_class($given) : gettype($given); @@ -89,7 +89,7 @@ static function fieldDefinitionExpected(QueryBuilder $queryBuilder, string $name * @param string $name * @param mixed $given */ - static function associationDefinitionExpected( + public static function associationDefinitionExpected( QueryBuilder $queryBuilder, string $name, $given @@ -107,7 +107,7 @@ static function associationDefinitionExpected( * @param string $name * @param mixed $given */ - static function filterDefinitionExpected(QueryBuilder $queryBuilder, string $name, $given): self + public static function filterDefinitionExpected(QueryBuilder $queryBuilder, string $name, $given): self { $class = get_class($queryBuilder); $type = is_object($given) ? get_class($given) : gettype($given); @@ -117,7 +117,7 @@ static function filterDefinitionExpected(QueryBuilder $queryBuilder, string $nam return new static($message, $queryBuilder, 'filter', $name); } - static function filterHandlerNotDefined(QueryBuilder $queryBuilder, Filter $filter): self + public static function filterHandlerNotDefined(QueryBuilder $queryBuilder, Filter $filter): self { $class = get_class($queryBuilder); $name = $filter->getName(); @@ -131,7 +131,7 @@ static function filterHandlerNotDefined(QueryBuilder $queryBuilder, Filter $filt * @param string $name * @param mixed $given */ - static function sortDefinitionExpected(QueryBuilder $queryBuilder, string $name, $given): self + public static function sortDefinitionExpected(QueryBuilder $queryBuilder, string $name, $given): self { $class = get_class($queryBuilder); $type = is_object($given) ? get_class($given) : gettype($given); @@ -141,7 +141,7 @@ static function sortDefinitionExpected(QueryBuilder $queryBuilder, string $name, return new static($message, $queryBuilder, 'sort', $name); } - static function sortHandlerNotDefined(QueryBuilder $queryBuilder, string $name): self + public static function sortHandlerNotDefined(QueryBuilder $queryBuilder, string $name): self { $class = get_class($queryBuilder); $message = "Expected a callable handler to be defined for sort [$name] on [$class]"; diff --git a/src/Apitizer/Parser/Context.php b/src/Apitizer/Parser/Context.php index 6918c00..106f679 100644 --- a/src/Apitizer/Parser/Context.php +++ b/src/Apitizer/Parser/Context.php @@ -46,7 +46,8 @@ class Context /** * @param Relation|ParsedInput $stack */ - public function __construct($stack, Context $parent = null) { + public function __construct($stack, Context $parent = null) + { $this->stack = $stack; $this->parent = $parent; } diff --git a/src/Apitizer/QueryBuilder.php b/src/Apitizer/QueryBuilder.php index dcec702..34367fb 100644 --- a/src/Apitizer/QueryBuilder.php +++ b/src/Apitizer/QueryBuilder.php @@ -240,7 +240,8 @@ public function afterQuery(Builder $query, FetchSpec $fetchSpec): Builder return $query; } - public function __construct(Request $request = null) { + public function __construct(Request $request = null) + { $this->setRequest($request); } @@ -382,7 +383,9 @@ public function paginate(int $perPage = null, $pageName = 'page', $page = null): return tap($paginator, function (AbstractPaginator $paginator) use ($fetchSpec) { $renderedData = $this->getRenderer()->render( - $this, $paginator->getCollection(), $fetchSpec + $this, + $paginator->getCollection(), + $fetchSpec ); $paginator->setCollection(collect($renderedData)); @@ -474,7 +477,8 @@ protected function makeFetchSpecification(): FetchSpec : RawInput::fromArray($this->specification); return FetchSpecFactory::fromRequestInput( - $this->getParser()->parse($rawInput), $this + $this->getParser()->parse($rawInput), + $this ); } @@ -497,7 +501,8 @@ public function getAssociations(): array { if (is_null($this->availableAssociations)) { $this->availableAssociations = DefinitionHelper::validateAssociations( - $this, $this->associations() + $this, + $this->associations() ); } diff --git a/src/Apitizer/Rendering/BasicRenderer.php b/src/Apitizer/Rendering/BasicRenderer.php index b85c0ea..39db1ef 100644 --- a/src/Apitizer/Rendering/BasicRenderer.php +++ b/src/Apitizer/Rendering/BasicRenderer.php @@ -14,7 +14,8 @@ class BasicRenderer extends AbstractRenderer implements Renderer public function render(QueryBuilder $queryBuilder, $data, FetchSpec $fetchSpec): array { return $this->doRender( - $queryBuilder, $data, + $queryBuilder, + $data, $fetchSpec->getFields(), $fetchSpec->getAssociations() ); @@ -120,7 +121,7 @@ protected function addRenderedAssociation( $row, Association $association, array &$renderedData - ): void{ + ): void { $associationData = $this->valueFromRow($row, $association->getKey()); if (! $association->passesPolicy($associationData, $row)) { @@ -128,8 +129,10 @@ protected function addRenderedAssociation( } $renderedData[$association->getName()] = $this->doRender( - $association->getRelatedQueryBuilder(), $associationData, - $association->getFields() ?? [], $association->getAssociations() ?? [] + $association->getRelatedQueryBuilder(), + $associationData, + $association->getFields() ?? [], + $association->getAssociations() ?? [] ); } } diff --git a/src/Apitizer/Support/DefinitionHelper.php b/src/Apitizer/Support/DefinitionHelper.php index 75b33ab..ccc0a93 100644 --- a/src/Apitizer/Support/DefinitionHelper.php +++ b/src/Apitizer/Support/DefinitionHelper.php @@ -24,7 +24,7 @@ class DefinitionHelper * * @return array */ - static function validateFields(QueryBuilder $queryBuilder, array $fields): array + public static function validateFields(QueryBuilder $queryBuilder, array $fields): array { $castFields = []; @@ -44,7 +44,7 @@ static function validateFields(QueryBuilder $queryBuilder, array $fields): array * * @return AbstractField */ - static function validateField(QueryBuilder $queryBuilder, string $name, $field) + public static function validateField(QueryBuilder $queryBuilder, string $name, $field) { if (is_string($field)) { $field = new Field($queryBuilder, $field, 'any'); @@ -65,7 +65,7 @@ static function validateField(QueryBuilder $queryBuilder, string $name, $field) * * @return array */ - static function validateAssociations(QueryBuilder $queryBuilder, array $associations): array + public static function validateAssociations(QueryBuilder $queryBuilder, array $associations): array { $castFields = []; @@ -81,14 +81,16 @@ static function validateAssociations(QueryBuilder $queryBuilder, array $associat * @param string $name * @param Association|mixed $association */ - static function validateAssociation( + public static function validateAssociation( QueryBuilder $queryBuilder, string $name, $association ): Association { if (! $association instanceof Association) { throw DefinitionException::associationDefinitionExpected( - $queryBuilder, $name, $association + $queryBuilder, + $name, + $association ); } @@ -104,8 +106,7 @@ static function validateAssociation( private static function isValidAssociation( QueryBuilder $queryBuilder, Association $association - ): bool - { + ): bool { $key = $association->getKey(); $model = $queryBuilder->model(); @@ -120,7 +121,7 @@ private static function isValidAssociation( * * @return array */ - static function validateSorts(QueryBuilder $queryBuilder, array $sorts): array + public static function validateSorts(QueryBuilder $queryBuilder, array $sorts): array { foreach ($sorts as $name => $sort) { static::validateSort($queryBuilder, $name, $sort); @@ -134,7 +135,7 @@ static function validateSorts(QueryBuilder $queryBuilder, array $sorts): array * @param string $name * @param Sort|mixed $sort */ - static function validateSort(QueryBuilder $queryBuilder, string $name, $sort): void + public static function validateSort(QueryBuilder $queryBuilder, string $name, $sort): void { if (! $sort instanceof Sort) { throw DefinitionException::sortDefinitionExpected($queryBuilder, $name, $sort); @@ -155,7 +156,7 @@ static function validateSort(QueryBuilder $queryBuilder, string $name, $sort): v * * @return array */ - static function validateFilters(QueryBuilder $queryBuilder, array $filters): array + public static function validateFilters(QueryBuilder $queryBuilder, array $filters): array { foreach ($filters as $name => $filter) { static::validateFilter($queryBuilder, $name, $filter); @@ -171,7 +172,7 @@ static function validateFilters(QueryBuilder $queryBuilder, array $filters): arr * * @throws DefinitionException */ - static function validateFilter(QueryBuilder $queryBuilder, string $name, $filter): void + public static function validateFilter(QueryBuilder $queryBuilder, string $name, $filter): void { if (! $filter instanceof Filter) { throw DefinitionException::filterDefinitionExpected($queryBuilder, $name, $filter); diff --git a/src/Apitizer/Support/FetchSpecFactory.php b/src/Apitizer/Support/FetchSpecFactory.php index 230d3dc..58fa20f 100644 --- a/src/Apitizer/Support/FetchSpecFactory.php +++ b/src/Apitizer/Support/FetchSpecFactory.php @@ -44,7 +44,9 @@ public function make(): FetchSpec { $fields = $this->selectedFields($this->queryBuilder, $this->input->fields); $associations = $this->selectedAssociations( - $this->queryBuilder, $this->input->associations, $this->input->fields + $this->queryBuilder, + $this->input->associations, + $this->input->fields ); // If nothing was (correct) was selected, return all the default fields @@ -107,7 +109,9 @@ protected function selectedAssociations( ); $association->setAssociations( $this->selectedAssociations( - $relatedBuilder, $relation->associations, $relation->fields + $relatedBuilder, + $relation->associations, + $relation->fields ) ); @@ -168,7 +172,8 @@ protected function selectedFilters(): array } } catch (InvalidInputException $e) { $this->queryBuilder->getExceptionStrategy()->handle( - $this->queryBuilder, $e + $this->queryBuilder, + $e ); } } diff --git a/src/Apitizer/Types/Concerns/FetchesValueFromRow.php b/src/Apitizer/Types/Concerns/FetchesValueFromRow.php index 0113d85..956877d 100644 --- a/src/Apitizer/Types/Concerns/FetchesValueFromRow.php +++ b/src/Apitizer/Types/Concerns/FetchesValueFromRow.php @@ -19,7 +19,7 @@ protected function valueFromRow($row, string $key) if ($row instanceof ArrayAccess || is_array($row)) { $value = $row[$key]; - } else if (is_object($row)) { + } elseif (is_object($row)) { $value = $row->{$key}; } diff --git a/src/Apitizer/Validation/Rules.php b/src/Apitizer/Validation/Rules.php index 31dcce9..b851f6b 100644 --- a/src/Apitizer/Validation/Rules.php +++ b/src/Apitizer/Validation/Rules.php @@ -120,7 +120,8 @@ public function hasRulesFor(string $actionMethod): bool protected function resolveRulesFor(string $actionMethod): ObjectRules { if (! $this->hasRulesFor($actionMethod)) { - return new ObjectRules(null, function () {}); + return new ObjectRules(null, function () { + }); } $object = $this->rules[$actionMethod]; diff --git a/tests/Feature/Commands/ValidateSchemaCommandTest.php b/tests/Feature/Commands/ValidateSchemaCommandTest.php index 7dc46b1..32f2502 100644 --- a/tests/Feature/Commands/ValidateSchemaCommandTest.php +++ b/tests/Feature/Commands/ValidateSchemaCommandTest.php @@ -98,7 +98,9 @@ public function it_should_list_any_unexpected_exceptions_that_occurred() } } -class NotABuilder{} +class NotABuilder +{ +} class AssociationDoesNotExist extends EmptyBuilder { public function associations(): array diff --git a/tests/Feature/QueryBuilder/PaginationTest.php b/tests/Feature/QueryBuilder/PaginationTest.php index e909926..a20a69e 100644 --- a/tests/Feature/QueryBuilder/PaginationTest.php +++ b/tests/Feature/QueryBuilder/PaginationTest.php @@ -42,7 +42,8 @@ public function pagination_links_contain_all_supported_query_parameters() $this->paginatorLinkContainsString($paginator, 'limit=1'); } - private function paginatorLinkContainsString(LengthAwarePaginator $paginator, string $string) { + private function paginatorLinkContainsString(LengthAwarePaginator $paginator, string $string) + { $this->assertStringContainsStringIgnoringCase($string, urldecode($paginator->nextPageUrl())); $this->assertStringContainsStringIgnoringCase($string, urldecode($paginator->url(1))); } diff --git a/tests/Feature/QueryBuilder/SelectTest.php b/tests/Feature/QueryBuilder/SelectTest.php index 88398da..c6857d8 100644 --- a/tests/Feature/QueryBuilder/SelectTest.php +++ b/tests/Feature/QueryBuilder/SelectTest.php @@ -12,7 +12,7 @@ class SelectTest extends TestCase { - /** @test */ + /** @test */ public function it_can_select_the_specified_fields() { $post = factory(Post::class)->create(); diff --git a/tests/Feature/Support/SchemaValidatorTest.php b/tests/Feature/Support/SchemaValidatorTest.php index 277b647..6e79efc 100644 --- a/tests/Feature/Support/SchemaValidatorTest.php +++ b/tests/Feature/Support/SchemaValidatorTest.php @@ -64,7 +64,9 @@ private function assertHasErrors(QueryBuilder $queryBuilder, $count = 1) } } -class NotABuilder {} +class NotABuilder +{ +} class BuilderClassExpected extends EmptyBuilder { public function fields(): array diff --git a/tests/Unit/Types/ApidocTest.php b/tests/Unit/Types/ApidocTest.php index 26aa764..52e5d61 100644 --- a/tests/Unit/Types/ApidocTest.php +++ b/tests/Unit/Types/ApidocTest.php @@ -45,5 +45,9 @@ public function arbitrary_metadata_can_be_attached_to_the_documenation() } // Used as examples for name guessing. -class UserQueryBuilder extends EmptyBuilder {} -class NonBuilderName extends EmptyBuilder {} +class UserQueryBuilder extends EmptyBuilder +{ +} +class NonBuilderName extends EmptyBuilder +{ +} diff --git a/tests/Unit/Validation/RulesTest.php b/tests/Unit/Validation/RulesTest.php index 616a9cd..6ba5141 100644 --- a/tests/Unit/Validation/RulesTest.php +++ b/tests/Unit/Validation/RulesTest.php @@ -35,8 +35,10 @@ public function it_returns_an_empty_object_when_none_is_defined() public function it_resolves_all_builders_when_all_rules_are_requested() { $rules = new Rules(); - $rules->storeRules(function (ObjectRules $builder) {}); - $rules->updateRules(function (ObjectRules $builder) {}); + $rules->storeRules(function (ObjectRules $builder) { + }); + $rules->updateRules(function (ObjectRules $builder) { + }); $rules = $rules->getValidationRules(); @@ -51,7 +53,8 @@ public function it_resolves_all_builders_when_all_rules_are_requested() public function it_resolves_a_single_builder_when_only_one_is_requested() { $rules = new Rules(); - $rules->storeRules(function (ObjectRules $builder) {}); + $rules->storeRules(function (ObjectRules $builder) { + }); $rules->updateRules(function (ObjectRules $builder) { $this->fail("The update rules should not be resolved"); });