Completed
Push — master ( 598fe1...4a905b )
by Michael
03:41
created

RelativeFileSearchTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 20%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 78
ccs 4
cts 20
cp 0.2
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setRelativeBaseDir() 0 6 1
B findRelativeFileWithPath() 0 27 4
A getRelativeBaseDir() 0 8 2
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains RelativeFileSearchTrait Trait.
5
 *
6
 * PHP version 7.0+
7
 *
8
 * LICENSE:
9
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
10
 * which can be used to access the Eve Online API data and place it into a
11
 * database.
12
 * Copyright (C) 2015-2016 Michael Cummings
13
 *
14
 * This program is free software: you can redistribute it and/or modify it
15
 * under the terms of the GNU Lesser General Public License as published by the
16
 * Free Software Foundation, either version 3 of the License, or (at your
17
 * option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
22
 * for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public License
25
 * along with this program. If not, see
26
 * <http://spdx.org/licenses/LGPL-3.0.html>.
27
 *
28
 * You should be able to find a copy of this license in the COPYING-LESSER.md
29
 * file. A copy of the GNU GPL should also be available in the COPYING.md file.
30
 *
31
 * @copyright 2015-2016 Michael Cummings
32
 * @license   http://www.gnu.org/copyleft/lesser.html GNU LGPL
33
 * @author    Michael Cummings <[email protected]>
34
 */
35
namespace Yapeal\FileSystem;
36
37
use FilePathNormalizer\FilePathNormalizerTrait;
38
use Yapeal\Exception\YapealFileSystemException;
39
40
/** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
41
/**
42
 * Trait RelativeFileSearchTrait
43
 *
44
 * @method \Yapeal\Event\MediatorInterface getYem()
45
 */
46
trait RelativeFileSearchTrait
47
{
48
    use FilePathNormalizerTrait;
49
    /**
50
     * Fluent interface setter for $relativeBaseDir.
51
     *
52
     * @param string $value
53
     *
54
     * @return RelativeFileSearchTrait Fluent interface.
0 ignored issues
show
Comprehensibility Bug introduced by
The return type RelativeFileSearchTrait is a trait, and thus cannot be used for type-hinting in PHP. Maybe consider adding an interface and use that for type-hinting?

In PHP traits cannot be used for type-hinting as they do not define a well-defined structure. This is because any class that uses a trait can rename that trait’s methods.

If you would like to return an object that has a guaranteed set of methods, you could create a companion interface that lists these methods explicitly.

Loading history...
55
     */
56 1
    public function setRelativeBaseDir(string $value): self
57
    {
58 1
        $this->relativeBaseDir = $this->getFpn()
59 1
            ->normalizePath($value);
60 1
        return $this;
61
    }
62
    /**
63
     * Used to find a file relative to the base path using prefix, name, and suffix parts in varies ways.
64
     *
65
     * @param string $prefix Used as subdirectory or as part of file name.
66
     * @param string $name   Used as part of file names only.
67
     * @param string $suffix Used as last part of file name or by self as file name. Think file extension without
68
     *                       leading dot.
69
     *
70
     * @return string
71
     * @throws \DomainException
72
     * @throws \InvalidArgumentException
73
     * @throws \LogicException
74
     * @throws \Yapeal\Exception\YapealFileSystemException
75
     */
76
    protected function findRelativeFileWithPath(string $prefix, string $name, string $suffix): string
77
    {
78
        // $relDir$prefix/$name.$suffix, $relDir$prefix/$name_$suffix,
1 ignored issue
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
        // $relDir$prefix.$name.$suffix, $relDir$prefix_$name.$suffix, $relDir$prefix_$name_$suffix,
1 ignored issue
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
        // $relDir$name.$suffix, $relDir$name_$suffix,
1 ignored issue
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
        // $relDir$prefix.$suffix, $relDir$prefix_$suffix,
1 ignored issue
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
        // $relDir'common'.$suffix, $relDir'common'_$suffix, $relDir$suffix
1 ignored issue
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
83
        $combinations = '%1$s%2$s/%3$s.%4$s,%1$s%2$s/%3$s_%4$s'
84
            . ',%1$s%2$s.%3$s.%4$s,%1$s%2$s_%3$s.%4$s,%1$s%2$s_%3$s_%4$s'
85
            . ',%1$s%3$s.%4$s,%1$s%3$s_%4$s'
86
            . ',%1$s%2$s.%4$s,%1$s%2$s_%4$s'
87
            . ',%1$scommon.%4$s,%1$scommon_%4$s,%1$s%4$s';
88
        $fileNames = explode(',', sprintf($combinations, $this->getRelativeBaseDir(), $prefix, $name, $suffix));
89
        $fpn = $this->getFpn();
90
        foreach ($fileNames as $fileName) {
91
            $fileName = $fpn->normalizeFile($fileName);
92
            if (is_readable($fileName) && is_file($fileName)) {
93
                return $fileName;
94
            }
95
        }
96
        $mess = sprintf('Failed to find accessible file in %1$s using "%1$s", "%2$s", and "%3$s"',
97
            $this->getRelativeBaseDir(),
98
            $prefix,
99
            $name,
100
            $suffix);
101
        throw new YapealFileSystemException($mess);
102
    }
103
    /**
104
     * Getter for $relativeBaseDir.
105
     *
106
     * @return string
107
     * @throws \LogicException
108
     */
109
    private function getRelativeBaseDir(): string
110
    {
111
        if (null === $this->relativeBaseDir) {
112
            $mess = 'Tried to use relativeBaseDir before it was set';
113
            throw new \LogicException($mess);
114
        }
115
        return $this->relativeBaseDir;
116
    }
117
    /**
118
     * Holds the path that is prepended for searches.
119
     *
120
     * @var string $relativeBaseDir
121
     */
122
    private $relativeBaseDir;
123
}
124