Skip to content

Commit 2273e82

Browse files
committed
feature #645 Add scalar typehints/return types (yceruto)
This PR was squashed before being merged into the master branch (closes #645). Discussion ---------- Add scalar typehints/return types Fixes #639 (comment) Commits ------- 5e93c07 Add scalar typehints/return types
2 parents 090bc25 + 5e93c07 commit 2273e82

24 files changed

+126
-233
lines changed

src/Controller/Admin/BlogController.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
2121
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
2222
use Symfony\Component\HttpFoundation\Request;
23+
use Symfony\Component\HttpFoundation\Response;
2324

2425
/**
2526
* Controller used to manage blog contents in the backend.
@@ -53,7 +54,7 @@ class BlogController extends Controller
5354
* @Route("/", name="admin_post_index")
5455
* @Method("GET")
5556
*/
56-
public function indexAction()
57+
public function indexAction(): Response
5758
{
5859
$em = $this->getDoctrine()->getManager();
5960
$posts = $em->getRepository(Post::class)->findBy(['author' => $this->getUser()], ['publishedAt' => 'DESC']);
@@ -71,7 +72,7 @@ public function indexAction()
7172
* to constraint the HTTP methods each controller responds to (by default
7273
* it responds to all methods).
7374
*/
74-
public function newAction(Request $request)
75+
public function newAction(Request $request): Response
7576
{
7677
$post = new Post();
7778
$post->setAuthor($this->getUser());
@@ -118,7 +119,7 @@ public function newAction(Request $request)
118119
* @Route("/{id}", requirements={"id": "\d+"}, name="admin_post_show")
119120
* @Method("GET")
120121
*/
121-
public function showAction(Post $post)
122+
public function showAction(Post $post): Response
122123
{
123124
// This security check can also be performed
124125
// using an annotation: @Security("is_granted('show', post)")
@@ -135,7 +136,7 @@ public function showAction(Post $post)
135136
* @Route("/{id}/edit", requirements={"id": "\d+"}, name="admin_post_edit")
136137
* @Method({"GET", "POST"})
137138
*/
138-
public function editAction(Request $request, Post $post)
139+
public function editAction(Request $request, Post $post): Response
139140
{
140141
$this->denyAccessUnlessGranted('edit', $post, 'Posts can only be edited by their authors.');
141142

@@ -167,7 +168,7 @@ public function editAction(Request $request, Post $post)
167168
* The Security annotation value is an expression (if it evaluates to false,
168169
* the authorization mechanism will prevent the user accessing this resource).
169170
*/
170-
public function deleteAction(Request $request, Post $post)
171+
public function deleteAction(Request $request, Post $post): Response
171172
{
172173
if (!$this->isCsrfTokenValid('delete', $request->request->get('token'))) {
173174
return $this->redirectToRoute('admin_post_index');

src/Controller/BlogController.php

+5-12
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
2424
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2525
use Symfony\Component\EventDispatcher\GenericEvent;
26-
use Symfony\Component\HttpFoundation\JsonResponse;
2726
use Symfony\Component\HttpFoundation\Request;
2827
use Symfony\Component\HttpFoundation\Response;
2928

@@ -48,7 +47,7 @@ class BlogController extends Controller
4847
* Content-Type header for the response.
4948
* See https://symfony.com/doc/current/quick_tour/the_controller.html#using-formats
5049
*/
51-
public function indexAction($page, $_format)
50+
public function indexAction(int $page, string $_format): Response
5251
{
5352
$em = $this->getDoctrine()->getManager();
5453
$posts = $em->getRepository(Post::class)->findLatest($page);
@@ -68,7 +67,7 @@ public function indexAction($page, $_format)
6867
* value given in the route.
6968
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
7069
*/
71-
public function postShowAction(Post $post)
70+
public function postShowAction(Post $post): Response
7271
{
7372
// Symfony provides a function called 'dump()' which is an improved version
7473
// of the 'var_dump()' function. It's useful to quickly debug the contents
@@ -93,7 +92,7 @@ public function postShowAction(Post $post)
9392
* (postSlug) doesn't match any of the Doctrine entity properties (slug).
9493
* See https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#doctrine-converter
9594
*/
96-
public function commentNewAction(Request $request, Post $post, EventDispatcherInterface $eventDispatcher)
95+
public function commentNewAction(Request $request, Post $post, EventDispatcherInterface $eventDispatcher): Response
9796
{
9897
$comment = new Comment();
9998
$comment->setAuthor($this->getUser());
@@ -137,12 +136,8 @@ public function commentNewAction(Request $request, Post $post, EventDispatcherIn
137136
*
138137
* The "id" of the Post is passed in and then turned into a Post object
139138
* automatically by the ParamConverter.
140-
*
141-
* @param Post $post
142-
*
143-
* @return Response
144139
*/
145-
public function commentFormAction(Post $post)
140+
public function commentFormAction(Post $post): Response
146141
{
147142
$form = $this->createForm(CommentType::class);
148143

@@ -155,10 +150,8 @@ public function commentFormAction(Post $post)
155150
/**
156151
* @Route("/search", name="blog_search")
157152
* @Method("GET")
158-
*
159-
* @return Response|JsonResponse
160153
*/
161-
public function searchAction(Request $request)
154+
public function searchAction(Request $request): Response
162155
{
163156
if (!$request->isXmlHttpRequest()) {
164157
return $this->render('blog/search.html.twig');

src/Controller/SecurityController.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
1515
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
16+
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
1718

1819
/**
@@ -27,7 +28,7 @@ class SecurityController extends Controller
2728
/**
2829
* @Route("/login", name="security_login")
2930
*/
30-
public function loginAction(AuthenticationUtils $helper)
31+
public function loginAction(AuthenticationUtils $helper): Response
3132
{
3233
return $this->render('security/login.html.twig', [
3334
// last username entered by the user (if any)
@@ -45,7 +46,7 @@ public function loginAction(AuthenticationUtils $helper)
4546
*
4647
* @Route("/logout", name="security_logout")
4748
*/
48-
public function logoutAction()
49+
public function logoutAction(): void
4950
{
5051
throw new \Exception('This should never be reached!');
5152
}

src/DataFixtures/FixturesTrait.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
trait FixturesTrait
1818
{
19-
private function getPostContent()
19+
private function getPostContent(): string
2020
{
2121
return <<<'MARKDOWN'
2222
Lorem ipsum dolor sit amet consectetur adipisicing elit, sed do eiusmod tempor
@@ -56,7 +56,7 @@ private function getPostContent()
5656
MARKDOWN;
5757
}
5858

59-
private function getPhrases()
59+
private function getPhrases(): array
6060
{
6161
return [
6262
'Lorem ipsum dolor sit amet consectetur adipiscing elit',
@@ -92,7 +92,7 @@ private function getPhrases()
9292
];
9393
}
9494

95-
private function getTagNames()
95+
private function getTagNames(): array
9696
{
9797
return [
9898
'lorem',
@@ -107,7 +107,7 @@ private function getTagNames()
107107
];
108108
}
109109

110-
private function getRandomPostTitles()
110+
private function getRandomPostTitles(): array
111111
{
112112
$phrases = $this->getPhrases();
113113

@@ -119,7 +119,7 @@ private function getRandomPostTitles()
119119
return $phrases;
120120
}
121121

122-
private function getRandomPostSummary($maxLength = 255)
122+
private function getRandomPostSummary(int $maxLength = 255): string
123123
{
124124
$phrases = $this->getPhrases();
125125

@@ -134,7 +134,7 @@ private function getRandomPostSummary($maxLength = 255)
134134
return $summary;
135135
}
136136

137-
private function getRandomCommentContent()
137+
private function getRandomCommentContent(): string
138138
{
139139
$phrases = $this->getPhrases();
140140

src/DataFixtures/ORM/PostFixtures.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\DataFixtures\FixturesTrait;
1515
use App\Entity\Comment;
1616
use App\Entity\Post;
17+
use App\Entity\User;
1718
use App\Utils\Slugger;
1819
use Doctrine\Common\DataFixtures\AbstractFixture;
1920
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
@@ -38,7 +39,7 @@ class PostFixtures extends AbstractFixture implements DependentFixtureInterface
3839
/**
3940
* {@inheritdoc}
4041
*/
41-
public function load(ObjectManager $manager)
42+
public function load(ObjectManager $manager): void
4243
{
4344
foreach ($this->getRandomPostTitles() as $i => $title) {
4445
$post = new Post();
@@ -82,26 +83,24 @@ public function load(ObjectManager $manager)
8283
* Instead of defining the exact order in which the fixtures files must be loaded,
8384
* this method defines which other fixtures this file depends on. Then, Doctrine
8485
* will figure out the best order to fit all the dependencies.
85-
*
86-
* @return array
8786
*/
88-
public function getDependencies()
87+
public function getDependencies(): array
8988
{
9089
return [
9190
TagFixtures::class,
9291
UserFixtures::class,
9392
];
9493
}
9594

96-
private function getRandomUser()
95+
private function getRandomUser(): User
9796
{
9897
$admins = ['jane-admin', 'tom-admin'];
9998
$index = array_rand($admins);
10099

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

104-
private function getRandomTags($numTags = 0)
103+
private function getRandomTags(int $numTags = 0): array
105104
{
106105
$tags = [];
107106

src/DataFixtures/ORM/TagFixtures.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class TagFixtures extends AbstractFixture
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
public function load(ObjectManager $manager)
36+
public function load(ObjectManager $manager): void
3737
{
3838
foreach ($this->getTagNames() as $index => $name) {
3939
$tag = new Tag();

src/DataFixtures/ORM/UserFixtures.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class UserFixtures extends AbstractFixture implements ContainerAwareInterface
3636
/**
3737
* {@inheritdoc}
3838
*/
39-
public function load(ObjectManager $manager)
39+
public function load(ObjectManager $manager): void
4040
{
4141
$passwordEncoder = $this->container->get('security.password_encoder');
4242

src/Entity/Comment.php

+10-19
Original file line numberDiff line numberDiff line change
@@ -84,63 +84,54 @@ public function __construct()
8484
/**
8585
* @Assert\IsTrue(message="comment.is_spam")
8686
*/
87-
public function isLegitComment()
87+
public function isLegitComment(): bool
8888
{
8989
$containsInvalidCharacters = false !== mb_strpos($this->content, '@');
9090

9191
return !$containsInvalidCharacters;
9292
}
9393

94-
public function getId()
94+
public function getId(): int
9595
{
9696
return $this->id;
9797
}
9898

99-
public function getContent()
99+
public function getContent(): ?string
100100
{
101101
return $this->content;
102102
}
103103

104-
/**
105-
* @param string $content
106-
*/
107-
public function setContent($content)
104+
public function setContent(string $content): void
108105
{
109106
$this->content = $content;
110107
}
111108

112-
public function getPublishedAt()
109+
public function getPublishedAt(): \DateTime
113110
{
114111
return $this->publishedAt;
115112
}
116113

117-
public function setPublishedAt(\DateTime $publishedAt)
114+
public function setPublishedAt(\DateTime $publishedAt): void
118115
{
119116
$this->publishedAt = $publishedAt;
120117
}
121118

122-
/**
123-
* @return User
124-
*/
125-
public function getAuthor()
119+
public function getAuthor(): User
126120
{
127121
return $this->author;
128122
}
129123

130-
/**
131-
* @param User $author
132-
*/
133-
public function setAuthor(User $author)
124+
public function setAuthor(User $author): void
134125
{
135126
$this->author = $author;
136127
}
137128

138-
public function getPost()
129+
public function getPost(): Post
139130
{
140131
return $this->post;
141132
}
142133

143-
public function setPost(Post $post)
134+
public function setPost(Post $post): void
144135
{
145136
$this->post = $post;
146137
}

0 commit comments

Comments
 (0)