Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

AbstractArticleOverviewPageController.php (1 issue)

Upgrade to new PHP Analysis Engine

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
Coding Style Comprehensibility introduced by
$context was never initialized. Although not strictly required by PHP, it is generally a good practice to add $context = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

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 the bar 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.

Loading history...
35
36
        $request->attributes->set('_renderContext', $context);
37
    }
38
}
39