Completed
Push — master ( 441321...88de30 )
by Martijn van
02:09
created

ConsoleController::writeSuperSakeFileToWebRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
use Symfony\Component\Console\Application;
4
use Symfony\Component\Console\Input\ArgvInput;
5
6
/**
7
 * Class ConsoleController
8
 * The Central Command Access Point and Bootstrapper
9
 */
10
class ConsoleController extends Controller
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...
11
{
12
    /**
13
     * @var array
14
     */
15
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
        'publish'
17
    );
18
19
    /**
20
     * @var Symfony\Component\Console\Application
21
     */
22
    protected $application;
23
24
    public function index()
0 ignored issues
show
Coding Style introduced by
index uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
    {
26
        parent::init();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (init() instead of index()). Are you sure this is correct? If so, you might want to change this to $this->init().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
27
        if (!Director::is_cli()) {
28
            $this->httpError(404);
29
        }
30
31
        $this->application = new Application();
32
33
        $this->loadCommands();
34
35
        // remove the framework/cli-script.php argument
36
        array_shift($_SERVER['argv']);
37
        return $this->application->run(new ArgvInput($_SERVER['argv']));
38
    }
39
40
    public function publish()
41
    {
42
        if (Director::is_cli()) {
43
            $this->writeSuperSakeFileToWebRoot();
44
            $this->writehtaccess();
0 ignored issues
show
Unused Code introduced by
The call to the method ConsoleController::writehtaccess() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
45
            $this->writewebconfig();
46
        }
47
    }
48
49
    public function loadCommands()
50
    {
51
        //somehow this does not work.
52
        //$commands = ClassInfo::subclassesFor('SilverstripeCommand');
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
        //var_dump($commands);exit();
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
54
55
        // and this is will not load other classes
56
        $commands = ClassInfo::classes_for_folder(BASE_PATH . '/console/');
57
58
        /** @var SilverstripeCommand $command */
59
        foreach ($commands as $command) {
60
            if (is_subclass_of($command, 'SilverstripeCommand')) {
61
                $this->application->add(new $command());
62
            }
63
        }
64
    }
65
66
    protected function writeSuperSakeFileToWebRoot()
67
    {
68
        file_put_contents(
69
            BASE_PATH . '/supersake',
70
            file_get_contents(BASE_PATH . '/console/publish/supersake')
71
        );
72
    }
73
74
    /**
75
     * protect the supersake file with htaccess
76
     */
77
    protected function writehtaccess()
78
    {
79
        $content = "# Deny access to supersake
0 ignored issues
show
Unused Code introduced by
$content 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...
80
<Files supersake>
81
	Order allow,deny
82
	Deny from all
83
</Files>";
84
85
    }
86
87
    /**
88
     * protect the supersake file with web.config
89
     */
90
    public function writewebconfig()
91
    {
92
        //<add fileExtension="supersake" allowed="false"/>
93
    }
94
95
96
}
97