Passed
Push — master ( fb5c25...257e8e )
by Max
05:13
created

Utils   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 22
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A nowdoc() 0 20 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GraphQL\Tests;
6
7
use function array_slice;
8
use function count;
9
use function implode;
10
use function preg_match;
11
use function preg_split;
12
use function str_replace;
13
14
class Utils
15
{
16
    public static function nowdoc(string $str) : string
17
    {
18
        $lines = preg_split('/\\n/', $str);
19
20
        if (count($lines) <= 2) {
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $var of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

20
        if (count(/** @scrutinizer ignore-type */ $lines) <= 2) {
Loading history...
21
            return '';
22
        }
23
24
        // Toss out the first and last lines.
25
        $lines = array_slice($lines, 1, count($lines) - 2);
0 ignored issues
show
Bug introduced by
It seems like $lines can also be of type false; however, parameter $array of array_slice() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        $lines = array_slice(/** @scrutinizer ignore-type */ $lines, 1, count($lines) - 2);
Loading history...
26
27
        // take the tabs form the first line, and subtract them from all lines
28
        $matches = [];
29
        preg_match('/(^\s+)/', $lines[1], $matches);
30
31
        for ($i = 0; $i < count($lines); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
32
            $lines[$i] = str_replace($matches[0], '', $lines[$i]);
33
        }
34
35
        return implode("\n", $lines);
36
    }
37
}
38