TextCollectorCommand::fire()   B
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 43
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 43
rs 8.439
cc 5
eloc 25
nc 16
nop 0
1
<?php
2
3
4
use Symfony\Component\Console\Input\InputOption;
5
6
class TextCollectorCommand extends SilverstripeCommand
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $name = 'task:collecttext';
12
13
    /**
14
     * @var string
15
     */
16
    protected $description = 'Traverses through files in order to collect the \'entity master tables\' stored in each module.';
17
18
    public function fire()
19
    {
20
        increase_memory_limit_to();
21
        increase_time_limit_to();
22
23
        /** @var i18nTextCollector $collector */
24
        $collector = i18nTextCollector::create($this->option('locale'));
25
26
        if (!$this->option('locale')) {
27
            $this->info('Starting text collection. No Locale specified');
28
        } else {
29
            $this->info('Starting text collection for: '.$this->option('locale'));
30
        }
31
32
        $merge = $this->getIsMerge();
33
34
        if ($merge) {
35
            $this->info('New strings will be merged with existing strings');
36
        }
37
38
        // Custom writer
39
        $writerName = $this->option('writer');
40
        if ($writerName) {
41
            $writer = Injector::inst()->get($writerName);
0 ignored issues
show
Bug introduced by
It seems like $writerName defined by $this->option('writer') on line 39 can also be of type array; however, Injector::get() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
            $collector->setWriter($writer);
43
            $this->info('Set collector writer to: '.$writer);
44
        }
45
46
        // Get restrictions
47
        $restrictModules = ($this->option('module'))
48
            ? explode(',', $this->option('module'))
49
            : null;
50
51
        $this->info('Collecting in modules: '.$this->option('module'));
52
53
        // hack untill we have something better
54
        ob_start();
55
        $collector->run($restrictModules, $merge);
56
        $this->warn(ob_get_contents());
57
        ob_get_clean();
58
59
        $this->info(__CLASS__.' completed!');
60
    }
61
62
    protected function getIsMerge()
63
    {
64
        $merge = $this->option('merge');
65
66
        // Default to false if not given
67
        if (!isset($merge)) {
68
            $this->error('merge will be enabled by default in 4.0. Please use merge=false if you do not want to merge.');
69
70
            return false;
71
        }
72
73
        // merge=0 or merge=false will disable merge
74
        return !in_array($merge, ['0', 'false']);
75
    }
76
77
    protected function getOptions()
78
    {
79
        return [
80
            ['module', 'm', InputOption::VALUE_REQUIRED, 'Select the modules to collect'],
81
            ['locale', 'l', InputOption::VALUE_REQUIRED, 'Please specify the target locale'],
82
            ['merge', 'mr', InputOption::VALUE_NONE, 'Merge with existing'],
83
            ['writer', 'w', InputOption::VALUE_OPTIONAL, 'Select a custom writer'],
84
        ];
85
    }
86
}
87