Completed
Push — master ( bdb3d4...9f5e72 )
by Anatol
03:32
created

Ini::updatePickleSection()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 14
nc 4
nop 2
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\HHVM;
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
        parent::__construct($php);
47
48
        $this->setupPickleSectionPositions();
49
    }
50
51
    /**
52
     * @param string $pickleSection
53
     * @param array  $dsos
0 ignored issues
show
Bug introduced by
There is no parameter named $dsos. 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 $dsos_add, array $dsos_del = array())
58
    {
59
        $lines = explode("\n", $pickleSection);
60
        $new = [];
61
62
        $names = array();
0 ignored issues
show
Unused Code introduced by
$names 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...
63
        foreach ($dsos_add as $dso) {
64
            $new[] = $this->buildDsoIniLine($dso);
65
        }
66
67
        foreach ($lines as $l) {
68
            $l = trim($l);
69
            if (0 !== strpos($l, 'hhvm.pickle_extensions[')) {
70
                continue;
71
            }
72
            list(, $dllname) = explode('=', $l);
73
74
            if (in_array(trim($dllname), $dsos_add)) {
75
                /* don't create a duplicated item */
76
                continue;
77
            }
78
            if (in_array(trim($dllname), $dsos_del)) {
79
                /* don't restore as it should be deleted */
80
                continue;
81
            }
82
            $new[] = $l;
83
        }
84
85
        return implode("\n", $new);
86
    }
87
88
    protected function setupPickleSectionPositions()
89
    {
90
        $posHeader = strpos($this->raw, self::PICKLE_HEADER);
91
        if (false === $posHeader) {
92
            /* no pickle section here yet */
93
            $this->pickleHeaderStartPos = strlen($this->raw);
94
95
            return;
96
        }
97
98
        $this->pickleHeaderStartPos = $posHeader;
99
        $this->pickleHeaderEndPos = $this->pickleHeaderStartPos + strlen(self::PICKLE_HEADER);
100
101
        $posFooter = strpos($this->raw, self::PICKLE_FOOTER);
102
        if (false === $posFooter) {
103
            /* This is bad, no end of section marker, will have to lookup. The strategy is
104
                - look for the last extension directve after the header
105
                - extension directives are expected to come one after another one per line
106
                - comments are not expected inbetveen
107
                - mark the next pos after the last extension directive as the footer pos
108
            */
109
            $pos = $this->pickleHeaderEndPos;
110
            do {
111
                $pos = strpos($this->raw, 'hhvm.dynamic_extensions[', $pos);
112
                if (false !== $pos) {
113
                    $this->pickleFooterStartPos = $pos;
114
                    $pos++;
115
                }
116
            } while (false !== $pos);
117
118
            $this->pickleFooterStartPos = strpos($this->raw, "\n", $this->pickleFooterStartPos);
119
        } else {
120
            $this->pickleFooterStartPos = $posFooter;
121
            $this->pickleFooterEndPos = $this->pickleFooterStartPos + strlen(self::PICKLE_FOOTER);
122
        }
123
    }
124
125
    public function updatePickleSection(array $dsos_add, array $dsos_del = array())
126
    {
127
        $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...
128
        $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...
129
130
        $pickleSection = $this->rebuildPickleParts($this->getPickleSection(), $dsos_add, $dsos_del);
131
132
        $before = substr($this->raw, 0, $this->pickleHeaderStartPos);
133
134
        /* If the footer end pos is < 0, there was no footer in php.ini. In this case the footer start pos
135
           means the end of the last extension directive after the header start, where the footer should  be */
136
        if ($this->pickleFooterEndPos > 0) {
137
            $after = substr($this->raw, $this->pickleFooterEndPos);
138
        } else {
139
            $after = substr($this->raw, $this->pickleFooterStartPos);
140
        }
141
142
        $before = rtrim($before);
143
        $after = ltrim($after);
144
145
        $this->raw = $before."\n\n".self::PICKLE_HEADER."\n".trim($pickleSection)."\n".self::PICKLE_FOOTER."\n\n".$after;
146
        if (!@file_put_contents($this->path, $this->raw)) {
147
            throw new \Exception('Cannot update php.ini');
148
        }
149
    }
150
151
    protected function buildDsoName($dso)
152
    {
153
        /* HHVM currently doesn't support Windows, so just a guess here. */
154
        $pre = defined('PHP_WINDOWS_VERSION_MAJOR') ? 'php_' : '';
155
        $suf = defined('PHP_WINDOWS_VERSION_MAJOR') ? '.dll' : '.so';
156
157
        return "$pre$dso$suf";
158
    }
159
160
    protected function buildDsoIniLine($dso)
161
    {
162
        return "hhvm.dynamic_extensions[$dso]=".$this->buildDsoIniLine($dso);
163
    }
164
}
165
166
/* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */
167