Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Repository/AnalyticsSegmentRepository.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\DashboardBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
7
/**
8
 * AnalyticsSegmentRepository
9
 *
10
 * This class was generated by the Doctrine ORM. Add your own custom
11
 * repository methods below.
12
 */
13
class AnalyticsSegmentRepository extends EntityRepository
14
{
15
    /**
16
     * Get a segment
17
     *
18
     * @param int $id
19
     *
20
     * @return AnalyticsOverview|bool
21
     */
22
    public function deleteSegment($id)
23
    {
24
        $em = $this->getEntityManager();
25
        $qb = $em->createQueryBuilder();
26
        $qb->select('s')
27
            ->from('KunstmaanDashboardBundle:AnalyticsSegment', 's')
28
            ->where('s.id = :id')
29
            ->setParameter('id', $id);
30
31
        $results = $qb->getQuery()->getResult();
32
        if ($results) {
33
            $em->remove($results[0]);
34
            $em->flush();
35
        }
36
    }
37
38
    /**
39
     * Initialise a segment by adding new overviews if they don't exist yet
40
     *
41
     * @param AnalyticsSegment $segment
42
     * @param int              $configId
43
     */
44
    public function initSegment($segment, $configId = false)
45
    {
46
        if (!\count($segment->getOverviews()->toArray())) {
47
            if ($configId) {
48
                $config = $this->getEntityManager()->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->find($configId);
49
            } else {
50
                $config = $this->getEntityManager()->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method findFirst() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
51
            }
52
            $this->getEntityManager()->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')->addOverviews($config, $segment);
53
        }
54
    }
55
}
56