Completed
Push — master ( 36fcd7...29b2a2 )
by Martijn van
02:10
created

ConsoleController::writeSuperSakeFileToWebRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
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 = [
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 SilverstripeApplication();
32
33
        // remove the framework/cli-script.php argument
34
        array_shift($_SERVER['argv']);
35
36
        $this->application->run(new ArgvInput($_SERVER['argv']));
37
    }
38
39
    public function publish()
40
    {
41
        if (Director::is_cli()) {
42
            $this->writeSuperSakeFileToWebRoot();
43
            $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...
44
            $this->writewebconfig();
45
        }
46
    }
47
48
    protected function writeSuperSakeFileToWebRoot()
49
    {
50
        file_put_contents(
51
            BASE_PATH.'/supersake',
52
            file_get_contents(BASE_PATH.'/console/publish/supersake')
53
        );
54
    }
55
56
    /**
57
     * protect the supersake file with htaccess.
58
     */
59
    protected function writehtaccess()
60
    {
61
        $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...
62
<Files supersake>
63
	Order allow,deny
64
	Deny from all
65
</Files>';
66
    }
67
68
    /**
69
     * protect the supersake file with web.config.
70
     */
71
    public function writewebconfig()
72
    {
73
        //<add fileExtension="supersake" allowed="false"/>
74
    }
75
}
76