Completed
Pull Request — master (#611)
by Kévin
04:43 queued 01:28
created

Reflection::getReflectionProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 5
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Util;
13
14
/**
15
 * Reflection utilities.
16
 *
17
 * @internal
18
 *
19
 * @author Kévin Dunglas <[email protected]>
20
 */
21
final class Reflection
22
{
23
    const ACCESSOR_PREFIXES = ['get', 'is', 'has', 'can'];
24
    const MUTATOR_PREFIXES = ['set', 'add', 'remove'];
25
26
    /**
27
     * Gets the property name associated with an accessor method.
28
     *
29
     * @param string $methodName
30
     *
31
     * @return string|null
32
     */
33
    public function getProperty($methodName)
34
    {
35
        $pattern = implode('|', array_merge(self::ACCESSOR_PREFIXES, self::MUTATOR_PREFIXES));
36
37
        if (preg_match('/^('.$pattern.')(.+)$/i', $methodName, $matches)) {
38
            return $matches[2];
39
        }
40
    }
41
}
42