Factory::createObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 13
rs 9.9332
1
<?php
2
declare(strict_types=1);
3
4
namespace kosuha606\HtmlUniParser;
5
6
use Assert\Assertion;
7
use ReflectionClass;
8
9
/**
10
 * Static factory for all classes of this package
11
 * @package kosuha606\HtmlUniParser
12
 */
13
final class Factory extends BaseObject
14
{
15
    /**
16
     * @param $config
17
     * @return mixed
18
     * @throws \Assert\AssertionFailedException
19
     * @throws \ReflectionException
20
     */
21
    public static function createObject($classConfig, $constuctorArguments = []): BaseObject
22
    {
23
        Assertion::keyExists($classConfig, 'class', 'Class key is required for Factory method');
24
        $class = $classConfig['class'];
25
        unset($classConfig['class']);
26
        if ($constuctorArguments) {
27
            $reflector = new ReflectionClass($class);
28
            $object = $reflector->newInstanceArgs($constuctorArguments);
29
        } else {
30
            $object = new $class($classConfig);
31
        }
32
        Assertion::isInstanceOf($object, BaseObject::class, 'Only BaseObject instance can be created by this factory');
33
        return $object;
34
    }
35
}