CatalogueCounter::getCatalogueStatistics()   B
last analyzed

Complexity

Conditions 10
Paths 84

Size

Total Lines 52
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 30
c 1
b 0
f 0
nc 84
nop 1
dl 0
loc 52
ccs 0
cts 41
cp 0
crap 110
rs 7.6666

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
    public function getNumberOfDefinedMessages(MessageCatalogueInterface $catalogue): int
26
    {
27
        $total = 0;
28
        foreach ($catalogue->getDomains() as $domain) {
29
            $total += \count($catalogue->all($domain));
30
        }
31
32
        return $total;
33
    }
34
35
    public function getCatalogueStatistics(MessageCatalogueInterface $catalogue): array
36
    {
37
        $result = [];
38
        $domains = $catalogue->getDomains();
39
        foreach ($domains as $domain) {
40
            $result[$domain]['defined'] = \count($catalogue->all($domain));
41
        }
42
43
        // Sum the number of defined messages.
44
        $result['_total']['defined'] = 0;
45
        foreach ($domains as $domain) {
46
            $result['_total']['defined'] += $result[$domain]['defined'];
47
        }
48
49
        if (!$catalogue instanceof MetadataAwareInterface) {
50
            return $result;
51
        }
52
53
        // For each domain check if the message is new or undefined.
54
        foreach ($domains as $domain) {
55
            $result[$domain]['new'] = 0;
56
            $result[$domain]['obsolete'] = 0;
57
            $result[$domain]['approved'] = 0;
58
59
            foreach ($catalogue->all($domain) as $key => $text) {
60
                $metadata = new Metadata($catalogue->getMetadata($key, $domain));
61
                $state = $metadata->getState();
62
                if ('new' === $state) {
63
                    ++$result[$domain]['new'];
64
                }
65
66
                if ('obsolete' === $state) {
67
                    ++$result[$domain]['obsolete'];
68
                }
69
70
                if ($metadata->isApproved()) {
71
                    ++$result[$domain]['approved'];
72
                }
73
            }
74
        }
75
76
        // Sum the number of new and obsolete messages.
77
        $result['_total']['new'] = 0;
78
        $result['_total']['obsolete'] = 0;
79
        $result['_total']['approved'] = 0;
80
        foreach ($domains as $domain) {
81
            $result['_total']['new'] += $result[$domain]['new'];
82
            $result['_total']['obsolete'] += $result[$domain]['obsolete'];
83
            $result['_total']['approved'] += $result[$domain]['approved'];
84
        }
85
86
        return $result;
87
    }
88
}
89