PathAnalyzer::detectFilenameWithoutDate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Symplify
7
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
8
 */
9
10
namespace Symplify\PHP7_Sculpin\Utils;
11
12
use DateTime;
13
use DateTimeInterface;
14
use SplFileInfo;
15
16
final class PathAnalyzer
17
{
18 5
    public static function startsWithDate(SplFileInfo $file) : bool
19
    {
20 5
        return (bool) preg_match('/(\d{4})[\/\-]*(\d{2})[\/\-]*(\d{2})[\/\-]*(\d+|)/', $file->getFilename(), $matches);
21
    }
22
23 4
    public static function detectDate(SplFileInfo $file) : DateTimeInterface
24
    {
25 4
        preg_match('/(\d{4})[\/\-]*(\d{2})[\/\-]*(\d{2})[\/\-]*(\d+|)/', $file->getFilename(), $matches);
26 4
        list($dummy, $year, $month, $day) = $matches;
0 ignored issues
show
Unused Code introduced by
The assignment to $dummy is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
27
28 4
        return new DateTime(implode('-', [$year, $month, $day]));
29
    }
30
31 4
    public static function detectFilenameWithoutDate(SplFileInfo $file) : string
32
    {
33 4
        preg_match(
34 4
            '/(\d{4})[\/\-]*(\d{2})[\/\-]*(\d{2})[\/\-]*(.+?)(\.[^\.]+|\.[^\.]+\.[^\.]+)$/',
35 4
            $file->getFilename(),
36
            $matches
37
        );
38
39 4
        return $matches[4];
40
    }
41
}
42