Skip to content

Commit de0d0d5

Browse files
committed
minor #757 Minor code fixes found with PhpStorm, Php Inspections and PHPStan (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #757). Discussion ---------- Minor code fixes found with PhpStorm, Php Inspections and PHPStan Commits ------- 953009f Minor code fixes found with PhpStorm, Php Inspections and PHPStan
2 parents e366044 + 953009f commit de0d0d5

File tree

11 files changed

+27
-27
lines changed

11 files changed

+27
-27
lines changed

src/Controller/BlogController.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,10 @@ public function search(Request $request, PostRepository $posts): Response
161161
$results = [];
162162
foreach ($foundPosts as $post) {
163163
$results[] = [
164-
'title' => htmlspecialchars($post->getTitle()),
164+
'title' => htmlspecialchars($post->getTitle(), ENT_COMPAT | ENT_HTML5),
165165
'date' => $post->getPublishedAt()->format('M d, Y'),
166-
'author' => htmlspecialchars($post->getAuthor()->getFullName()),
167-
'summary' => htmlspecialchars($post->getSummary()),
166+
'author' => htmlspecialchars($post->getAuthor()->getFullName(), ENT_COMPAT | ENT_HTML5),
167+
'summary' => htmlspecialchars($post->getSummary(), ENT_COMPAT | ENT_HTML5),
168168
'url' => $this->generateUrl('blog_post', ['slug' => $post->getSlug()]),
169169
];
170170
}

src/DataFixtures/AppFixtures.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private function loadPosts(ObjectManager $manager)
8282
$comment = new Comment();
8383
$comment->setAuthor($this->getReference('john_user'));
8484
$comment->setContent($this->getRandomText(random_int(255, 512)));
85-
$comment->setPublishedAt(new \DateTime('now + '.($i).'seconds'));
85+
$comment->setPublishedAt(new \DateTime('now + '.$i.'seconds'));
8686

8787
$post->addComment($comment);
8888
}

src/Entity/Comment.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ public function setAuthor(User $author): void
126126
$this->author = $author;
127127
}
128128

129-
public function getPost(): Post
129+
public function getPost(): ?Post
130130
{
131131
return $this->post;
132132
}
133133

134-
public function setPost(Post $post): void
134+
public function setPost(?Post $post): void
135135
{
136136
$this->post = $post;
137137
}

src/Entity/Post.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Post
3939
*
4040
* See https://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options
4141
*/
42-
const NUM_ITEMS = 10;
42+
public const NUM_ITEMS = 10;
4343

4444
/**
4545
* @var int
@@ -138,7 +138,7 @@ public function getTitle(): ?string
138138
return $this->title;
139139
}
140140

141-
public function setTitle(string $title): void
141+
public function setTitle(?string $title): void
142142
{
143143
$this->title = $title;
144144
}
@@ -148,7 +148,7 @@ public function getSlug(): ?string
148148
return $this->slug;
149149
}
150150

151-
public function setSlug(string $slug): void
151+
public function setSlug(?string $slug): void
152152
{
153153
$this->slug = $slug;
154154
}
@@ -158,7 +158,7 @@ public function getContent(): ?string
158158
return $this->content;
159159
}
160160

161-
public function setContent(string $content): void
161+
public function setContent(?string $content): void
162162
{
163163
$this->content = $content;
164164
}
@@ -168,7 +168,7 @@ public function getPublishedAt(): \DateTime
168168
return $this->publishedAt;
169169
}
170170

171-
public function setPublishedAt(\DateTime $publishedAt): void
171+
public function setPublishedAt(?\DateTime $publishedAt): void
172172
{
173173
$this->publishedAt = $publishedAt;
174174
}
@@ -178,7 +178,7 @@ public function getAuthor(): User
178178
return $this->author;
179179
}
180180

181-
public function setAuthor(User $author): void
181+
public function setAuthor(?User $author): void
182182
{
183183
$this->author = $author;
184184
}
@@ -188,7 +188,7 @@ public function getComments(): Collection
188188
return $this->comments;
189189
}
190190

191-
public function addComment(Comment $comment): void
191+
public function addComment(?Comment $comment): void
192192
{
193193
$comment->setPost($this);
194194
if (!$this->comments->contains($comment)) {
@@ -207,12 +207,12 @@ public function getSummary(): ?string
207207
return $this->summary;
208208
}
209209

210-
public function setSummary(string $summary): void
210+
public function setSummary(?string $summary): void
211211
{
212212
$this->summary = $summary;
213213
}
214214

215-
public function addTag(Tag ...$tags): void
215+
public function addTag(?Tag ...$tags): void
216216
{
217217
foreach ($tags as $tag) {
218218
if (!$this->tags->contains($tag)) {

src/Events.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ final class Events
2828
*
2929
* @var string
3030
*/
31-
const COMMENT_CREATED = 'comment.created';
31+
public const COMMENT_CREATED = 'comment.created';
3232
}

