EndsWith   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 9 3
A fileEndsWith() 0 8 2
1
<?php
2
namespace exussum12\CoverageChecker\FileMatchers;
3
4
use exussum12\CoverageChecker\FileMatcher;
5
use exussum12\CoverageChecker\Exceptions\FileNotFound;
6
7
/**
8
 * Class EndsWith
9
 * @package exussum12\CoverageChecker\FileMatchers
10
 */
11
class EndsWith implements FileMatcher
12
{
13
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function match(string $needle, array $haystack): string
18
    {
19
        foreach ($haystack as $file) {
20
            if ($this->fileEndsWith($file, $needle)) {
21
                return $file;
22
            }
23
        }
24
25
        throw new FileNotFound();
26
    }
27
28
    /**
29
     * Find if two strings end in the same way
30
     */
31
    protected function fileEndsWith(string $haystack, string $needle): bool
32
    {
33
        $length = strlen($needle);
34
        if (strlen($haystack) < $length) {
35
            return $this->fileEndsWith($needle, $haystack);
36
        }
37
38
        return (substr($haystack, -$length) === $needle);
39
    }
40
}
41