Skip to content

Commit 11248a9

Browse files
committed
Removed the type hint mixed as it is not compatible with PHP 7.4.
Added #[\ReturnTypeWillChange] to methods that return mixed for PHP 8.1 compatibility. Fixed some docblock issues. Updated usage of `str...` functions to be `mb_str...`. Fixed issue in `value::minify()` where if the `decimalplaces` setting was false it would be treated as `0`. Added tests.
1 parent cba4e09 commit 11248a9

File tree

6 files changed

+71
-26
lines changed

6 files changed

+71
-26
lines changed

src/cssdoc.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public function __construct(array $config = []) {
227227
* @param string $var The name of the property to retrieve, currently 'length' and output
228228
* @return mixed The number of children in the object for length, the output config, or null if the parameter doesn't exist
229229
*/
230+
#[\ReturnTypeWillChange]
230231
public function __get(string $var) {
231232
if ($var === 'length') {
232233
return \count($this->children);
@@ -284,7 +285,8 @@ public function offsetUnset($i) : void {
284285
* @param string|integer $i The key to be accessed, can be a string or integer
285286
* @return mixed The requested value or null if the key doesn't exist
286287
*/
287-
public function offsetGet($i) : mixed { // return reference so you can set it like an array
288+
#[\ReturnTypeWillChange]
289+
public function offsetGet($i) { // return reference so you can set it like an array
288290
return $this->document->rules[$i] ?? null;
289291
}
290292

@@ -293,7 +295,8 @@ public function offsetGet($i) : mixed { // return reference so you can set it li
293295
*
294296
* @return document|rule The child node at the current pointer position
295297
*/
296-
public function current() : mixed {
298+
#[\ReturnTypeWillChange]
299+
public function current() {
297300
return $this->document->rules[$this->pointer] ?? null;
298301
}
299302

@@ -459,7 +462,7 @@ public function minify(array $minify = []) : void {
459462
* Compile the document to a string
460463
*
461464
* @param array $options An array indicating output options, this is merged with cssdoc::$output
462-
* @return void
465+
* @return string The document as a string
463466
*/
464467
public function compile(array $options = []) : string {
465468
$options = \array_merge($this->config['output'], $options);
@@ -471,7 +474,7 @@ public function compile(array $options = []) : string {
471474
*
472475
* @param string|null $file The file location to save the document to, or null to just return the compiled code
473476
* @param array $options An array indicating output options, this is merged with cssdoc::$output
474-
* @return string|bool The compiled CSS, or false if the file could not be saved
477+
* @return string|false The compiled CSS, or false if the file could not be saved
475478
*/
476479
public function save(string $file = null, array $options = []) {
477480
$css = $this->compile($options);

src/tokens/document.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function minify(array $minify) : void {
8080
* Compile the property to a string
8181
*
8282
* @param array $options An array of compilation options
83-
* @return void
83+
* @return string The compiled document
8484
*/
8585
public function compile(array $options) : string {
8686
$b = $options['style'] !== 'minify';
@@ -102,7 +102,7 @@ public function compile(array $options) : string {
102102
* @param array|string $hasProp A string or array specifying the properties that any rules must contain
103103
* @param array $media An array specifying how any media queries should be match, where the key is the property and the key the value. 'max-width' will match any rules where the value is lower that that specified, 'min-width' the value must be higher. Use 'media' to specify the media type
104104
* @param bool $exact Denotes whether to match selectors exactly, if false, selectors will be matched from the left
105-
* @return cssdoc A CSSdoc object
105+
* @return array A CSSdoc object
106106
*/
107107
public function find(?array $selectors, $hasProp = null, array $media = [], bool $exact = true) {
108108
$rules = [];

src/tokens/rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function matches(?array $selectors, array $hasProp = [], bool $exact = tr
147147
}
148148
} else {
149149
foreach ($selectors AS $item) {
150-
if (\mb_stripos($compiled, $item)) {
150+
if (\mb_stripos($compiled, $item) !== false) {
151151
$matches = true;
152152
break;
153153
}

src/tokens/selector.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ public function minify(array $minify) : void {
129129
foreach ($this->selectors AS &$item) {
130130

131131
// minify sub-selector
132-
if (is_object($item)) {
132+
if (\is_object($item)) {
133133
$item->minify($minify);
134134

135135
// change double colon to single colon
136-
} elseif ($minify['selectors'] && (\strpos($item['selector'], '::before') === 0 || \strpos($item['selector'], '::after') === 0)) {
137-
$item['selector'] = substr($item['selector'], 1);
136+
} elseif ($minify['selectors'] && (\mb_strpos($item['selector'], '::before') === 0 || \mb_strpos($item['selector'], '::after') === 0)) {
137+
$item['selector'] = mb_substr($item['selector'], 1);
138138

139139
// quoted attributes
140140
} elseif (\strpbrk($item['selector'], '\'"') !== false && \preg_match('/^((?U).*)([\'"])((?:\\\\.|[^\\2])*)(\\2)(.*)$/i', $item['selector'], $match)) {
@@ -146,12 +146,12 @@ public function minify(array $minify) : void {
146146
// convert quotes
147147
} elseif ($minify['convertquotes'] && $match[2] === "'") {
148148
$match[2] = $match[4] = '"';
149-
$match[3] = str_replace(["\\'", '"'], ["'", '\\"'], $match[3]);
149+
$match[3] = \str_replace(["\\'", '"'], ["'", '\\"'], $match[3]);
150150
}
151151

152152
// recompile
153153
unset($match[0]);
154-
$item['selector'] = implode('', $match);
154+
$item['selector'] = \implode('', $match);
155155
}
156156
}
157157
unset($item);
@@ -167,7 +167,7 @@ public function compile(array $options) : string {
167167
$space = $options['style'] !== 'minify' ? ' ' : '';
168168
$css = '';
169169
foreach ($this->selectors AS $item) {
170-
if (is_object($item)) {
170+
if (\is_object($item)) {
171171
$css .= '('.$item->compile($options).')';
172172
} else {
173173
if ($item['join']) {

src/tokens/value.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public function __construct(cssdoc $root, ?string $name = null, bool $brackets =
4747
* @return bool Whether anything was parsed
4848
*/
4949
public function parse(tokenise $tokens) : bool {
50-
$comment = null;
5150
while (($token = $tokens->next()) !== null) {
5251
switch ($token['type']) {
5352
case 'string':
@@ -105,7 +104,7 @@ public function minify(array $minify) : void {
105104
foreach ($this->properties AS &$item) {
106105

107106
// value in brackets
108-
if (is_object($item)) {
107+
if (\is_object($item)) {
109108
$item->minify($minify);
110109
} else {
111110

@@ -155,11 +154,11 @@ public function minify(array $minify) : void {
155154
$unit = \strtolower($match[6]);
156155

157156
// shorten time values
158-
if ($minify['time'] && $unit === 'ms' && ($len = \strlen($match[3])) >= 3 && $match[3][$len-1] === '0') {
159-
if (($match[4] = rtrim(\substr($match[3], -3), '0')) !== '') {
157+
if ($minify['time'] && $unit === 'ms' && ($len = \mb_strlen($match[3])) >= 3 && $match[3][$len-1] === '0') {
158+
if (($match[4] = \rtrim(\mb_substr($match[3], -3), '0')) !== '') {
160159
$match[4] = '.'.$match[4];
161160
}
162-
$match[3] = $len > 3 ? \substr($match[3], 0, -3) : '';
161+
$match[3] = $len > 3 ? \mb_substr($match[3], 0, -3) : '';
163162
$match[6] = 's';
164163

165164
// remove unit on 0 values, not inside brackets where they must remain
@@ -168,25 +167,25 @@ public function minify(array $minify) : void {
168167
}
169168

170169
// reduce decimal places
171-
if ($minify['decimalplaces'] !== null && \strlen($match[4]) > $minify['decimalplaces']) {
172-
$match[4] = $minify['decimalplaces'] ? \substr($match[4], 0, $minify['decimalplaces'] + 1) : '';
170+
if (!\in_array($minify['decimalplaces'], [null, false], true) && \mb_strlen($match[4]) > $minify['decimalplaces']) {
171+
$match[4] = $minify['decimalplaces'] ? \mb_substr($match[4], 0, $minify['decimalplaces'] + 1) : '';
173172
$match[5] = '';
174173
}
175174

176175
// rebuild value
177176
unset($match[0]);
178-
$item = implode('', $match);
177+
$item = \implode('', $match);
179178
}
180179

181180
// quoted values
182181
if (($single = \mb_strpos($item, "'")) === 0 || \mb_strpos($item, '"') === 0) {
183182

184183
// remove quotes where possible
185-
if ($minify['quotes'] && !in_array($name, $config['quoted'], true) && preg_match('/^("|\')((?!-?\\d)[-_a-z0-9.\\/]++)\\1$/i', $item, $match)) {
184+
if ($minify['quotes'] && !\in_array($name, $config['quoted'], true) && \preg_match('/^("|\')((?!-?\\d)[-_a-z0-9.\\/]++)\\1$/i', $item, $match)) {
186185
$item = $match[2];
187186

188187
// or convert to double quotes
189-
} elseif ($minify['convertquotes'] && $single === 0 && mb_strpos($item, '"') === false) {
188+
} elseif ($minify['convertquotes'] && $single === 0 && \mb_strpos($item, '"') === false) {
190189
$item = '"'.\str_replace("\\'", "'", \trim($item, "'")).'"';
191190
}
192191

@@ -211,7 +210,7 @@ public function minify(array $minify) : void {
211210
$i = 0;
212211
$options = ['style' => 'minify', 'prefix' => ''];
213212
foreach ($this->properties AS $key => $item) {
214-
if (is_object($item)) {
213+
if (\is_object($item)) {
215214
$props[$i-1]['value'] .= '('.$item->compile($options).')';
216215
$props[$i-1]['keys'][] = $key;
217216
} else {
@@ -260,7 +259,7 @@ public function compile(array $options) : string {
260259
}
261260
$css .= '('.$item->compile($options).')';
262261
$join = ' ';
263-
} elseif (\in_array($item, ['-', '+'], true) && !\in_array(mb_strtolower($this->name), $this->root->config['spaced'], true)) {
262+
} elseif (\in_array($item, ['-', '+'], true) && !\in_array(\mb_strtolower($this->name), $this->root->config['spaced'], true)) {
264263
$css .= $item;
265264
$join = '';
266265
} elseif (\in_array($item, [':', ',', '*', '/'], true)) {

tests/cssdocTest.php

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,12 @@ public function testCanRemoveLeadingZeros() {
464464
bottom: -0.5px;
465465
}',
466466
'output' => '#id{top:calc(-.1% + 1em);bottom:-.5px;}'
467+
],
468+
[
469+
'input' => '#id {
470+
animation: 0.5s 0.3s both lines;
471+
}',
472+
'output' => '#id{animation:.5s .3s both lines;}'
467473
]
468474
];
469475
$config = $this->config;
@@ -479,8 +485,9 @@ public function testCanRemoveTrailingZeros() {
479485
transition: all 500ms;
480486
transition-delay: 3.2000s;
481487
padding: 32.5000px;
488+
animation-delay: .5000s;
482489
}',
483-
'output' => '#id{font-size:14em;transition:all 500ms;transition-delay:3.2s;padding:32.5px;}'
490+
'output' => '#id{font-size:14em;transition:all 500ms;transition-delay:3.2s;padding:32.5px;animation-delay:.5s;}'
484491
]
485492
];
486493
$config = $this->config;
@@ -496,6 +503,18 @@ public function testCanReduceDecimalPlaces() {
496503
width: 33.3333333333333333%;
497504
}',
498505
'output' => '#id{font-size:0.9838em;width:33.3333%;}'
506+
],
507+
[
508+
'input' => '#id {
509+
animation: 0.5s 0.4s both lines;
510+
}',
511+
'output' => '#id{animation:0.5s 0.4s both lines;}'
512+
],
513+
[
514+
'input' => '#id {
515+
animation: .5s .4s both lines;
516+
}',
517+
'output' => '#id{animation:.5s .4s both lines;}'
499518
]
500519
];
501520
$config = $this->config;
@@ -748,6 +767,24 @@ public function testCanShortenTimeValues() {
748767
transition: all 00450ms;
749768
}',
750769
'output' => '#id{transition:all 00.45s;}'
770+
],
771+
[
772+
'input' => '#id {
773+
animation: 0.5s 0.4s both lines;
774+
}',
775+
'output' => '#id{animation:0.5s 0.4s both lines;}'
776+
],
777+
[
778+
'input' => '#id {
779+
animation: .5s .4s both lines;
780+
}',
781+
'output' => '#id{animation:.5s .4s both lines;}'
782+
],
783+
[
784+
'input' => '#id {
785+
animation: lines .5s both;
786+
}',
787+
'output' => '#id{animation:lines .5s both;}'
751788
]
752789
];
753790
$config = $this->config;
@@ -832,6 +869,12 @@ public function testCanShortenNone() {
832869
}",
833870
'output' => '#id{border:0;outline:0;background:0;background-color:transparent;}'
834871
],
872+
[
873+
'input' => '#id {
874+
border: 2px solid transparent;
875+
}',
876+
'output' => '#id{border:2px solid transparent;}'
877+
]
835878
];
836879
$config = $this->config;
837880
$config['none'] = true;

0 commit comments

Comments
 (0)