CreateListOfTasks::mu()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Api;
4
5
use Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader;
6
7
class CreateListOfTasks
8
{
9
    public function run()
10
    {
11
        $this->myMu = ModuleUpgrader::create();
0 ignored issues
show
Bug Best Practice introduced by
The property myMu does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
12
        $html = '';
13
        foreach (array_keys($this->mu()->getAvailableRecipes()) as $recipeKey) {
0 ignored issues
show
Bug introduced by
It seems like getAvailableRecipes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

13
        foreach (array_keys($this->mu()->/** @scrutinizer ignore-call */ getAvailableRecipes()) as $recipeKey) {
Loading history...
14
            $this->mu()->applyRecipe($recipeKey);
0 ignored issues
show
Bug introduced by
It seems like applyRecipe() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

14
            $this->mu()->/** @scrutinizer ignore-call */ applyRecipe($recipeKey);
Loading history...
15
            $html .= '<h1>List of Tasks in run order for recipe: ' . $recipeKey . '</h1>';
16
            $count = 0;
17
            $totalCount = count($this->mu()->getListOfTasks());
0 ignored issues
show
Bug introduced by
It seems like getListOfTasks() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

17
            $totalCount = count($this->mu()->/** @scrutinizer ignore-call */ getListOfTasks());
Loading history...
18
            $previousStep = '';
19
            foreach ($this->mu()->getListOfTasks() as $class => $params) {
20
                $properClass = current(explode('-', $class));
21
                $nameSpacesArray = explode('\\', $class);
22
                $shortClassCode = end($nameSpacesArray);
23
                if (! class_exists($properClass)) {
24
                    $properClass = $this->mu()->getDefaultNamespaceForTasks() . '\\' . $properClass;
0 ignored issues
show
Bug introduced by
It seems like getDefaultNamespaceForTasks() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
                    $properClass = $this->mu()->/** @scrutinizer ignore-call */ getDefaultNamespaceForTasks() . '\\' . $properClass;
Loading history...
25
                }
26
                if (class_exists($properClass)) {
27
                    $count++;
28
                    // $runItNow = $this->mu()->shouldWeRunIt((string) $shortClassCode);
29
                    $params['taskName'] = $shortClassCode;
30
                    $obj = $properClass::create($this, $params);
31
                    if ($obj->getTaskName()) {
32
                        $params['taskName'] = $obj->getTaskName();
33
                    }
34
                    $reflectionClass = new \ReflectionClass($properClass);
35
                    $path = 'https://github.com/sunnysideup/silverstripe-upgrade_to_silverstripe_4/tree/master/src/';
36
                    $path .= str_replace('\\', '/', $reflectionClass->getName()) . '.php';
37
                    $path = str_replace('Sunnysideup/UpgradeToSilverstripe4/', '', $path);
38
                    $currentStepCode = $obj->getTaskStepCode();
39
                    $currentStep = $obj->getTaskStep($currentStepCode);
40
                    if ($currentStepCode === 's00') {
41
                        //do nothing when it is an anytime step
42
                    } else {
43
                        if ($previousStep !== $currentStep) {
44
                            $html .= '<h2>' . $currentStep . '</h2>';
45
                        }
46
                        $previousStep = $currentStep;
47
                    }
48
                    $html .= '<h4>' . $count . '/' . $totalCount . ': ' . $obj->getTitle() . '</h4>';
49
                    $html .= '<p>' . $obj->getDescription() . '<br />';
50
                    $html .= '<strong>Class Code: </strong>' . $class;
51
                    $html .= '<br /><strong>Class Name: </strong>';
52
                    $html .= '<a href="' . $path . '">' . $reflectionClass->getShortName() . '</a>';
53
                    $html .= '</p>';
54
                    $obj = $properClass::deleteTask($params);
0 ignored issues
show
Unused Code introduced by
The assignment to $obj is dead and can be removed.
Loading history...
55
                } else {
56
                    user_error($properClass . ' could not be found as class', E_USER_ERROR);
57
                }
58
            }
59
        }
60
        $dir = $this->mu()->checkIfPathExistsAndCleanItUp(__DIR__ . '/../../docs/en/');
0 ignored issues
show
Bug introduced by
It seems like checkIfPathExistsAndCleanItUp() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        $dir = $this->mu()->/** @scrutinizer ignore-call */ checkIfPathExistsAndCleanItUp(__DIR__ . '/../../docs/en/');
Loading history...
61
        if ($dir) {
62
            $html = str_replace(' _', ' \_', $html);
63
            file_put_contents(
64
                rtrim($dir, '/') . '/AvailableTasks.md',
65
                $html
66
            );
67
        } else {
68
            user_error('Coult not find ' . $dir . ' directory');
69
        }
70
    }
71
72
    protected function mu()
73
    {
74
        return $this->myMu;
75
    }
76
}
77