Skip to content

Commit

Permalink
add RouteName value object
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 13, 2021
1 parent 3b4f851 commit d926085
Show file tree
Hide file tree
Showing 24 changed files with 108 additions and 57 deletions.
7 changes: 4 additions & 3 deletions packages/blog/src/Controller/BlogController.php
Expand Up @@ -11,11 +11,12 @@

final class BlogController extends AbstractController
{
public function __construct(private PostRepository $postRepository)
{
public function __construct(
private PostRepository $postRepository
) {
}

#[Route('blog', name: 'blog')]
#[Route('blog', name: \Rector\Website\ValueObject\Routing\RouteName::BLOG)]
public function __invoke(): Response
{
return $this->render('blog/blog.twig', [
Expand Down
7 changes: 4 additions & 3 deletions packages/blog/src/Controller/PostController.php
Expand Up @@ -11,11 +11,12 @@

final class PostController extends AbstractController
{
public function __construct(private PostRepository $postRepository)
{
public function __construct(
private PostRepository $postRepository
) {
}

#[Route('blog/{postSlug}', name: 'post', requirements: [
#[Route('blog/{postSlug}', name: \Rector\Website\ValueObject\Routing\RouteName::POST, requirements: [
'postSlug' => '.+',
])]
public function __invoke(string $postSlug): Response
Expand Down
7 changes: 4 additions & 3 deletions packages/blog/src/Controller/RssController.php
Expand Up @@ -13,11 +13,12 @@

final class RssController extends AbstractController
{
public function __construct(private PostRepository $postRepository)
{
public function __construct(
private PostRepository $postRepository
) {
}

#[Route('rss.xml', name: 'rss')]
#[Route('rss.xml', name: \Rector\Website\ValueObject\Routing\RouteName::RSS)]
public function __invoke(): Response
{
$posts = $this->postRepository->getPosts();
Expand Down
6 changes: 4 additions & 2 deletions packages/blog/src/Repository/PostRepository.php
Expand Up @@ -22,8 +22,10 @@ final class PostRepository
*/
private array $posts = [];

public function __construct(private FinderSanitizer $finderSanitizer, private PostFactory $postFactory)
{
public function __construct(
private FinderSanitizer $finderSanitizer,
private PostFactory $postFactory
) {
$this->createPosts();
}

Expand Down
5 changes: 3 additions & 2 deletions packages/blog/src/Twig/MarkdownTwigExtension.php
Expand Up @@ -10,8 +10,9 @@

final class MarkdownTwigExtension extends AbstractExtension
{
public function __construct(private ParsedownExtra $parsedownExtra)
{
public function __construct(
private ParsedownExtra $parsedownExtra
) {
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/blog/src/Twig/RoutingTwigExtension.php
Expand Up @@ -12,8 +12,9 @@

final class RoutingTwigExtension extends AbstractExtension
{
public function __construct(private RequestStack $requestStack)
{
public function __construct(
private RequestStack $requestStack
) {
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/demo/src/DemoRunner.php
Expand Up @@ -11,8 +11,9 @@

final class DemoRunner
{
public function __construct(private RectorProcessRunner $rectorProcessRunner)
{
public function __construct(
private RectorProcessRunner $rectorProcessRunner
) {
}

public function processRectorRun(RectorRun $rectorRun): void
Expand Down
5 changes: 3 additions & 2 deletions packages/demo/src/Lint/PHPLinter.php
Expand Up @@ -18,8 +18,9 @@ final class PHPLinter
*/
private const OPENING_PHP_TAG_REGEX = '#(\s+)?\<\?php#';

public function __construct(private SmartFileSystem $smartFileSystem)
{
public function __construct(
private SmartFileSystem $smartFileSystem
) {
}

public function checkContentSyntax(string $content): void
Expand Down
5 changes: 3 additions & 2 deletions packages/demo/src/Repository/RectorRunRepository.php
Expand Up @@ -11,8 +11,9 @@

final class RectorRunRepository
{
public function __construct(private EntityManagerInterface $entityManager)
{
public function __construct(
private EntityManagerInterface $entityManager
) {
}

public function save(RectorRun $rectorRun): void
Expand Down
5 changes: 3 additions & 2 deletions packages/demo/src/ValueObject/AppliedRule.php
Expand Up @@ -17,8 +17,9 @@ final class AppliedRule

private string $shortClass;

public function __construct(private string $class)
{
public function __construct(
private string $class
) {
$shortClassName = Strings::after($class, '\\', -1);
if (! is_string($shortClassName)) {
throw new ShouldNotHappenException();
Expand Down
5 changes: 3 additions & 2 deletions packages/demo/src/ValueObjectFactory/RectorRunFactory.php
Expand Up @@ -19,8 +19,9 @@ final class RectorRunFactory
*/
public const CONFIG_FILE_PATH = __DIR__ . '/../../data/demo-config.php';

public function __construct(private SmartFileSystem $smartFileSystem)
{
public function __construct(
private SmartFileSystem $smartFileSystem
) {
}

public function createEmpty(): RectorRun
Expand Down
Expand Up @@ -15,8 +15,9 @@ final class IssueLinkFactory
*/
private const BASE_URL = 'https://github.com/rectorphp/rector/issues/new?labels=bug&template=1_Bug_report.md';

public function __construct(private IssueBodyFactory $issueBodyFactory)
{
public function __construct(
private IssueBodyFactory $issueBodyFactory
) {
}

public function create(RectorRun $rectorRun): string
Expand Down
Expand Up @@ -16,8 +16,9 @@ final class FixtureLinkTwigExtension extends AbstractExtension
*/
private const PR_LINK = 'pr_link';

public function __construct(private FixtureLinkFactory $fixtureLinkFactory)
{
public function __construct(
private FixtureLinkFactory $fixtureLinkFactory
) {
}

/**
Expand Down
Expand Up @@ -16,8 +16,9 @@ final class IssueLinkTwigExtension extends AbstractExtension
*/
private const ISSUE_LINK = 'issue_link';

public function __construct(private IssueLinkFactory $issueLinkFactory)
{
public function __construct(
private IssueLinkFactory $issueLinkFactory
) {
}

/**
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Expand Up @@ -17,3 +17,4 @@ parameters:
- '#Method Rector\\Website\\Demo\\Form\\DemoFormType\:\:buildForm\(\) has parameter \$formBuilder with no value type specified in iterable type Symfony\\Component\\Form\\FormBuilderInterface#'
- '#Method Rector\\Website\\Form\\ContactFormType\:\:buildForm\(\) has parameter \$formBuilder with no value type specified in iterable type Symfony\\Component\\Form\\FormBuilderInterface#'
- '#Method Rector\\Website\\Demo\\Controller\\DemoController\:\:processFormAndReturnRoute\(\) has parameter \$form with no value type specified in iterable type Symfony\\Component\\Form\\FormInterface#'
- '#Content of method "save\(\)" is duplicated with method "save\(\)" in "Rector\\Website\\Repository\\ContactMessageRepository" class\. Use unique content or abstract service instead#'
3 changes: 3 additions & 0 deletions rector.php
Expand Up @@ -7,6 +7,7 @@
use Rector\DeadCode\Rector\Class_\RemoveUnusedDoctrineEntityMethodAndPropertyRector;
use Rector\Generic\Rector\FuncCall\FuncCallToStaticCallRector;
use Rector\Set\ValueObject\SetList;
use Rector\SymfonyCodeQuality\Rector\Attribute\ExtractAttributeRouteNameConstantsRector;
use Rector\Transform\ValueObject\FuncCallToStaticCall;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\inline_service;
Expand Down Expand Up @@ -46,6 +47,8 @@

$services = $containerConfigurator->services();

$services->set(ExtractAttributeRouteNameConstantsRector::class);

$services->set(FuncCallToStaticCallRector::class)
->call('configure', [[
FuncCallToStaticCallRector::FUNC_CALLS_TO_STATIC_CALLS => [
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/BankDetailsController.php
Expand Up @@ -10,7 +10,7 @@

final class BankDetailsController extends AbstractController
{
#[Route(path: 'bank-details', name: 'bank_details')]
#[Route(path: 'bank-details', name: \Rector\Website\ValueObject\Routing\RouteName::BANK_DETAILS)]
public function __invoke(): Response
{
return $this->render('homepage/bank_details.twig');
Expand Down
10 changes: 5 additions & 5 deletions src/Controller/ContactController.php
Expand Up @@ -8,19 +8,19 @@
use Rector\Website\Exception\ShouldNotHappenException;
use Rector\Website\Form\ContactFormType;
use Rector\Website\Repository\ContactMessageRepository;
use Rector\Website\ValueObject\RouteName;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

final class ContactController extends AbstractController
{
public function __construct(private ContactMessageRepository $contactMessageRepository, )
{
public function __construct(
private ContactMessageRepository $contactMessageRepository,
) {
}

#[Route(path: 'contact', name: RouteName::CONTACT)]
#[Route(path: 'contact', name: \Rector\Website\ValueObject\Routing\RouteName::CONTACT)]
public function __invoke(Request $request): Response
{
$form = $this->createForm(ContactFormType::class);
Expand All @@ -35,7 +35,7 @@ public function __invoke(Request $request): Response
$this->contactMessageRepository->save($contactMessage);
$this->addFlash('success', 'Your message is on the way!');

return $this->redirectToRoute(RouteName::CONTACT);
return $this->redirectToRoute(\Rector\Website\ValueObject\Routing\RouteName::CONTACT);
}

return $this->render('homepage/contact.twig', [
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/HomepageController.php
Expand Up @@ -11,11 +11,12 @@

final class HomepageController extends AbstractController
{
public function __construct(private PostRepository $postRepository)
{
public function __construct(
private PostRepository $postRepository
) {
}

#[Route(path: '/', name: 'homepage')]
#[Route(path: '/', name: \Rector\Website\ValueObject\Routing\RouteName::HOMEPAGE)]
public function __invoke(): Response
{
return $this->render('homepage/homepage.twig', [
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/MissionController.php
Expand Up @@ -10,7 +10,7 @@

final class MissionController extends AbstractController
{
#[Route(path: 'mission', name: 'mission')]
#[Route(path: 'mission', name: \Rector\Website\ValueObject\Routing\RouteName::MISSION)]
public function __invoke(): Response
{
return $this->render('homepage/mission.twig');
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/RssController.php
Expand Up @@ -10,7 +10,7 @@

final class RssController extends AbstractController
{
#[Route(path: 'rss.xml', name: 'rss')]
#[Route(path: 'rss.xml', name: \Rector\Website\ValueObject\Routing\RouteName::RSS)]
public function __invoke(): Response
{
return $this->render('homepage/rss.twig');
Expand Down
5 changes: 3 additions & 2 deletions src/Repository/ContactMessageRepository.php
Expand Up @@ -9,8 +9,9 @@

final class ContactMessageRepository
{
public function __construct(private EntityManagerInterface $entityManager)
{
public function __construct(
private EntityManagerInterface $entityManager
) {
}

public function save(ContactMessage $contactMessage): void
Expand Down
13 changes: 0 additions & 13 deletions src/ValueObject/RouteName.php

This file was deleted.

43 changes: 43 additions & 0 deletions src/ValueObject/Routing/RouteName.php
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Rector\Website\ValueObject\Routing;

final class RouteName
{
/**
* @var string
*/
public const BANK_DETAILS = 'bank_details';

/**
* @var string
*/
public const HOMEPAGE = 'homepage';

/**
* @var string
*/
public const MISSION = 'mission';

/**
* @var string
*/
public const RSS = 'rss';

/**
* @var string
*/
public const BLOG = 'blog';

/**
* @var string
*/
public const POST = 'post';

/**
* @var string
*/
public const CONTACT = 'contact';
}

0 comments on commit d926085

Please sign in to comment.