These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Kunstmaan\ArticleBundle\Controller; |
||
4 | |||
5 | use Pagerfanta\Adapter\ArrayAdapter; |
||
6 | use Pagerfanta\Pagerfanta; |
||
7 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
||
8 | use Symfony\Component\HttpFoundation\Request; |
||
9 | |||
10 | /** |
||
11 | * Class AbstractArticleOverviewPageController |
||
12 | */ |
||
13 | class AbstractArticleOverviewPageController extends Controller |
||
14 | { |
||
15 | /** |
||
16 | * @param Request $request |
||
17 | */ |
||
18 | public function serviceAction(Request $request) |
||
19 | { |
||
20 | $em = $this->getDoctrine()->getManager(); |
||
21 | $entity = $request->attributes->get('_entity'); |
||
22 | $repository = $entity->getArticleRepository($em); |
||
23 | $pages = $repository->getArticles($request->getLocale()); |
||
24 | |||
25 | $adapter = new ArrayAdapter($pages); |
||
26 | $pagerfanta = new Pagerfanta($adapter); |
||
27 | $pagerfanta->setMaxPerPage(5); |
||
28 | |||
29 | $pagenumber = $request->get('page'); |
||
30 | if (!$pagenumber || $pagenumber < 1) { |
||
31 | $pagenumber = 1; |
||
32 | } |
||
33 | $pagerfanta->setCurrentPage($pagenumber); |
||
34 | $context['pagerfanta'] = $pagerfanta; |
||
0 ignored issues
–
show
|
|||
35 | |||
36 | $request->attributes->set('_renderContext', $context); |
||
37 | } |
||
38 | } |
||
39 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.