TranslationExtractor   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 11.63 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 82.76%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 6
dl 10
loc 86
ccs 24
cts 29
cp 0.8276
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B extract() 0 18 5
A addMessageCatalogue() 0 12 3
A getLoader() 10 10 2

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
namespace Itkg\TranslationBundle\Extractor;
4
5
use Symfony\Component\DependencyInjection\ContainerAware;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Finder\Finder;
8
use Symfony\Component\Finder\SplFileInfo;
9
use Symfony\Component\Translation\Loader\LoaderInterface;
10
use Symfony\Component\Translation\MessageCatalogue;
11
12
/**
13
 * Class TranslationExtractor
14
 */
15
class TranslationExtractor extends ContainerAware
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Component\Depend...njection\ContainerAware has been deprecated with message: since version 2.8, to be removed in 3.0. Use the ContainerAwareTrait instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
{
17
    /**
18
     * @var Finder
19
     */
20
    private $finder;
21
22
    /**
23
     * @var Filesystem
24
     */
25
    private $filesystem;
26
27
    /**
28
     * @var MessageCatalogue[]
29
     */
30
    private $messageCatalogues = [];
31
32
    /**
33
     * @param Finder     $finder
34
     * @param Filesystem $filesystem
35
     */
36 3
    public function __construct(Finder $finder, Filesystem $filesystem)
37
    {
38 3
        $this->finder = $finder;
39 3
        $this->filesystem = $filesystem;
40 3
    }
41
42
    /**
43
     * @param string      $path
44
     * @param string      $format
45
     * @param null|string $domain
46
     *
47
     * @return MessageCatalogue[]
48
     */
49 3
    public function extract($path, $format, $domain = null)
50
    {
51 3
        $this->messageCatalogues = [];
52 3
        if (!$this->filesystem->exists($path)) {
53
            throw new \RuntimeException(sprintf('Path %s does not exist', $path));
54
        }
55 3
        $files = $this->finder->files()->name('/[a-z]+\.[a-z]{2}\.'.$format.'/')->in($path);
56
57
        /** @var SplFileInfo $file */
58 3
        foreach ($files as $file) {
59 3
            list($translationDomain, $language) = explode('.', $file->getFilename());
60 3
            if ($domain && $domain !== $translationDomain) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $domain of type null|string is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
61 1
                continue;
62
            }
63 3
            $this->addMessageCatalogue($this->getLoader($format)->load($file->getRealPath(), $language, $translationDomain), $language);
64 3
        }
65 3
        return $this->messageCatalogues;
66
    }
67
68
    /**
69
     * @param MessageCatalogue $messageCatalogue
70
     * @param string           $language
71
     */
72 3
    private function addMessageCatalogue(MessageCatalogue $messageCatalogue, $language)
73
    {
74 3
        if (!$messageCatalogue->all()) {
75
            return;
76
        }
77
78 3
        if (isset($this->messageCatalogues[$language])) {
79
            $this->messageCatalogues[$language]->addCatalogue($messageCatalogue);
80
        } else {
81 3
            $this->messageCatalogues[$language] = $messageCatalogue;
82
        }
83 3
    }
84
85
    /**
86
     * @param string $format
87
     *
88
     * @return LoaderInterface
89
     */
90 3 View Code Duplication
    private function getLoader($format)
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...
91
    {
92 3
        $service = sprintf('translation.loader.%s', $format);
93
94 3
        if (!$this->container->has($service)) {
95
            throw new \InvalidArgumentException(sprintf('Unable to find Symfony Translation loader for format "%s"', $format));
96
        }
97
98 3
        return $this->container->get($service);
99
    }
100
}
101