Skip to content

Commit 559a2ac

Browse files
committed
minor #1162 Move some constant to a better place (javiereguiluz)
This PR was squashed before being merged into the master branch. Discussion ---------- Move some constant to a better place This continues #1160 to refactor some code. Commits ------- 0de89e1 Move some constant to a better place
2 parents 51fbfb5 + 0de89e1 commit 559a2ac

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

src/Entity/Post.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@
3535
*/
3636
class Post
3737
{
38-
/**
39-
* Use constants to define configuration options that rarely change instead
40-
* of specifying them under parameters section in config/services.yaml file.
41-
*
42-
* See https://symfony.com/doc/current/best_practices.html#use-constants-to-define-options-that-rarely-change
43-
*/
44-
public const NUM_ITEMS = 10;
45-
4638
/**
4739
* @var int
4840
*

src/Pagination/Paginator.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace App\Pagination;
1313

14-
use App\Entity\Post;
1514
use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder;
1615
use Doctrine\ORM\Tools\Pagination\CountWalker;
1716
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
@@ -21,13 +20,21 @@
2120
*/
2221
class Paginator
2322
{
23+
/**
24+
* Use constants to define configuration options that rarely change instead
25+
* of specifying them under parameters section in config/services.yaml file.
26+
*
27+
* See https://symfony.com/doc/current/best_practices.html#use-constants-to-define-options-that-rarely-change
28+
*/
29+
public const PAGE_SIZE = 10;
30+
2431
private $queryBuilder;
2532
private $currentPage;
2633
private $pageSize;
2734
private $results;
2835
private $numResults;
2936

30-
public function __construct(DoctrineQueryBuilder $queryBuilder, int $pageSize = Post::NUM_ITEMS)
37+
public function __construct(DoctrineQueryBuilder $queryBuilder, int $pageSize = self::PAGE_SIZE)
3138
{
3239
$this->queryBuilder = $queryBuilder;
3340
$this->pageSize = $pageSize;

src/Repository/PostRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function findLatest(int $page = 1, Tag $tag = null): Paginator
5757
/**
5858
* @return Post[]
5959
*/
60-
public function findBySearchQuery(string $query, int $limit = Post::NUM_ITEMS): array
60+
public function findBySearchQuery(string $query, int $limit = Paginator::PAGE_SIZE): array
6161
{
6262
$searchTerms = $this->extractSearchTerms($query);
6363

tests/Controller/BlogControllerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace App\Tests\Controller;
1313

1414
use App\Entity\Post;
15+
use App\Pagination\Paginator;
1516
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
1617

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

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

5051
$this->assertCount(
51-
Post::NUM_ITEMS,
52+
Paginator::PAGE_SIZE,
5253
$crawler->filter('item'),
5354
'The xml file displays the right number of posts.'
5455
);

0 commit comments

Comments
 (0)