Completed
Branch 1.0 (9c4148)
by Mike
03:56
created

ClassParser::getFileName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Mediadevs\Validator\Helpers;
4
5
use ReflectionClass;
6
use ReflectionException;
7
use Mediadevs\Validator\Filters\FilterInterface;
8
9
class ClassParser
10
{
11
    /**
12
     * Parsing the filter to collect it's properties and configuration.
13
     *
14
     * @param string $namespace
15
     *
16
     * @throws ReflectionException
17
     *
18
     * @return array
19
     */
20
    public function parse(string $namespace): array
21
    {
22
        // Results will be stored in here
23
        $collection = array();
24
25
        // Collecting the classname and instantiating the class
26
        $filename = $this->getFileName($namespace);
27
        $this->loadFile($filename);
28
29
        // Creating a workable instance of the class with empty arrays so the constructor won't fail.
30
        $filter = (new $namespace(array(), array()));
31
32
        // Validating whether all the properties exist.
33
        $hasIdentifier = $this->hasProperty($filter, 'identifier');
34
        $hasAliases = $this->hasProperty($filter, 'aliases');
35
        $hasMessage = $this->hasProperty($filter, 'message');
36
37
        // Validating whether the properties exist.
38
        if ($hasIdentifier && $hasAliases) {
39
            $collection['identifiers'] = $this->getProperty($filter, 'identifier');
40
            $collection['aliases'] = $this->getProperty($filter, 'identifier');
41
42
            // This only comes in action when it is a custom filter.
43
            if ($hasMessage) {
44
                $collection['message'] = $this->getProperty($filter, 'message');
45
            }
46
        }
47
48
        return $collection;
49
    }
50
51
    /**
52
     * Collecting the filename from the filter using a reflection class.
53
     *
54
     * @param string $namespace
55
     *
56
     * @throws ReflectionException
57
     *
58
     * @return string
59
     */
60
    private function getFileName(string $namespace): string
61
    {
62
        return (new ReflectionClass($namespace))->getFileName();
63
    }
64
65
    /**
66
     * Requiring the file in case it is a custom filter, else the files will be automatically loaded by composer.
67
     *
68
     * @param string $filename
69
     *
70
     * @return mixed
71
     */
72
    private function loadFile(string $filename)
73
    {
74
        return require_once $filename;
75
    }
76
77
    /**
78
     * Whether the filter has the given property or not.
79
     *
80
     * @param FilterInterface $filter
81
     * @param string          $property
82
     *
83
     * @return bool
84
     */
85
    private function hasProperty(FilterInterface $filter, string $property): bool
86
    {
87
        return (bool) property_exists($filter, $property) ? true : false;
88
    }
89
90
    /**
91
     * Collecting the property from the filter.
92
     *
93
     * @param FilterInterface $filter
94
     * @param string          $property
95
     *
96
     * @return mixed
97
     */
98
    private function getProperty(FilterInterface $filter, string $property)
99
    {
100
        return $filter->getProperty($property);
0 ignored issues
show
Bug introduced by
The method getProperty() does not exist on Mediadevs\Validator\Filters\FilterInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Mediadevs\Validator\Filters\FilterInterface. ( Ignorable by Annotation )

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

100
        return $filter->/** @scrutinizer ignore-call */ getProperty($property);
Loading history...
101
    }
102
}
103