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

AllCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 97
Duplicated Lines 21.65 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 21
loc 97
ccs 0
cts 24
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() 0 18 1
A translateJsonFiles() 0 24 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 AllCommand extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'autotrans:all';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Translates all source translations to target translations';
26
27
    protected $phpTranslator;
28
    protected $jsonTranslator;
29
30
    protected $targetLanguages;
31
32
    /**
33
     * Create a new command instance.
34
     *
35
     * @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...
36
     */
37 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...
38
    {
39
        parent::__construct();
40
        $this->phpTranslator = $phpTranslator;
41
        $this->jsonTranslator = $jsonTranslator;
42
        $this->targetLanguages = Arr::wrap(config('auto-translate.target_language'));
43
    }
44
45
    /**
46
     * Execute the console command.
47
     *
48
     * @return mixed
49
     */
50 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...
51
    {
52
        $numTargetLanguages = count($this->targetLanguages);
53
54
        $this->line(implode(' ', [
55
            'Found',
56
            $numTargetLanguages,
57
            Str::plural('language', $numTargetLanguages),
58
            'to translate',
59
        ]));
60
61
        $this->translatePhpFiles();
62
        $this->translateJsonFiles();
63
    }
64
    private function translatePhpFiles()
65
    {
66
        $this->line('Translating PHP files');
67
68
        $sourceTranslations = $this->phpTranslator->getSourceTranslations();
69
        $availableTranslations = count($sourceTranslations) * count($this->targetLanguages);
70
71
        $bar = $this->output->createProgressBar($availableTranslations);
72
        $bar->start();
73
74
        $this->phpTranslator->translateAll($this->targetLanguages, function () use ($bar) {
75
            $bar->advance();
76
        });
77
78
        $bar->finish();
79
80
        $this->info("\nTranslated $availableTranslations language keys.");
81
    }
82
83
    private function translateJsonFiles()
84
    {
85
        $sourceTranslations = $this->jsonTranslator->getSourceTranslations();
86
87
        if (count($sourceTranslations) === 0) {
88
            $this->line('No JSON files to translate!');
89
            return;
90
        }
91
92
        $this->line('Translating JSON files');
93
94
        $availableTranslations = count($sourceTranslations) * count($this->targetLanguages);
95
96
        $bar = $this->output->createProgressBar($availableTranslations);
97
        $bar->start();
98
99
        $this->jsonTranslator->translateAll($this->targetLanguages, function () use ($bar) {
100
            $bar->advance();
101
        });
102
103
        $bar->finish();
104
105
        $this->info("\nTranslated $availableTranslations language keys.");
106
    }
107
}
108