Completed
Branch develop (781062)
by Nate
02:36
created

TransformerHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 27.27%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 38
ccs 3
cts 11
cp 0.2727
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isTransformer() 0 5 3
A isTransformerClass() 0 4 2
A resolve() 0 12 3
1
<?php
2
3
/**
4
 * @author    Flipbox Factory
5
 * @copyright Copyright (c) 2017, Flipbox Digital
6
 * @link      https://github.com/flipbox/transform/releases/latest
7
 * @license   https://github.com/flipbox/transform/blob/master/LICENSE
8
 */
9
10
namespace Flipbox\Transform\Helpers;
11
12
use Flipbox\Transform\Transformers\TransformerInterface;
13
use Closure;
14
15
/**
16
 * @author Flipbox Factory <[email protected]>
17
 * @since 1.0.0
18
 */
19
class TransformerHelper
20
{
21
    /**
22
     * @param $transformer
23
     * @return bool
24
     */
25 3
    public static function isTransformer($transformer): bool
26
    {
27 3
        return (is_object($transformer) && ($transformer instanceof Closure)) ||
28 3
            $transformer instanceof TransformerInterface;
29
    }
30
31
    /**
32
     * @param $transformer
33
     * @return bool
34
     */
35
    public static function isTransformerClass($transformer): bool
36
    {
37
        return is_string($transformer) && is_subclass_of($transformer, TransformerInterface::class);
1 ignored issue
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Flipbox\Transform\Trans...sformerInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
38
    }
39
40
    /**
41
     * @param $transformer
42
     * @return null|callable|TransformerInterface
43
     */
44
    public static function resolve($transformer)
45
    {
46
        if (static::isTransformer($transformer)) {
47
            return $transformer;
48
        }
49
50
        if (static::isTransformerClass($transformer)) {
51
            return new $transformer();
52
        }
53
54
        return null;
55
    }
56
}
57