Skip to content

Commit

Permalink
php-cs-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka committed Sep 18, 2023
1 parent 3a18c6b commit 4932e32
Show file tree
Hide file tree
Showing 17 changed files with 234 additions and 151 deletions.
1 change: 1 addition & 0 deletions docs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ node_modules
composer.lock
vendor
var
.php-cs-fixer.cache
92 changes: 92 additions & 0 deletions docs/.php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

$finder = PhpCsFixer\Finder::create()
->in(__DIR__.'/guides');

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@DoctrineAnnotation' => true,
'@PHP71Migration' => true,
'@PHP71Migration:risky' => true,
'@PHPUnit60Migration:risky' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'align_multiline_comment' => [
'comment_type' => 'phpdocs_like',
],
'array_indentation' => true,
'compact_nullable_typehint' => true,
'doctrine_annotation_array_assignment' => [
'operator' => '=',
],
'doctrine_annotation_spaces' => [
'after_array_assignments_equals' => false,
'before_array_assignments_equals' => false,
],
'explicit_indirect_variable' => true,
'fully_qualified_strict_types' => true,
'logical_operators' => true,
'multiline_comment_opening_closing' => true,
'multiline_whitespace_before_semicolons' => [
'strategy' => 'no_multi_line',
],
'no_alternative_syntax' => true,
'no_extra_blank_lines' => [
'tokens' => [
'break',
'continue',
'curly_brace_block',
'extra',
'parenthesis_brace_block',
'return',
'square_brace_block',
'throw',
'use',
],
],
'no_superfluous_elseif' => true,
'no_superfluous_phpdoc_tags' => [
'allow_mixed' => false,
],
'no_unset_cast' => true,
'no_unset_on_property' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => [
'imports_order' => [
'class',
'function',
'const',
],
'sort_algorithm' => 'alpha',
],
'php_unit_method_casing' => [
'case' => 'camel_case',
],
'php_unit_set_up_tear_down_visibility' => true,
'php_unit_test_annotation' => [
'style' => 'prefix',
],
'phpdoc_add_missing_param_annotation' => [
'only_untyped' => true,
],
'phpdoc_no_alias_tag' => true,
'phpdoc_order' => true,
'phpdoc_trim_consecutive_blank_line_separation' => true,
'phpdoc_var_annotation_correct_order' => true,
'return_assignment' => true,
'strict_param' => true,
'blank_line_after_opening_tag' => false,
'declare_strict_types' => false,
'visibility_required' => [
'elements' => [
'const',
'method',
'property',
],
],
])
->setFinder($finder);
6 changes: 3 additions & 3 deletions docs/guides/create-a-custom-doctrine-filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ protected function filterProperty(string $property, $value, QueryBuilder $queryB
* Otherwise this filter is applied to order and page as well.
*/
if (
!$this->isPropertyEnabled($property, $resourceClass) ||
!$this->isPropertyMapped($property, $resourceClass)
!$this->isPropertyEnabled($property, $resourceClass)
|| !$this->isPropertyMapped($property, $resourceClass)
) {
return;
}
Expand Down Expand Up @@ -138,9 +138,9 @@ public function up(Schema $schema): void
}

namespace App\Tests {
use ApiPlatform\Playground\Test\TestGuideTrait;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Book;
use ApiPlatform\Playground\Test\TestGuideTrait;

final class BookTest extends ApiTestCase
{
Expand Down
8 changes: 3 additions & 5 deletions docs/guides/custom-pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// The following example shows how to handle it using a custom Provider. You will need to use the Doctrine Paginator and pass it to the API Platform Paginator.

namespace App\Entity {

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use App\Repository\BookRepository;
Expand All @@ -22,7 +21,7 @@
/* Use custom Provider on operation to retrieve the custom collection */
#[ApiResource(
operations: [
new GetCollection(provider: BooksListProvider::class)
new GetCollection(provider: BooksListProvider::class),
]
)]
#[ORM\Entity(repositoryClass: BookRepository::class)]
Expand Down Expand Up @@ -71,7 +70,6 @@ public function getPublishedBooks(int $page = 1, int $itemsPerPage = 30): Doctri
}

namespace App\State {

use ApiPlatform\Doctrine\Orm\Paginator;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\Pagination\Pagination;
Expand Down Expand Up @@ -105,7 +103,6 @@ function request(): Request
}

namespace DoctrineMigrations {

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

Expand All @@ -123,6 +120,7 @@ public function up(Schema $schema): void
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Zenstruck\Foundry\AnonymousFactory;

use function Zenstruck\Foundry\faker;

final class BookFixtures extends Fixture
Expand All @@ -148,9 +146,9 @@ public function load(ObjectManager $manager): void
}

