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

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

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...
54
        }
55
    }
56
}
57