Completed
Push — master ( 1758db...0d3940 )
by Jordi Sala
02:09
created

src/Entity/GalleryManager.php (2 issues)

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Entity;
15
16
use Sonata\DatagridBundle\Pager\Doctrine\Pager;
17
use Sonata\DatagridBundle\ProxyQuery\Doctrine\ProxyQuery;
18
use Sonata\Doctrine\Entity\BaseEntityManager;
19
use Sonata\MediaBundle\Model\GalleryInterface;
20
use Sonata\MediaBundle\Model\GalleryManagerInterface;
21
22
/**
23
 * @final since sonata-project/media-bundle 3.21.0
24
 */
25
class GalleryManager extends BaseEntityManager implements GalleryManagerInterface
26
{
27
    /**
28
     * BC Compatibility.
29
     *
30
     * NEXT_MAJOR: remove this method.
31
     *
32
     * @deprecated Please use save() from now
33
     */
34
    public function update(GalleryInterface $gallery): void
35
    {
36
        parent::save($gallery);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (save() instead of update()). Are you sure this is correct? If so, you might want to change this to $this->save().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
$gallery is of type object<Sonata\MediaBundle\Model\GalleryInterface>, but the function expects a object<Sonata\Doctrine\Model\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getPager(array $criteria, $page, $limit = 10, array $sort = [])
43
    {
44
        $query = $this->getRepository()
45
            ->createQueryBuilder('g')
46
            ->select('g');
47
48
        $fields = $this->getEntityManager()->getClassMetadata($this->class)->getFieldNames();
49
        foreach ($sort as $field => $direction) {
50
            if (!\in_array($field, $fields, true)) {
51
                throw new \RuntimeException(sprintf("Invalid sort field '%s' in '%s' class", $field, $this->class));
52
            }
53
        }
54
        if (0 === \count($sort)) {
55
            $sort = ['name' => 'ASC'];
56
        }
57
        foreach ($sort as $field => $direction) {
58
            $query->orderBy(sprintf('g.%s', $field), strtoupper($direction));
59
        }
60
61
        $parameters = [];
62
63
        if (isset($criteria['enabled'])) {
64
            $query->andWhere('g.enabled = :enabled');
65
            $parameters['enabled'] = $criteria['enabled'];
66
        }
67
68
        $query->setParameters($parameters);
69
70
        $pager = new Pager();
71
        $pager->setMaxPerPage($limit);
72
        $pager->setQuery(new ProxyQuery($query));
73
        $pager->setPage($page);
74
        $pager->init();
75
76
        return $pager;
77
    }
78
}
79