Passed
Push — master ( e6bc2a...a7212f )
by Kirill
04:45
created

TypeLoader::withExtension()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\SDL\TypeLoader;
11
12
/**
13
 * Class TypeLoader
14
 */
15
abstract class TypeLoader implements TypeLoaderInterface
16
{
17
    /**
18
     * @var string[]
19
     */
20
    private const DEFAULT_SCHEMA_EXTENSIONS = [
21
        'graphqls',
22
        'graphql',
23
        'gql',
24
    ];
25
26
    /**
27
     * @var array|string[]
28
     */
29
    protected $fileExtensions = self::DEFAULT_SCHEMA_EXTENSIONS;
30
31
    /**
32
     * @param string $extension
33
     * @return string
34
     */
35
    private function filterExtension(string $extension): string
36
    {
37
        return \ltrim(\trim($extension), '.');
38
    }
39
40
    /**
41
     * @param string $extension
42
     * @return TypeLoader|$this
43
     */
44
    public function withExtension(string $extension): self
45
    {
46
        $this->fileExtensions[] = $this->filterExtension($extension);
47
48
        return $this;
49
    }
50
51
    /**
52
     * @param string $extension
53
     * @return TypeLoader|$this
54
     */
55
    public function withoutExtension(string $extension): self
56
    {
57
        $extension = $this->filterExtension($extension);
58
59
        $filter = function (string $haystack) use ($extension): bool {
60
            return $haystack !== $extension;
61
        };
62
63
        $this->fileExtensions = \array_filter($this->fileExtensions, $filter);
64
65
        return $this;
66
    }
67
}
68