ExposuresFactory::fromValues()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 10
loc 10
c 0
b 0
f 0
ccs 7
cts 7
cp 1
rs 9.9332
cc 3
nc 3
nop 1
crap 3
1
<?php
2
namespace FilmTools\Commons;
3
4
5 View Code Duplication
class ExposuresFactory
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
6
{
7
8
    /**
9
     * @var string
10
     */
11
    public $php_exposures_class;
12
13
14
    /**
15
     * @param string|null $php_exposures_class
16
     */
17 30
    public function __construct( string $php_exposures_class = null )
18
    {
19 30
        $this->php_exposures_class = $php_exposures_class ?: Exposures::class;
20
21 30
        if (!is_subclass_of($this->php_exposures_class, ExposuresInterface::class ))
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \FilmTools\Commons\ExposuresInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
22 3
            throw new FilmToolsInvalidArgumentException("Class name must implement ExposuresInterface.");
23
24 27
    }
25
26
27
    /**
28
     * @param  array  $data
29
     * @return ExposuresInterface
30
     */
31 18
    public function fromValues( $data ) : ExposuresInterface
32
    {
33 18
        if ($data instanceOf \Traversable )
34 6
            $data = iterator_to_array( $data );
35 12
        else if (!is_array($data))
36 9
            throw new FilmToolsInvalidArgumentException("Array or Traversable expected");
37
38 9
        $php_class = $this->php_exposures_class;
39 9
        return new $php_class( array_values( $data ));
40
    }
41
42
43
    /**
44
     * @param  array  $data
45
     * @return ExposuresInterface
46
     */
47 9
    public function fromKeys( $data ) : ExposuresInterface
48
    {
49 9
        if ($data instanceOf \Traversable )
50 6
            $data = iterator_to_array( $data );
51 3
        else if (!is_array($data))
52
            throw new FilmToolsInvalidArgumentException("Array or Traversable expected");
53
54 9
        $php_class = $this->php_exposures_class;
55 9
        return new $php_class( array_keys( $data ));
56
    }
57
}
58