Skip to content

Add scalar typehints/return types #645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Controller/Admin/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Controller used to manage blog contents in the backend.
Expand Down Expand Up @@ -53,7 +54,7 @@ class BlogController extends Controller
* @Route("/", name="admin_post_index")
* @Method("GET")
*/
public function indexAction()
public function indexAction(): Response
{
$em = $this->getDoctrine()->getManager();
$posts = $em->getRepository(Post::class)->findBy(['author' => $this->getUser()], ['publishedAt' => 'DESC']);
Expand All @@ -71,7 +72,7 @@ public function indexAction()
* to constraint the HTTP methods each controller responds to (by default
* it responds to all methods).
*/
public function newAction(Request $request)
public function newAction(Request $request): Response
{
$post = new Post();
$post->setAuthor($this->getUser());
Expand Down Expand Up @@ -118,7 +119,7 @@ public function newAction(Request $request)
* @Route("/{id}", requirements={"id": "\d+"}, name="admin_post_show")
* @Method("GET")
*/
public function showAction(Post $post)
public function showAction(Post $post): Response
{
// This security check can also be performed
// using an annotation: @Security("is_granted('show', post)")
Expand All @@ -135,7 +136,7 @@ public function showAction(Post $post)
* @Route("/{id}/edit", requirements={"id": "\d+"}, name="admin_post_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, Post $post)
public function editAction(Request $request, Post $post): Response
{
$this->denyAccessUnlessGranted('edit', $post, 'Posts can only be edited by their authors.');

Expand Down Expand Up @@ -167,7 +168,7 @@ public function editAction(Request $request, Post $post)
* The Security annotation value is an expression (if it evaluates to false,
* the authorization mechanism will prevent the user accessing this resource).
*/
public function deleteAction(Request $request, Post $post)
public function deleteAction(Request $request, Post $post): Response
{
if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {
return $this->redirectToRoute('admin_post_index');
Expand Down
17 changes: 5 additions & 12 deletions src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand All @@ -48,7 +47,7 @@ class BlogController extends Controller
* Content-Type header for the response.
* See https://symfony.com/doc/current/quick_tour/the_controller.html#using-formats
*/
public function indexAction($page, $_format)
public function indexAction(int $page, string $_format): Response
{
$em = $this->getDoctrine()->getManager();
$posts = $em->getRepository(Post::class)->findLatest($page);
Expand All @@ -68,7 +67,7 @@ public function indexAction($page, $_format)
* value given in the route.
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
*/
public function postShowAction(Post $post)
public function postShowAction(Post $post): Response
{
// Symfony provides a function called 'dump()' which is an improved version
// of the 'var_dump()' function. It's useful to quickly debug the contents
Expand All @@ -93,7 +92,7 @@ public function postShowAction(Post $post)
* (postSlug) doesn't match any of the Doctrine entity properties (slug).
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter
*/
public function commentNewAction(Request $request, Post $post, EventDispatcherInterface $eventDispatcher)
public function commentNewAction(Request $request, Post $post, EventDispatcherInterface $eventDispatcher): Response
{
$comment = new Comment();
$comment->setAuthor($this->getUser());
Expand Down Expand Up @@ -137,12 +136,8 @@ public function commentNewAction(Request $request, Post $post, EventDispatcherIn
*
* The "id" of the Post is passed in and then turned into a Post object
* automatically by the ParamConverter.
*
* @param Post $post
*
* @return Response
*/
public function commentFormAction(Post $post)
public function commentFormAction(Post $post): Response
{
$form = $this->createForm(CommentType::class);

Expand All @@ -155,10 +150,8 @@ public function commentFormAction(Post $post)
/**
* @Route("/search", name="blog_search")
* @Method("GET")
*
* @return Response|JsonResponse
*/
public function searchAction(Request $request)
public function searchAction(Request $request): Response
{
if (!$request->isXmlHttpRequest()) {
return $this->render('blog/search.html.twig');
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/SecurityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;

/**
Expand All @@ -27,7 +28,7 @@ class SecurityController extends Controller
/**
* @Route("/login", name="security_login")
*/
public function loginAction(AuthenticationUtils $helper)
public function loginAction(AuthenticationUtils $helper): Response
{
return $this->render('security/login.html.twig', [
// last username entered by the user (if any)
Expand All @@ -45,7 +46,7 @@ public function loginAction(AuthenticationUtils $helper)
*
* @Route("/logout", name="security_logout")
*/
public function logoutAction()
public function logoutAction(): void
{
throw new \Exception('This should never be reached!');
}
Expand Down
12 changes: 6 additions & 6 deletions src/DataFixtures/FixturesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
trait FixturesTrait
{
private function getPostContent()
private function getPostContent(): string
{
return <<<'MARKDOWN'
Lorem ipsum dolor sit amet consectetur adipisicing elit, sed do eiusmod tempor
Expand Down Expand Up @@ -56,7 +56,7 @@ private function getPostContent()
MARKDOWN;
}

private function getPhrases()
private function getPhrases(): array
{
return [
'Lorem ipsum dolor sit amet consectetur adipiscing elit',
Expand Down Expand Up @@ -92,7 +92,7 @@ private function getPhrases()
];
}

private function getTagNames()
private function getTagNames(): array
{
return [
'lorem',
Expand All @@ -107,7 +107,7 @@ private function getTagNames()
];
}

private function getRandomPostTitles()
private function getRandomPostTitles(): array
{
$phrases = $this->getPhrases();

Expand All @@ -119,7 +119,7 @@ private function getRandomPostTitles()
return $phrases;
}

private function getRandomPostSummary($maxLength = 255)
private function getRandomPostSummary(int $maxLength = 255): string
{
$phrases = $this->getPhrases();

Expand All @@ -134,7 +134,7 @@ private function getRandomPostSummary($maxLength = 255)
return $summary;
}

private function getRandomCommentContent()
private function getRandomCommentContent(): string
{
$phrases = $this->getPhrases();

Expand Down
11 changes: 5 additions & 6 deletions src/DataFixtures/ORM/PostFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\DataFixtures\FixturesTrait;
use App\Entity\Comment;
use App\Entity\Post;
use App\Entity\User;
use App\Utils\Slugger;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
Expand All @@ -38,7 +39,7 @@ class PostFixtures extends AbstractFixture implements DependentFixtureInterface
/**
* {@inheritdoc}
*/
public function load(ObjectManager $manager)
public function load(ObjectManager $manager): void
{
foreach ($this->getRandomPostTitles() as $i => $title) {
$post = new Post();
Expand Down Expand Up @@ -82,26 +83,24 @@ public function load(ObjectManager $manager)
* Instead of defining the exact order in which the fixtures files must be loaded,
* this method defines which other fixtures this file depends on. Then, Doctrine
* will figure out the best order to fit all the dependencies.
*
* @return array
*/
public function getDependencies()
public function getDependencies(): array
{
return [
TagFixtures::class,
UserFixtures::class,
];
}

private function getRandomUser()
private function getRandomUser(): User
{
$admins = ['jane-admin', 'tom-admin'];
$index = array_rand($admins);

return $this->getReference($admins[$index]);
}

private function getRandomTags($numTags = 0)
private function getRandomTags(int $numTags = 0): array
{
$tags = [];

Expand Down
2 changes: 1 addition & 1 deletion src/DataFixtures/ORM/TagFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TagFixtures extends AbstractFixture
/**
* {@inheritdoc}
*/
public function load(ObjectManager $manager)
public function load(ObjectManager $manager): void
{
foreach ($this->getTagNames() as $index => $name) {
$tag = new Tag();
Expand Down
2 changes: 1 addition & 1 deletion src/DataFixtures/ORM/UserFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class UserFixtures extends AbstractFixture implements ContainerAwareInterface
/**
* {@inheritdoc}
*/
public function load(ObjectManager $manager)
public function load(ObjectManager $manager): void
{
$passwordEncoder = $this->container->get('security.password_encoder');

Expand Down
29 changes: 10 additions & 19 deletions src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,63 +84,54 @@ public function __construct()
/**
* @Assert\IsTrue(message="comment.is_spam")
*/
public function isLegitComment()
public function isLegitComment(): bool
{
$containsInvalidCharacters = false !== mb_strpos($this->content, '@');

return !$containsInvalidCharacters;
}

public function getId()
public function getId(): int
{
return $this->id;
}

public function getContent()
public function getContent(): ?string
{
return $this->content;
}

/**
* @param string $content
*/
public function setContent($content)
public function setContent(string $content): void
{
$this->content = $content;
}

public function getPublishedAt()
public function getPublishedAt(): \DateTime
{
return $this->publishedAt;
}

public function setPublishedAt(\DateTime $publishedAt)
public function setPublishedAt(\DateTime $publishedAt): void
{
$this->publishedAt = $publishedAt;
}

/**
* @return User
*/
public function getAuthor()
public function getAuthor(): User
{
return $this->author;
}

/**
* @param User $author
*/
public function setAuthor(User $author)
public function setAuthor(User $author): void
{
$this->author = $author;
}

public function getPost()
public function getPost(): Post
{
return $this->post;
}

public function setPost(Post $post)
public function setPost(Post $post): void
{
$this->post = $post;
}
Expand Down
Loading