Passed
Push — develop ( 716c8d...45cc91 )
by Brent
02:50
created

Htaccess::clearPageBlocks()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 0
dl 0
loc 10
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Site\Http;
4
5
use Brendt\Stitcher\Exception\ConfigurationException;
6
use Brendt\Stitcher\Site\Page;
7
use Symfony\Component\Filesystem\Filesystem;
8
use Tivie\HtaccessParser\Parser;
9
use Tivie\HtaccessParser\Token\Block;
10
11
class Htaccess
12
{
13
    /**
14
     * @var Filesystem
15
     */
16
    private $fs;
17
18
    /**
19
     * @var Parser
20
     */
21
    private $parser;
22
23
    /**
24
     * @var array
25
     */
26
    private $contents;
27
28
    /**
29
     * Htaccess constructor.
30
     *
31
     * @param string $path
32
     *
33
     * @throws ConfigurationException
34
     */
35
    function __construct(string $path) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
36
        $this->fs = new Filesystem();
37
38
        if (!$this->fs->exists($path)) {
39
            $this->fs->dumpFile($path, __DIR__ . '/../../../../.htaccess');
40
        }
41
42
        $this->parser = new Parser(new \SplFileObject($path));
43
        $this->parser->ignoreWhitelines(false);
44
        $this->contents = $this->parser->parse();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parser->parse() can also be of type object<ArrayAccess>. However, the property $contents is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
45
    }
46
47
    /**
48
     * Parse the modified .htaccess
49
     *
50
     * @return string
51
     */
52
    public function parse() : string {
53
        return (string) $this->contents;
54
    }
55
56
    /**
57
     * Get or create the headers block
58
     *
59
     * @return Block
60
     */
61
    public function &getHeaderBlock() : Block {
62
        $headerBlock = null;
63
64
        foreach ($this->contents as $content) {
65
            if ($content instanceof Block
66
                && strtolower($content->getName()) === 'ifmodule'
67
                && count($content->getArguments())
68
                && $content->getArguments()[0] === 'mod_headers.c'
69
            ) {
70
                $headerBlock = $content;
71
72
                break;
73
            }
74
        }
75
76
        if (!$headerBlock) {
77
            $headerBlock = new Block('ifmodule', 'mod_headers.c');
78
            $this->contents->append($headerBlock);
0 ignored issues
show
Bug introduced by
The method append cannot be called on $this->contents (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
79
        }
80
81
        return $headerBlock;
82
    }
83
84
    /**
85
     * Get or create a page block within the headers block
86
     *
87
     * @param Page $page
88
     *
89
     * @return mixed|null|Block
90
     */
91
    public function &getPageBlock(Page $page) {
92
        $pageBlock = null;
93
        $headerBlock = $this->getHeaderBlock();
94
        $pageId = trim($page->getId(), '/');
95
        $pageName = '"^\/' . $pageId . '.html$"';
96
97
        foreach ($headerBlock as $content) {
98
            if ($content instanceof Block
99
                && strtolower($content->getName()) === 'filesmatch'
100
                && count($content->getArguments())
101
                && $content->getArguments()[0] === $pageName
102
            ) {
103
                $pageBlock = $content;
104
105
                break;
106
            }
107
        }
108
109
        if (!$pageBlock) {
110
            $pageBlock = new Block('filesmatch');
111
            $pageBlock->addArgument($pageName);
112
            $headerBlock->addChild($pageBlock);
113
        }
114
115
        return $pageBlock;
116
    }
117
118
    public function clearPageBlocks() {
119
        $pageBlock = null;
0 ignored issues
show
Unused Code introduced by
$pageBlock 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...
120
        $headerBlock = $this->getHeaderBlock();
121
122
        foreach ($headerBlock as $content) {
123
            if ($content instanceof Block && strtolower($content->getName()) === 'filesmatch') {
124
                $headerBlock->removeChild($content);
125
            }
126
        }
127
    }
128
}
129