Passed
Push — develop ( 7930dc...d757ee )
by Brent
02:47
created

Htaccess::getPageBlock()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 2
nop 1
dl 0
loc 16
rs 9.4285
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\HtaccessContainer;
9
use Tivie\HtaccessParser\Parser;
10
use Tivie\HtaccessParser\Token\Block;
11
12
class Htaccess
13
{
14
    /**
15
     * @var Filesystem
16
     */
17
    private $fs;
18
19
    /**
20
     * @var Parser
21
     */
22
    private $parser;
23
24
    /**
25
     * @var \ArrayAccess|HtaccessContainer
26
     */
27
    private $contents;
28
29
    /**
30
     * Htaccess constructor.
31
     *
32
     * @param string $path
33
     *
34
     * @throws ConfigurationException
35
     */
36
    public function __construct(string $path) {
37
        $this->fs = new Filesystem();
38
39
        if (!$this->fs->exists($path)) {
40
            $this->fs->dumpFile($path, file_get_contents(__DIR__ . '/../../../../.htaccess'));
41
        }
42
43
        $this->parser = new Parser(new \SplFileObject($path));
44
        $this->parser->ignoreWhitelines(false);
45
        $this->contents = $this->parser->parse();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->parser->parse() can also be of type array. However, the property $contents is declared as type object<ArrayAccess>|obje...rser\HtaccessContainer>. 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...
46
    }
47
48
    /**
49
     * Parse the modified .htaccess
50
     *
51
     * @return string
52
     */
53
    public function parse() : string {
54
        return (string) $this->contents;
55
    }
56
57
    /**
58
     * Get or create the headers block
59
     *
60
     * @return Block
61
     */
62
    public function &getHeaderBlock() : Block {
63
        $headerBlock = $this->findHeaderBlockByModName('mod_headers.c');
64
65
        if (!$headerBlock) {
66
            $headerBlock = new Block('ifmodule');
67
            $headerBlock->addArgument('mod_headers.c');
68
            $this->contents->append($headerBlock);
0 ignored issues
show
Bug introduced by
The method append does only exist in Tivie\HtaccessParser\HtaccessContainer, but not in ArrayAccess.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
69
        }
70
71
        return $headerBlock;
72
    }
73
74
    /**
75
     * Get or create a page block within the headers block
76
     *
77
     * @param Page $page
78
     *
79
     * @return Block
80
     */
81
    public function &getPageBlock(Page $page) : Block {
82
        $headerBlock = $this->getHeaderBlock();
83
        $pageId = trim($page->getId(), '/') ?? 'index';
84
        $pageId = pathinfo($pageId !== '' ? "{$pageId}" : 'index', PATHINFO_BASENAME);
85
        $pageName = '"^' . $pageId . '\.html$"';
86
87
        $pageBlock = $this->findPageBlockByParentAndName($headerBlock, $pageName);
88
89
        if (!$pageBlock) {
90
            $pageBlock = new Block('filesmatch');
91
            $pageBlock->addArgument($pageName);
92
            $headerBlock->addChild($pageBlock);
93
        }
94
95
        return $pageBlock;
96
    }
97
98
    /**
99
     * Clear all page header blocks
100
     */
101
    public function clearPageBlocks() : void {
102
        $headerBlock = $this->getHeaderBlock();
103
104
        foreach ($headerBlock as $content) {
105
            if ($content instanceof Block && strtolower($content->getName()) === 'filesmatch') {
106
                $headerBlock->removeChild($content);
107
            }
108
        }
109
    }
110
111
    /**
112
     * @param Block  $headerBlock
113
     * @param string $pageName
114
     *
115
     * @return null|Block
116
     */
117
    private function findPageBlockByParentAndName(Block $headerBlock, string $pageName) : ?Block {
118
        foreach ($headerBlock as $content) {
119
            if ($content instanceof Block
120
                && strtolower($content->getName()) === 'filesmatch'
121
                && count($content->getArguments())
122
                && $content->getArguments()[0] === $pageName
123
            ) {
124
                return $content;
125
            }
126
        }
127
128
        return null;
129
    }
130
131
    /**
132
     * @param string $modName
133
     *
134
     * @return null|Block
135
     */
136
    private function findHeaderBlockByModName(string $modName) : ?Block {
137
        foreach ($this->contents as $content) {
138
            if ($content instanceof Block
139
                && strtolower($content->getName()) === 'ifmodule'
140
                && count($content->getArguments())
141
                && $content->getArguments()[0] === $modName
142
            ) {
143
                return $content;
144
            }
145
        }
146
147
        return null;
148
    }
149
}
150