Completed
Pull Request — master (#29)
by
unknown
12:01
created

MissingCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 110
Duplicated Lines 70 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 77
loc 110
ccs 0
cts 27
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A handle() 14 14 1
A translatePhpFiles() 28 28 2
A translateJsonFiles() 28 28 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 Ben182\AutoTranslate\Commands;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use Illuminate\Console\Command;
8
use Ben182\AutoTranslate\AutoTranslators\JsonTranslator;
9
use Ben182\AutoTranslate\AutoTranslators\PhpTranslator;
10
11
class MissingCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'autotrans:missing';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Translates all source translations that are not set in your target translations';
26
27
    protected $phpTranslator;
28
    protected $jsonTranslator;
29
30
    /**
31
     * Create a new command instance.
32
     *
33
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
34
     */
35 View Code Duplication
    public function __construct(PhpTranslator $phpTranslator, JsonTranslator $jsonTranslator)
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...
36
    {
37
        parent::__construct();
38
        $this->phpTranslator = $phpTranslator;
39
        $this->jsonTranslator = $jsonTranslator;
40
        $this->targetLanguages = Arr::wrap(config('auto-translate.target_language'));
0 ignored issues
show
Bug introduced by
The property targetLanguages does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
    }
42
43
    /**
44
     * Execute the console command.
45
     *
46
     * @return mixed
47
     */
48 View Code Duplication
    public function handle()
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...
49
    {
50
        $numTargetLanguages = count($this->targetLanguages);
51
52
        $this->line(implode(' ', [
53
            'Found',
54
            $numTargetLanguages,
55
            Str::plural('language', $numTargetLanguages),
56
            'to translate',
57
        ]));
58
59
        $this->translatePhpFiles();
60
        $this->translateJsonFiles();
61
    }
62
63 View Code Duplication
    public function translatePhpFiles()
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...
64
    {
65
        $this->line('Translating PHP files');
66
67
        $missingCount = 0;
68
        foreach ($this->targetLanguages as $targetLanguage) {
69
            $missingTranslations = $this->phpTranslator->getMissingTranslations($targetLanguage);
70
            $missingCount += count($missingTranslations);
71
72
            $this->line(implode(' ', [
73
                'Found',
74
                count($missingTranslations),
75
                'missing keys in',
76
                $targetLanguage,
77
            ]));
78
        }
79
80
        $bar = $this->output->createProgressBar($missingCount);
81
        $bar->start();
82
83
        $this->phpTranslator->translateMissing($this->targetLanguages, function () use ($bar) {
84
            $bar->advance();
85
        });
86
87
        $bar->finish();
88
89
        $this->info("\nTranslated ".$missingCount.' missing language keys.');
90
    }
91
92 View Code Duplication
    public function translateJsonFiles()
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...
93
    {
94
        $this->line('Translating JSON files');
95
96
        $missingCount = 0;
97
        foreach ($this->targetLanguages as $targetLanguage) {
98
            $missingTranslations = $this->jsonTranslator->getMissingTranslations($targetLanguage);
99
            $missingCount += count($missingTranslations);
100
101
            $this->line(implode(' ', [
102
                'Found',
103
                count($missingTranslations),
104
                'missing keys in',
105
                $targetLanguage,
106
            ]));
107
        }
108
109
        $bar = $this->output->createProgressBar($missingCount);
110
        $bar->start();
111
112
        $this->jsonTranslator->translateMissing($this->targetLanguages, function () use ($bar) {
113
            $bar->advance();
114
        });
115
116
        $bar->finish();
117
118
        $this->info("\nTranslated ".$missingCount.' missing language keys.');
119
    }
120
}
121