Skip to content

Commit

Permalink
[Set] FOSRest annotation to attribute (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
wkania authored Nov 12, 2022
1 parent ae66bbc commit 8b63302
Show file tree
Hide file tree
Showing 6 changed files with 389 additions and 0 deletions.
36 changes: 36 additions & 0 deletions config/sets/fosrest/annotations-to-attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\Php80\ValueObject\AnnotationToAttribute;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [
// @see https://github.com/FriendsOfSymfony/FOSRestBundle/pull/2325
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Copy'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Delete'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Get'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Head'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Link'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Lock'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Mkcol'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Move'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Options'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Patch'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Post'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\PropFind'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\PropPatch'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Put'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Route'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Unlink'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\Unlock'),
// @see https://github.com/FriendsOfSymfony/FOSRestBundle/pull/2326
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\View'),
// @see https://github.com/FriendsOfSymfony/FOSRestBundle/pull/2327
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\FileParam'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\QueryParam'),
new AnnotationToAttribute('FOS\RestBundle\Controller\Annotations\RequestParam'),
]);
};
15 changes: 15 additions & 0 deletions src/Set/FOSRestSetList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Set;

use Rector\Set\Contract\SetListInterface;

final class FOSRestSetList implements SetListInterface
{
/**
* @var string
*/
final public const ANNOTATIONS_TO_ATTRIBUTES = __DIR__ . '/../../config/sets/fosrest/annotations-to-attributes.php';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\Set\FOSRestAnnotationsToAttributes;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class FOSRestAnnotationsToAttributesTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/fosrestAnnotationsToAttributes.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

use FOS\RestBundle\Controller\Annotations\FileParam;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\RequestParam;

class BarController
{
/**
* @QueryParam(name="fooId", nullable=false, requirements="\d+")
* @QueryParam(name="name", nullable=true, description="Foo Name")
*/
public function foo()
{
}

/**
* @RequestParam(name="name", requirements="[a-z]+", description="Foo Name")
* @FileParam(name="avatar", requirements={"mimeTypes"="image/jpeg", "minWidth"="500"}, image=true)
*/
public function create()
{
}
}

?>
-----
<?php

use FOS\RestBundle\Controller\Annotations\FileParam;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\RequestParam;

class BarController
{
#[QueryParam(name: 'fooId', nullable: false, requirements: '\d+')]
#[QueryParam(name: 'name', nullable: true, description: 'Foo Name')]
public function foo()
{
}

#[RequestParam(name: 'name', requirements: '[a-z]+', description: 'Foo Name')]
#[FileParam(name: 'avatar', requirements: ['mimeTypes' => 'image/jpeg', 'minWidth' => 500], image: true)]
public function create()
{
}
}

?>
249 changes: 249 additions & 0 deletions tests/Set/FOSRestAnnotationsToAttributes/Fixture/route_fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
<?php

use FOS\RestBundle\Controller\Annotations\Copy;
use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Head;
use FOS\RestBundle\Controller\Annotations\Link;
use FOS\RestBundle\Controller\Annotations\Lock;
use FOS\RestBundle\Controller\Annotations\Mkcol;
use FOS\RestBundle\Controller\Annotations\Move;
use FOS\RestBundle\Controller\Annotations\Options;
use FOS\RestBundle\Controller\Annotations\Patch;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\PropFind;
use FOS\RestBundle\Controller\Annotations\PropPatch;
use FOS\RestBundle\Controller\Annotations\Put;
use FOS\RestBundle\Controller\Annotations\Route;
use FOS\RestBundle\Controller\Annotations\Unlink;
use FOS\RestBundle\Controller\Annotations\Unlock;
use FOS\RestBundle\Controller\Annotations\View;

/**
* @Route("/api/foo")
*/
class BarController
{
/**
* @Copy
*/
public function copy()
{
}

/**
* @Delete("/delete", name="foo_delete")
*/
public function delete()
{
}

/**
* @Get("/list", name="foo_list")
* @View(serializerEnableMaxDepthChecks=false)
*/
public function list()
{
}

/**
* @Head
*/
public function foo()
{
}

/**
* @Link
*/
public function addFriend()
{
}

/**
* @Lock
*/
public function pessimisticLock()
{
}

/**
* @Mkcol
*/
public function createList()
{
}

/**
* @Move
*/
public function move()
{
}

/**
* @Options
*/
public function options()
{
}

/**
* @Patch("/update", name="foo_update")
*/
public function update()
{
}

/**
* @Post("/create", name="foo_create")
*/
public function create()
{
}

/**
* @PropFind
*/
public function propFind()
{
}

/**
* @PropPatch
*/
public function propPatch()
{
}

/**
* @Put("/replace", name="foo_replace")
*/
public function replace()
{
}

/**
* @Unlink
*/
public function removeFriend()
{
}

/**
* @Unlock
*/
public function unlock()
{
}
}

?>
-----
<?php

use FOS\RestBundle\Controller\Annotations\Copy;
use FOS\RestBundle\Controller\Annotations\Delete;
use FOS\RestBundle\Controller\Annotations\Get;
use FOS\RestBundle\Controller\Annotations\Head;
use FOS\RestBundle\Controller\Annotations\Link;
use FOS\RestBundle\Controller\Annotations\Lock;
use FOS\RestBundle\Controller\Annotations\Mkcol;
use FOS\RestBundle\Controller\Annotations\Move;
use FOS\RestBundle\Controller\Annotations\Options;
use FOS\RestBundle\Controller\Annotations\Patch;
use FOS\RestBundle\Controller\Annotations\Post;
use FOS\RestBundle\Controller\Annotations\PropFind;
use FOS\RestBundle\Controller\Annotations\PropPatch;
use FOS\RestBundle\Controller\Annotations\Put;
use FOS\RestBundle\Controller\Annotations\Route;
use FOS\RestBundle\Controller\Annotations\Unlink;
use FOS\RestBundle\Controller\Annotations\Unlock;
use FOS\RestBundle\Controller\Annotations\View;

#[Route('/api/foo')]
class BarController
{
#[Copy]
public function copy()
{
}

#[Delete('/delete', name: 'foo_delete')]
public function delete()
{
}

#[Get('/list', name: 'foo_list')]
#[View(serializerEnableMaxDepthChecks: false)]
public function list()
{
}

#[Head]
public function foo()
{
}

#[Link]
public function addFriend()
{
}

#[Lock]
public function pessimisticLock()
{
}

#[Mkcol]
public function createList()
{
}

#[Move]
public function move()
{
}

#[Options]
public function options()
{
}

#[Patch('/update', name: 'foo_update')]
public function update()
{
}

#[Post('/create', name: 'foo_create')]
public function create()
{
}

#[PropFind]
public function propFind()
{
}

#[PropPatch]
public function propPatch()
{
}

#[Put('/replace', name: 'foo_replace')]
public function replace()
{
}

#[Unlink]
public function removeFriend()
{
}

#[Unlock]
public function unlock()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Set\FOSRestSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../config/config.php');
$rectorConfig->sets([FOSRestSetList::ANNOTATIONS_TO_ATTRIBUTES]);
};

0 comments on commit 8b63302

Please sign in to comment.