namespace App\Tests {
use ApiPlatform\Playground\Test\TestGuideTrait;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use App\Entity\Book;
use ApiPlatform\Playground\Test\TestGuideTrait;

final class BookTest extends ApiTestCase
{
Expand Down
9 changes: 5 additions & 4 deletions docs/guides/declare-a-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@

// # Declare a Resource
// This class represents an API resource

namespace App\ApiResource {
// The `#[ApiResource]` attribute registers this class as an HTTP resource.
use ApiPlatform\Metadata\ApiResource;
// These are the list of HTTP operations we use to declare a "CRUD" (Create, Read, Update, Delete).
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Post;
use ApiPlatform\Validator\Exception\ValidationException;

// Each resource has its set of Operations.
Expand All @@ -34,7 +35,7 @@
],
// This is a configuration that is shared accross every operations. More details are available at [ApiResource::exceptionToStatus](/reference/Metadata/ApiResource#exceptionToStatus).
exceptionToStatus: [
ValidationException::class => 422
ValidationException::class => 422,
]
)]
// If a property named `id` is found it is the property used in your URI template
Expand All @@ -46,6 +47,7 @@ class Book
}

// Check our next guide to [provide the resource state](./provide-the-resource-state).

namespace App\Playground {
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -54,4 +56,3 @@ function request(): Request
return Request::create('/docs', 'GET');
}
}

22 changes: 8 additions & 14 deletions docs/guides/delete-operation-with-validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
// ---

// Let's add a [custom Constraint](https://symfony.com/doc/current/validation/custom_constraint.html).
namespace App\Validator {

namespace App\Validator {
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

#[\Attribute]
class AssertCanDelete extends Constraint
Expand All @@ -27,27 +26,25 @@ public function getTargets(): string
}

// And a custom validator following Symfony's naming conventions.
namespace App\Validator {

use Symfony\Component\Validator\ConstraintValidator;
namespace App\Validator {
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class AssertCanDeleteValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint)
public function validate(mixed $value, Constraint $constraint): void
{
$this->context->buildViolation($constraint->message)->addViolation();
}
}
}


namespace App\Entity {

use ApiPlatform\Metadata\Delete;
use ApiPlatform\Symfony\Validator\Exception\ValidationException;
use App\Validator\AssertCanDelete;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Symfony\Validator\Exception\ValidationException;

#[ORM\Entity]
#[Delete(
Expand Down Expand Up @@ -75,7 +72,6 @@ public function getId()
}

namespace App\Playground {

use Symfony\Component\HttpFoundation\Request;

function request(): Request
Expand All @@ -85,13 +81,13 @@ function request(): Request
}

namespace App\Fixtures {

use App\Entity\Book;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

use function Zenstruck\Foundry\anonymous;
use function Zenstruck\Foundry\repository;
use function Zenstruck\Foundry\faker;
use function Zenstruck\Foundry\repository;

final class BookFixtures extends Fixture
{
Expand All @@ -103,8 +99,7 @@ public function load(ObjectManager $manager): void
}

$bookFactory->many(10)->create(
fn () =>
[
fn () => [
'title' => faker()->name(),
]
);
Expand All @@ -113,7 +108,6 @@ public function load(ObjectManager $manager): void
}

namespace DoctrineMigrations {

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

Expand Down
24 changes: 11 additions & 13 deletions docs/guides/doctrine-entity-as-resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
// # API Resource on a Doctrine Entity.
//
// API Platform is compatible with [Doctrine ORM](https://www.doctrine-project.org), all we need is to declare an
namespace App\Entity {

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\ApiFilter;
namespace App\Entity {
use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use Doctrine\ORM\Mapping as ORM;

// When an ApiResource is declared on an `\ORM\Entity` we have access to [Doctrine filters](https://api-platform.com/docs/core/filters/).
Expand All @@ -37,20 +37,20 @@ public function getId(): ?int
}

namespace App\Playground {

use Symfony\Component\HttpFoundation\Request;

function request(): Request
{
// Persistence is automatic, you can try to create or read data:
return Request::create('/books?order[id]=desc', 'GET');

return Request::create('/books/1', 'GET');

return Request::create(uri: '/books', method: 'POST', server: ['CONTENT_TYPE' => 'application/ld+json'], content: json_encode(['id' => 1, 'title' => 'API Platform rocks.']));
}
}

namespace DoctrineMigrations {

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

Expand All @@ -64,13 +64,13 @@ public function up(Schema $schema): void
}

namespace App\Fixtures {

use App\Entity\Book;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

use function Zenstruck\Foundry\anonymous;
use function Zenstruck\Foundry\repository;
use function Zenstruck\Foundry\faker;
use function Zenstruck\Foundry\repository;

final class BookFixtures extends Fixture
{
Expand All @@ -81,19 +81,17 @@ public function load(ObjectManager $manager): void
return;
}

$bookFactory->many(10)->create(fn() =>
[
'title' => faker()->name(),
]
$bookFactory->many(10)->create(fn () => [
'title' => faker()->name(),
]
);
}
}
}

namespace App\Tests {

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Playground\Test\TestGuideTrait;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;

final class BookTest extends ApiTestCase
{
Expand Down
Loading

0 comments on commit 4932e32

Please sign in to comment.