Storage   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 94
Duplicated Lines 29.79 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 28
loc 94
ccs 0
cts 70
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 9 1
B getList() 0 26 3
B getLastUpdate() 28 28 3
A getLast() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Repository;
11
12
use Doctrine\ORM\EntityRepository;
13
use AnimeDb\Bundle\CatalogBundle\Entity\Storage as StorageEntity;
14
15
/**
16
 * @author  Peter Gribanov <[email protected]>
17
 */
18
class Storage extends EntityRepository
19
{
20
    /**
21
     * @return int
22
     */
23
    public function count()
24
    {
25
        return $this->getEntityManager()->createQuery('
26
            SELECT
27
                COUNT(s)
28
            FROM
29
                AnimeDbCatalogBundle:Storage s
30
        ')->getSingleScalarResult();
31
    }
32
33
    /**
34
     * @param array $types
35
     *
36
     * @return StorageEntity[]
37
     */
38
    public function getList(array $types = [])
39
    {
40
        if (!$types || $types == StorageEntity::getTypes()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $types of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
41
            return $this->getEntityManager()->createQuery('
42
                SELECT
43
                    s
44
                FROM
45
                    AnimeDbCatalogBundle:Storage s
46
                ORDER BY
47
                    s.id DESC
48
            ')->getResult();
49
        }
50
51
        return $this->getEntityManager()->createQuery('
52
            SELECT
53
                s
54
            FROM
55
                AnimeDbCatalogBundle:Storage s
56
            WHERE
57
                s.type IN (:types)
58
            ORDER BY
59
                s.id DESC
60
        ')
61
            ->setParameter(':types', $types)
62
            ->getResult();
63
    }
64
65
    /**
66
     * @param int|null $id
67
     *
68
     * @return \DateTime|null
69
     */
70 View Code Duplication
    public function getLastUpdate($id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
73
            $result = $this->getEntityManager()->createQuery('
74
                SELECT
75
                    s.date_update
76
                FROM
77
                    AnimeDbCatalogBundle:Storage s
78
                WHERE
79
                    s.id = :id'
80
            )
81
                ->setParameter(':id', $id)
82
                ->getOneOrNullResult();
83
        } else {
84
            $result = $this->getEntityManager()->createQuery('
85
                SELECT
86
                    s.date_update
87
                FROM
88
                    AnimeDbCatalogBundle:Storage s
89
                ORDER BY
90
                    s.date_update DESC'
91
            )
92
                ->setMaxResults(1)
93
                ->getOneOrNullResult();
94
        }
95
96
        return $result ? $result['date_update'] : null;
97
    }
98
99
    /**
100
     * @return StorageEntity|null
101
     */
102
    public function getLast()
103
    {
104
        return $this
105
            ->createQueryBuilder('s')
106
            ->addOrderBy('s.id', 'DESC')
107
            ->setMaxResults(1)
108
            ->getQuery()
109
            ->getOneOrNullResult();
110
    }
111
}
112