Completed
Push — master ( 6c82ee...baa3a3 )
by Anatol
03:29
created

Ini::setupPickleSectionPositions()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 36
rs 8.439
ccs 20
cts 20
cp 1
cc 5
eloc 20
nc 4
nop 0
crap 5
1
<?php
2
3
/**
4
 * Pickle
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2015-2015, Pickle community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
37
namespace Pickle\Engine\PHP;
38
39
use Pickle\Base\Interfaces;
40
use Pickle\Base\Abstracts;
41
42
class Ini extends Abstracts\Engine\Ini implements Interfaces\Engine\Ini
43
{
44
    public function __construct(Interfaces\Engine $php)
45
    {
46 1
        parent::__construct($php);
47
48 1
        $this->setupPickleSectionPositions();
49 1
    }
50
51
    /**
52
     * @param string $pickleSection
53
     * @param array  $dlls
0 ignored issues
show
Bug introduced by
There is no parameter named $dlls. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
54
     *
55
     * @return string
56
     */
57
    protected function rebuildPickleParts($pickleSection, array $dlls_add, array $dlls_del = array())
58
    {
59 1
        $lines = explode("\n", $pickleSection);
60 1
        $new = [];
61
62
        /* First add the lines for exts that are requested to be added. */
63 1
        foreach ($dlls_add as $dll) {
64 1
            $new[] =  $this->buildDllIniLine($dll);
65 1
        }
66
67
        /* Then, go over the existing lines, restore those that are not
68
            requested to be deleted and not already added. */
69 1
        foreach ($lines as $l) {
70 1
            $l = trim($l);
71 1
            if (0 !== strpos($l, 'extension')) {
72 1
                continue;
73
            }
74 1
            list(, $dllname) = explode('=', $l);
75
76 1
            if (in_array(trim($dllname), $dlls_add)) {
77
                /* don't create a duplicated item */
78 1
                continue;
79
            }
80 1
            if (in_array(trim($dllname), $dlls_del)) {
81
                /* don't restore as it should be deleted */
82 1
                continue;
83
            }
84 1
            $new[] = $l;
85 1
        }
86
87 1
        sort($new);
88
89 1
        return implode("\n", $new);
90
    }
91
92
    protected function setupPickleSectionPositions()
93
    {
94 1
        $posHeader = strpos($this->raw, self::PICKLE_HEADER);
95 1
        if (false === $posHeader) {
96
            /* no pickle section here yet */
97 1
            $this->pickleHeaderStartPos = strlen($this->raw);
98
99 1
            return;
100
        }
101
102 1
        $this->pickleHeaderStartPos = $posHeader;
103 1
        $this->pickleHeaderEndPos = $this->pickleHeaderStartPos + strlen(self::PICKLE_HEADER);
104
105 1
        $posFooter = strpos($this->raw, self::PICKLE_FOOTER);
106 1
        if (false === $posFooter) {
107
            /* This is bad, no end of section marker, will have to lookup. The strategy is
108
                - look for the last extension directve after the header
109
                - extension directives are expected to come one after another one per line
110
                - comments are not expected inbetveen
111
                - mark the next pos after the last extension directive as the footer pos
112
            */
113 1
            $pos = $this->pickleHeaderEndPos;
114
            do {
115 1
                $pos = strpos($this->raw, 'extension', $pos);
116 1
                if (false !== $pos) {
117 1
                    $this->pickleFooterStartPos = $pos;
118 1
                    $pos++;
119 1
                }
120 1
            } while (false !== $pos);
121
122 1
            $this->pickleFooterStartPos = strpos($this->raw, "\n", $this->pickleFooterStartPos);
123 1
        } else {
124 1
            $this->pickleFooterStartPos = $posFooter;
125 1
            $this->pickleFooterEndPos = $this->pickleFooterStartPos + strlen(self::PICKLE_FOOTER);
126
        }
127 1
    }
128
129
    public function updatePickleSection(array $dlls_add, array $dlls_del = array())
130
    {
131 1
        $before = '';
0 ignored issues
show
Unused Code introduced by
$before 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...
132 1
        $after = '';
0 ignored issues
show
Unused Code introduced by
$after 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...
133
134 1
        $pickleSection = $this->rebuildPickleParts($this->getPickleSection(), $dlls_add, $dlls_del);
135
136 1
        $before = substr($this->raw, 0, $this->pickleHeaderStartPos);
137
138
        /* If the footer end pos is < 0, there was no footer in php.ini. In this case the footer start pos
139
           means the end of the last extension directive after the header start, where the footer should  be */
140 1
        if ($this->pickleFooterEndPos > 0) {
141 1
            $after = substr($this->raw, $this->pickleFooterEndPos);
142 1
        } else {
143 1
            $after = substr($this->raw, $this->pickleFooterStartPos);
144
        }
145
146 1
        $before = rtrim($before);
147 1
        $after = ltrim($after);
148
149 1
        $this->raw = $before."\n\n".self::PICKLE_HEADER."\n".trim($pickleSection)."\n".self::PICKLE_FOOTER."\n\n".$after;
150 1
        if (!@file_put_contents($this->path, $this->raw)) {
151
            throw new \Exception('Cannot update php.ini');
152
        }
153 1
    }
154
155
    protected function buildDllIniLine($dll)
156
    {
157 1
        return 'extension='.$dll;
158
    }
159
}
160
161
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
162