AllCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 79
Duplicated Lines 11.39 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 9
loc 79
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 9 44 4

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\AutoTranslate;
9
10
class AllCommand extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'autotrans:all';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Translates all source translations to target translations';
25
26
    protected $autoTranslator;
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @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...
32
     */
33
    public function __construct(AutoTranslate $autoTranslator)
34
    {
35
        parent::__construct();
36
        $this->autoTranslator = $autoTranslator;
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return mixed
43
     */
44
    public function handle()
45
    {
46
        $targetLanguages = Arr::wrap(config('auto-translate.target_language'));
47
48
        $foundLanguages = count($targetLanguages);
49
        $this->line('Found '.$foundLanguages.' '.Str::plural('language', $foundLanguages).' to translate');
50
51
        $availableTranslations = 0;
0 ignored issues
show
Unused Code introduced by
$availableTranslations is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
52
        $sourceTranslations = $this->autoTranslator->getSourceTranslations();
53
        $availableTranslations = count($dottedTranslations = Arr::dot($sourceTranslations)) * count($targetLanguages);
54
55
        if (empty($dottedTranslations)) {
56
            $this->line('0 keys found...aborting');
57
58
            return;
59
        }
60
61
        $strLen = collect($dottedTranslations)->map(function ($value) {
62
            return strlen($value);
63
        })->sum() * count($targetLanguages);
64
65
        $this->line($strLen.' characters will be translated');
66
67
        if (! $this->confirm('Continue?', true)) {
68
            return;
69
        }
70
71
        $bar = $this->output->createProgressBar($availableTranslations);
72
        $bar->start();
73
74 View Code Duplication
        foreach ($targetLanguages as $targetLanguage) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
            $dottedSource = Arr::dot($sourceTranslations);
76
77
            $translated = $this->autoTranslator->translate($targetLanguage, $dottedSource, function () use ($bar) {
78
                $bar->advance();
79
            });
80
81
            $this->autoTranslator->fillLanguageFiles($targetLanguage, $translated);
82
        }
83
84
        $bar->finish();
85
86
        $this->info("\nTranslated ".$availableTranslations.' language keys.');
87
    }
88
}
89