Skip to content
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

Minor code fixes found with PhpStorm, Php Inspections and PHPStan #757

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/Controller/BlogController.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ public function search(Request $request, PostRepository $posts): Response
$results = [];
foreach ($foundPosts as $post) {
$results[] = [
'title' => htmlspecialchars($post->getTitle()),
'title' => htmlspecialchars($post->getTitle(), ENT_COMPAT | ENT_HTML5),
'date' => $post->getPublishedAt()->format('M d, Y'),
'author' => htmlspecialchars($post->getAuthor()->getFullName()),
'summary' => htmlspecialchars($post->getSummary()),
'author' => htmlspecialchars($post->getAuthor()->getFullName(), ENT_COMPAT | ENT_HTML5),
'summary' => htmlspecialchars($post->getSummary(), ENT_COMPAT | ENT_HTML5),
'url' => $this->generateUrl('blog_post', ['slug' => $post->getSlug()]),
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function loadPosts(ObjectManager $manager)
$comment = new Comment();
$comment->setAuthor($this->getReference('john_user'));
$comment->setContent($this->getRandomText(random_int(255, 512)));
$comment->setPublishedAt(new \DateTime('now + '.($i).'seconds'));
$comment->setPublishedAt(new \DateTime('now + '.$i.'seconds'));

$post->addComment($comment);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ public function setAuthor(User $author): void
$this->author = $author;
}

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

public function setPost(Post $post): void
public function setPost(?Post $post): void
Copy link
Member

@yceruto yceruto Jan 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, could we do the same on Post::setTitle() & Co. to fix #741?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Done.

{
$this->post = $post;
}
Expand Down
18 changes: 9 additions & 9 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Post
*
* See https://symfony.com/doc/current/best_practices/configuration.html#constants-vs-configuration-options
*/
const NUM_ITEMS = 10;
public const NUM_ITEMS = 10;

/**
* @var int
Expand Down Expand Up @@ -138,7 +138,7 @@ public function getTitle(): ?string
return $this->title;
}

public function setTitle(string $title): void
public function setTitle(?string $title): void
{
$this->title = $title;
}
Expand All @@ -148,7 +148,7 @@ public function getSlug(): ?string
return $this->slug;
}

public function setSlug(string $slug): void
public function setSlug(?string $slug): void
{
$this->slug = $slug;
}
Expand All @@ -158,7 +158,7 @@ public function getContent(): ?string
return $this->content;
}

public function setContent(string $content): void
public function setContent(?string $content): void
{
$this->content = $content;
}
Expand All @@ -168,7 +168,7 @@ public function getPublishedAt(): \DateTime
return $this->publishedAt;
}

public function setPublishedAt(\DateTime $publishedAt): void
public function setPublishedAt(?\DateTime $publishedAt): void
{
$this->publishedAt = $publishedAt;
}
Expand All @@ -178,7 +178,7 @@ public function getAuthor(): User
return $this->author;
}

public function setAuthor(User $author): void
public function setAuthor(?User $author): void
{
$this->author = $author;
}
Expand All @@ -188,7 +188,7 @@ public function getComments(): Collection
return $this->comments;
}

public function addComment(Comment $comment): void
public function addComment(?Comment $comment): void
{
$comment->setPost($this);
if (!$this->comments->contains($comment)) {
Expand All @@ -207,12 +207,12 @@ public function getSummary(): ?string
return $this->summary;
}

public function setSummary(string $summary): void
public function setSummary(?string $summary): void
{
$this->summary = $summary;
}

public function addTag(Tag ...$tags): void
public function addTag(?Tag ...$tags): void
{
foreach ($tags as $tag) {
if (!$this->tags->contains($tag)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ final class Events
*
* @var string
*/
const COMMENT_CREATED = 'comment.created';
public const COMMENT_CREATED = 'comment.created';
}
6 changes: 3 additions & 3 deletions src/Security/PostVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class PostVoter extends Voter
{
// Defining these constants is overkill for this simple application, but for real
// applications, it's a recommended practice to avoid relying on "magic strings"
const SHOW = 'show';
const EDIT = 'edit';
const DELETE = 'delete';
private const SHOW = 'show';
private const EDIT = 'edit';
private const DELETE = 'delete';

/**
* {@inheritdoc}
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/SourceCodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private function unindentCode(string $code): string
$codeLines = explode("\n", $code);

$indentedLines = array_filter($codeLines, function ($lineOfCode) {
return '' === $lineOfCode || ' ' === mb_substr($lineOfCode, 0, 4);
return '' === $lineOfCode || 0 === mb_strpos($lineOfCode, ' ');
});

if (count($indentedLines) === count($codeLines)) {
Expand Down
6 changes: 3 additions & 3 deletions tests/Command/AddUserCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function setUp()
* This test provides all the arguments required by the command, so the
* command runs non-interactively and it won't ask for any argument.
*/
public function testCreateUserNonInteractive($isAdmin)
public function testCreateUserNonInteractive(bool $isAdmin)
{
$input = $this->userData;
if ($isAdmin) {
Expand All @@ -65,7 +65,7 @@ public function testCreateUserNonInteractive($isAdmin)
* arguments.
* See https://symfony.com/doc/current/components/console/helpers/questionhelper.html#testing-a-command-that-expects-input
*/
public function testCreateUserInteractive($isAdmin)
public function testCreateUserInteractive(bool $isAdmin)
{
$this->executeCommand(
// these are the arguments (only 1 is passed, the rest are missing)
Expand All @@ -92,7 +92,7 @@ public function isAdminDataProvider()
* This helper method checks that the user was correctly created and saved
* in the database.
*/
private function assertUserCreated($isAdmin)
private function assertUserCreated(bool $isAdmin)
{
$container = self::$kernel->getContainer();

Expand Down
2 changes: 1 addition & 1 deletion tests/Controller/Admin/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class BlogControllerTest extends WebTestCase
/**
* @dataProvider getUrlsForRegularUsers
*/
public function testAccessDeniedForRegularUsers($httpMethod, $url)
public function testAccessDeniedForRegularUsers(string $httpMethod, string $url)
{
$client = static::createClient([], [
'PHP_AUTH_USER' => 'john_user',
Expand Down
4 changes: 2 additions & 2 deletions tests/Controller/DefaultControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DefaultControllerTest extends WebTestCase
*
* @dataProvider getPublicUrls
*/
public function testPublicUrls($url)
public function testPublicUrls(string $url)
{
$client = static::createClient();
$client->request('GET', $url);
Expand Down Expand Up @@ -70,7 +70,7 @@ public function testPublicBlogPost()
*
* @dataProvider getSecureUrls
*/
public function testSecureUrls($url)
public function testSecureUrls(string $url)
{
$client = static::createClient();
$client->request('GET', $url);
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/SluggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SluggerTest extends TestCase
/**
* @dataProvider getSlugs
*/
public function testSlugify($string, $slug)
public function testSlugify(string $string, string $slug)
{
$this->assertSame($slug, Slugger::slugify($string));
}
Expand Down