ApplyPSR2::hasCommitAndPush()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sunnysideup\UpgradeToSilverstripe4\Tasks\IndividualTasks;
4
5
use Sunnysideup\PHP2CommandLine\PHP2CommandLineSingleton;
6
use Sunnysideup\UpgradeToSilverstripe4\Api\FileSystemFixes;
7
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Helpers\Composer;
8
use Sunnysideup\UpgradeToSilverstripe4\Tasks\Task;
9
10
/**
11
 * Adds a new branch to your repository that is going to be used for upgrading it.
12
 */
13
class ApplyPSR2 extends Task
14
{
15
    protected $taskStep = 's60';
16
17
    protected $composerOptions = '';
18
19
    protected $lintingIssuesFileName = 'LINTING_ERRORS';
20
21
    public function getTitle()
22
    {
23
        return 'Apply PSR2 Cleanup.';
24
    }
25
26
    public function getDescription()
27
    {
28
        return '
29
            Applies a light cleanup of the code to match PSR-2 standards.';
30
    }
31
32
    public function runActualTask($params = []): ?string
33
    {
34
        $webRoot = $this->mu()->getWebRootDirLocation();
0 ignored issues
show
Bug introduced by
The method getWebRootDirLocation() does not exist on Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

34
        $webRoot = $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation();
Loading history...
Bug introduced by
It seems like getWebRootDirLocation() 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

34
        $webRoot = $this->mu()->/** @scrutinizer ignore-call */ getWebRootDirLocation();
Loading history...
35
        $localInstall = (bool) PHP2CommandLineSingleton::commandExists('sake-ling-all');
36
        $commandAdd = '';
37
        if ($localInstall) {
38
            $commandAdd = 'vendor/bin/';
39
            Composer::inst($this->mu())
40
                ->RequireDev(
41
                    'sunnysideup/easy-coding-standards',
42
                    'dev-master',
43
                    $this->composerOptions
44
                );
45
        }
46
47
        //1. apply
48
        foreach ($this->mu()->findNameSpaceAndCodeDirs() as $baseNameSpace => $codeDir) {
0 ignored issues
show
Bug introduced by
It seems like findNameSpaceAndCodeDirs() 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

48
        foreach ($this->mu()->/** @scrutinizer ignore-call */ findNameSpaceAndCodeDirs() as $baseNameSpace => $codeDir) {
Loading history...
49
            $knownIssuesFileName = $codeDir . '/' . $this->lintingIssuesFileName;
50
            $relativeDir = str_replace($webRoot, '', $codeDir);
0 ignored issues
show
Bug introduced by
It seems like $webRoot can also be of type Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader and Sunnysideup\UpgradeToSilverstripe4\Traits\Creator; however, parameter $search of str_replace() does only seem to accept string|string[], maybe add an additional type check? ( Ignorable by Annotation )

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

50
            $relativeDir = str_replace(/** @scrutinizer ignore-type */ $webRoot, '', $codeDir);
Loading history...
51
            $relativeDir = ltrim($relativeDir, '/');
52
            FileSystemFixes::inst($this->mu())
53
                ->removeDirOrFile($knownIssuesFileName);
54
            $this->mu()->execMe(
0 ignored issues
show
Bug introduced by
It seems like execMe() 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

54
            $this->mu()->/** @scrutinizer ignore-call */ execMe(
Loading history...
55
                $webRoot,
0 ignored issues
show
Bug introduced by
It seems like $webRoot can also be of type Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader and Sunnysideup\UpgradeToSilverstripe4\Traits\Creator and null; however, parameter $newDir of Sunnysideup\UpgradeToSil...oduleUpgrader::execMe() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

55
                /** @scrutinizer ignore-type */ $webRoot,
Loading history...
56
                $commandAdd . 'sake-lint-all ' . $relativeDir,
57
                'Apply easy coding standards to ' . $relativeDir . ' (' . $baseNameSpace . ')',
58
                false
59
            );
60
            $this->mu()->execMe(
61
                $webRoot,
62
                $commandAdd . 'sake-lint-all ' . $relativeDir,
63
                'Apply easy coding standards a second time ' . $relativeDir . ' (' . $baseNameSpace . ')',
64
                false
65
            );
66
            $this->mu()->execMe(
67
                $webRoot,
68
                $commandAdd . 'sake-lint-all ' . $relativeDir . ' > ' . $knownIssuesFileName,
69
                'Apply easy coding standards a third time ' . $relativeDir . ' (' . $baseNameSpace . ') and saving to ' . $knownIssuesFileName,
70
                false
71
            );
72
            $this->mu()->execMe(
73
                $webRoot,
74
                $commandAdd . 'sslint-stan ' . $relativeDir . ' >> ' . $knownIssuesFileName,
75
                'Apply phpstan. to ' . $relativeDir . ' (' . $baseNameSpace . ') and saving to: ' . $knownIssuesFileName,
76
                false
77
            );
78
        }
79
        if ($localInstall) {
80
            $commandAdd = 'vendor/bin/';
0 ignored issues
show
Unused Code introduced by
The assignment to $commandAdd is dead and can be removed.
Loading history...
81
            Composer::inst($this->mu())
82
                ->RemoveDev(
83
                    'sunnysideup/easy-coding-standards',
84
                    'dev-master',
85
                    $this->composerOptions
86
                );
87
        }
88
        return null;
89
    }
90
91
    protected function hasCommitAndPush()
92
    {
93
        return true;
94
    }
95
}
96