ModuleChecks::exitsOnPackagist()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 1
c 2
b 2
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * check if everything is in plcae for a module
5
 * some quick and dirty methods ....
6
 *
7
 *
8
 */
9
10
class ModuleChecks extends BuildTask
11
{
12
    private static $packagist_user_name = '';
0 ignored issues
show
introduced by
The private property $packagist_user_name is not used, and could be removed.
Loading history...
13
14
    /**
15
     * list of methods to run for each module
16
     * @var array
17
     */
18
    private static $methods_to_check = array(
0 ignored issues
show
introduced by
The private property $methods_to_check is not used, and could be removed.
Loading history...
19
        "exitsOnPackagist",
20
        "hasReadMeFile",
21
        "hasLicense",
22
        "hasComposerFile",
23
        "existsOnAddOns",
24
    );
25
26
    protected $title = "Check Modules on Github and Packagist";
27
28
    protected $description = "Goes through every module on github and checks for some of the basic requirements. You will need to set your GitHub Username in the configs.";
29
30
    public function run($request)
31
    {
32
        increase_time_limit_to(3600);
33
34
        $modules = GitRepoFinder::get_all_repos();
35
36
        $gitUser = Config::inst()->get('GitHubModule', "github_user_name");
37
        $packagistUser = $this->Config()->get("packagist_user_name");
38
39
        if ($gitUser && $packagistUser) {
40
            //all is good ...
41
        } else {
42
            user_error("make sure to set your git user name ($gitUser) and packagist username ($packagistUser) via the standard config system");
43
        }
44
45
        $count = 0;
46
        echo "<h1>Testing ".count($modules)." modules (git user: $gitUser and packagist user: $packagistUser) ...</h1>";
0 ignored issues
show
Bug introduced by
$modules of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

46
        echo "<h1>Testing ".count(/** @scrutinizer ignore-type */ $modules)." modules (git user: $gitUser and packagist user: $packagistUser) ...</h1>";
Loading history...
47
        $methodsToCheck = $this->Config()->get("methods_to_check");
48
        foreach ($modules as $module) {
0 ignored issues
show
Bug introduced by
The expression $modules of type string is not traversable.
Loading history...
49
            $count++;
50
            $failures = 0;
51
            echo "<h3><a href=\"https://github.com/".$gitUser."/silverstripe-".$module."\"></a>$count. checking $module</h3>";
52
            foreach ($methodsToCheck as $method) {
53
                if (!$this->$method($module)) {
54
                    $failures++;
55
                    DB::alteration_message("bad response for $method", "deleted");
56
                }
57
            }
58
            if ($failures == 0) {
59
                DB::alteration_message("OK", "created");
60
            }
61
            echo '
62
            ';
63
            @ob_flush();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for ob_flush(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

63
            /** @scrutinizer ignore-unhandled */ @ob_flush();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
Are you sure the usage of ob_flush() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
64
            @flush();
0 ignored issues
show
Bug introduced by
Are you sure the usage of flush() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition for flush(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

64
            /** @scrutinizer ignore-unhandled */ @flush();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
65
        }
66
        echo "----------------------------- THE END --------------------------";
67
    }
68
69
    /**
70
     * @param string $name
71
     *
72
     * @return boolean
73
     */
74
    protected function exitsOnPackagist($name)
75
    {
76
        return GeneralMethods::check_location("https://packagist.org/packages/".$this->Config()->get("packagist_user_name")."/$name");
77
    }
78
79
80
    /**
81
     * @param string $name
82
     *
83
     * @return boolean
84
     */
85
    protected function hasLicense($name)
86
    {
87
        return GeneralMethods::check_location("https://raw.githubusercontent.com/".Config::inst()->get('GitHubModule', 'github_user_name')."/silverstripe-".$name."/master/LICENSE");
88
    }
89
90
    /**
91
     * @param string $name
92
     *
93
     * @return boolean
94
     */
95
    protected function hasComposerFile($name)
96
    {
97
        return GeneralMethods::check_location("https://raw.githubusercontent.com/".Config::inst()->get('GitHubModule', 'github_user_name')."/silverstripe-".$name."/master/composer.json");
98
    }
99
100
    /**
101
     * @param string $name
102
     *
103
     * @return boolean
104
     */
105
    protected function hasReadMeFile($name)
106
    {
107
        return GeneralMethods::check_location("https://raw.githubusercontent.com/".Config::inst()->get('GitHubModule', 'github_user_name')."/silverstripe-".$name."/master/README.md");
108
    }
109
110
    protected function existsOnAddOns($name)
111
    {
112
        return GeneralMethods::check_location("http://addons.silverstripe.org/add-ons/".$this->Config()->get("packagist_user_name")."/$name");
113
    }
114
115
116
    /**
117
     * checks if a particular variable is present in the composer.json file
118
     *
119
     * @param string $name
120
     * @param string $variable
121
     * @return boolean
122
     */
123
    protected function checkForDetailsInComposerFile($name, $variable)
124
    {
125
        die("to be completed");
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
126
    }
127
}
128