Skip to content

Move some constant to a better place #1162

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

Merged
merged 1 commit into from
Oct 8, 2020
Merged
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
8 changes: 0 additions & 8 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@
*/
class Post
{
/**
* Use constants to define configuration options that rarely change instead
* of specifying them under parameters section in config/services.yaml file.
*
* See https://symfony.com/doc/current/best_practices.html#use-constants-to-define-options-that-rarely-change
*/
public const NUM_ITEMS = 10;

/**
* @var int
*
Expand Down
11 changes: 9 additions & 2 deletions src/Pagination/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace App\Pagination;

use App\Entity\Post;
use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder;
use Doctrine\ORM\Tools\Pagination\CountWalker;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
Expand All @@ -21,13 +20,21 @@
*/
class Paginator
{
/**
* Use constants to define configuration options that rarely change instead
* of specifying them under parameters section in config/services.yaml file.
*
* See https://symfony.com/doc/current/best_practices.html#use-constants-to-define-options-that-rarely-change
*/
public const PAGE_SIZE = 10;

private $queryBuilder;
private $currentPage;
private $pageSize;
private $results;
private $numResults;

public function __construct(DoctrineQueryBuilder $queryBuilder, int $pageSize = Post::NUM_ITEMS)
public function __construct(DoctrineQueryBuilder $queryBuilder, int $pageSize = self::PAGE_SIZE)
{
$this->queryBuilder = $queryBuilder;
$this->pageSize = $pageSize;
Expand Down
2 changes: 1 addition & 1 deletion src/Repository/PostRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function findLatest(int $page = 1, Tag $tag = null): Paginator
/**
* @return Post[]
*/
public function findBySearchQuery(string $query, int $limit = Post::NUM_ITEMS): array
public function findBySearchQuery(string $query, int $limit = Paginator::PAGE_SIZE): array
{
$searchTerms = $this->extractSearchTerms($query);

Expand Down
5 changes: 3 additions & 2 deletions tests/Controller/BlogControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace App\Tests\Controller;

use App\Entity\Post;
use App\Pagination\Paginator;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
Expand All @@ -34,7 +35,7 @@ public function testIndex(): void
$this->assertResponseIsSuccessful();

$this->assertCount(
Post::NUM_ITEMS,
Paginator::PAGE_SIZE,
$crawler->filter('article.post'),
'The homepage displays the right number of posts.'
);
Expand All @@ -48,7 +49,7 @@ public function testRss(): void
$this->assertResponseHeaderSame('Content-Type', 'text/xml; charset=UTF-8');

$this->assertCount(
Post::NUM_ITEMS,
Paginator::PAGE_SIZE,
$crawler->filter('item'),
'The xml file displays the right number of posts.'
);
Expand Down