src/Security/PostVoter.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ class PostVoter extends Voter
2828
{
2929
// Defining these constants is overkill for this simple application, but for real
3030
// applications, it's a recommended practice to avoid relying on "magic strings"
31-
const SHOW = 'show';
32-
const EDIT = 'edit';
33-
const DELETE = 'delete';
31+
private const SHOW = 'show';
32+
private const EDIT = 'edit';
33+
private const DELETE = 'delete';
3434

3535
/**
3636
* {@inheritdoc}

src/Twig/SourceCodeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function unindentCode(string $code): string
117117
$codeLines = explode("\n", $code);
118118

119119
$indentedLines = array_filter($codeLines, function ($lineOfCode) {
120-
return '' === $lineOfCode || ' ' === mb_substr($lineOfCode, 0, 4);
120+
return '' === $lineOfCode || 0 === mb_strpos($lineOfCode, ' ');
121121
});
122122

123123
if (count($indentedLines) === count($codeLines)) {

tests/Command/AddUserCommandTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function setUp()
4646
* This test provides all the arguments required by the command, so the
4747
* command runs non-interactively and it won't ask for any argument.
4848
*/
49-
public function testCreateUserNonInteractive($isAdmin)
49+
public function testCreateUserNonInteractive(bool $isAdmin)
5050
{
5151
$input = $this->userData;
5252
if ($isAdmin) {
@@ -65,7 +65,7 @@ public function testCreateUserNonInteractive($isAdmin)
6565
* arguments.
6666
* See https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
6767
*/
68-
public function testCreateUserInteractive($isAdmin)
68+
public function testCreateUserInteractive(bool $isAdmin)
6969
{
7070
$this->executeCommand(
7171
// these are the arguments (only 1 is passed, the rest are missing)
@@ -92,7 +92,7 @@ public function isAdminDataProvider()
9292
* This helper method checks that the user was correctly created and saved
9393
* in the database.
9494
*/
95-
private function assertUserCreated($isAdmin)
95+
private function assertUserCreated(bool $isAdmin)
9696
{
9797
$container = self::$kernel->getContainer();
9898

tests/Controller/Admin/BlogControllerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class BlogControllerTest extends WebTestCase
3535
/**
3636
* @dataProvider getUrlsForRegularUsers
3737
*/
38-
public function testAccessDeniedForRegularUsers($httpMethod, $url)
38+
public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
3939
{
4040
$client = static::createClient([], [
4141
'PHP_AUTH_USER' => 'john_user',

tests/Controller/DefaultControllerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class DefaultControllerTest extends WebTestCase
3434
*
3535
* @dataProvider getPublicUrls
3636
*/
37-
public function testPublicUrls($url)
37+
public function testPublicUrls(string $url)
3838
{
3939
$client = static::createClient();
4040
$client->request('GET', $url);
@@ -70,7 +70,7 @@ public function testPublicBlogPost()
7070
*
7171
* @dataProvider getSecureUrls
7272
*/
73-
public function testSecureUrls($url)
73+
public function testSecureUrls(string $url)
7474
{
7575
$client = static::createClient();
7676
$client->request('GET', $url);

tests/Utils/SluggerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class SluggerTest extends TestCase
2929
/**
3030
* @dataProvider getSlugs
3131
*/
32-
public function testSlugify($string, $slug)
32+
public function testSlugify(string $string, string $slug)
3333
{
3434
$this->assertSame($slug, Slugger::slugify($string));
3535
}

0 commit comments

Comments
 (0)