Completed
Push — master ( 04c37a...3d7f4d )
by Tobias
10:27 queued 08:42
created

CatalogueCounter::getCatalogueStatistics()   C

Complexity

Conditions 10
Paths 84

Size

Total Lines 51
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 51
ccs 0
cts 40
cp 0
rs 6
c 0
b 0
f 0
cc 10
eloc 29
nc 84
nop 1
crap 110

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Catalogue;
13
14
use Symfony\Component\Translation\MessageCatalogueInterface;
15
use Symfony\Component\Translation\MetadataAwareInterface;
16
use Translation\Bundle\Model\Metadata;
17
18
/**
19
 * Calculate the number of messages in a catalogue.
20
 *
21
 * @author Tobias Nyholm <[email protected]>
22
 */
23
class CatalogueCounter
24
{
25
    /**
26
     * @param MessageCatalogueInterface $catalogue
27
     *
28
     * @return int
29
     */
30
    public function getNumberOfDefinedMessages(MessageCatalogueInterface $catalogue)
31
    {
32
        $total = 0;
33
        foreach ($catalogue->getDomains() as $domain) {
34
            $total += count($catalogue->all($domain));
35
        }
36
37
        return $total;
38
    }
39
40
    /**
41
     * @param MessageCatalogueInterface $catalogue
42
     *
43
     * @return array
44
     */
45
    public function getCatalogueStatistics(MessageCatalogueInterface $catalogue)
46
    {
47
        $result = [];
48
        $domains = $catalogue->getDomains();
49
        foreach ($domains as $domain) {
50
            $result[$domain]['defined'] = count($catalogue->all($domain));
51
        }
52
53
        // Sum the number of defined messages.
54
        $result['_total']['defined'] = 0;
55
        foreach ($domains as $domain) {
56
            $result['_total']['defined'] += $result[$domain]['defined'];
57
        }
58
59
        if (!$catalogue instanceof MetadataAwareInterface) {
60
            return $result;
61
        }
62
63
        // For each domain check if the message is new or undefined.
64
        foreach ($domains as $domain) {
65
            $result[$domain]['new'] = 0;
66
            $result[$domain]['obsolete'] = 0;
67
68
            foreach ($catalogue->all($domain) as $key => $text) {
69
                $metadata = new Metadata($catalogue->getMetadata($key, $domain));
70
                $state = $metadata->getState();
71
                if ($state === 'new') {
72
                    ++$result[$domain]['new'];
73
                }
74
75
                if ($state === 'obsolete') {
76
                    ++$result[$domain]['obsolete'];
77
                }
78
79
                if ($metadata->isApproved()) {
80
                    ++$result[$domain]['approved'];
81
                }
82
            }
83
        }
84
85
        // Sum the number of new and obsolete messages.
86
        $result['_total']['new'] = 0;
87
        $result['_total']['obsolete'] = 0;
88
        foreach ($domains as $domain) {
89
            $result['_total']['new'] += $result[$domain]['new'];
90
            $result['_total']['obsolete'] += $result[$domain]['obsolete'];
91
            $result['_total']['approved'] += $result[$domain]['approved'];
92
        }
93
94
        return $result;
95
    }
96
}
97