Passed
Branch main (e68017)
by James Ekow Abaka
10:11
created

ElementFactory::findMethodClass()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
namespace ntentan\honam\engines\php\helpers\form;
3
4
use ntentan\honam\exceptions\HonamException;
5
use ntentan\utils\Text;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ntentan\honam\engines\php\helpers\form\Text. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
7
trait ElementFactory {
8
9
    private $caller;
10
11
    public function findMethodClass($name) {
12
        if($this->caller === null) {
13
            return null;
14
        } else if(method_exists($this->caller, $name)) {
15
            return $this->caller;
16
        } else {
17
            return $this->caller->findMethodClass($name);
18
        }
19
    }
20
21
    public function __call($name, $arguments)
22
    {
23
        // Check if the method exists in the parent class which we call the caller.
24
        $methodClass = $this->findMethodClass($name);
25
        if($methodClass !== null) {
26
            return call_user_func_array([$methodClass, $name], $arguments);
27
        } 
28
        if(str_starts_with($name, "open_")) {
29
            $containerCheck = true;
30
            $name = substr($name, 5);
31
        } else if (str_starts_with($name, "close_")) {
32
            return $this->popContainer(substr($name, 6));
0 ignored issues
show
Bug introduced by
The method popContainer() does not exist on ntentan\honam\engines\ph...ers\form\ElementFactory. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

32
            return $this->/** @scrutinizer ignore-call */ popContainer(substr($name, 6));
Loading history...
33
        } else {
34
            $containerCheck = false;
35
        }
36
        $elementClass = new \ReflectionClass(__NAMESPACE__ . "\\" . Text::ucamelize($name));
37
        $element = $elementClass->newInstanceArgs($arguments == null ? array() : $arguments);
38
        $element->setTemplateRenderer($this->templateRenderer);
39
        $element->setCaller($this);
40
41
        if($containerCheck && is_a($element, __NAMESPACE__ . "\\Container")) {
42
            $this->pushContainer($name, $element);
0 ignored issues
show
Bug introduced by
The method pushContainer() does not exist on ntentan\honam\engines\ph...ers\form\ElementFactory. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

42
            $this->/** @scrutinizer ignore-call */ 
43
                   pushContainer($name, $element);
Loading history...
43
        } else if ($containerCheck) {
44
            throw new HonamException("Element $name is not a container. Use the open_ prefix only when creating a container.");
45
        } else if (!$containerCheck) {
0 ignored issues
show
introduced by
The condition $containerCheck is always false.
Loading history...
46
            $this->getActiveContainer()->add($element);
0 ignored issues
show
Bug introduced by
The method getActiveContainer() does not exist on ntentan\honam\engines\ph...ers\form\ElementFactory. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

46
            $this->/** @scrutinizer ignore-call */ 
47
                   getActiveContainer()->add($element);
Loading history...
47
        }
48
49
        return $element;
50
    }  
51
    
52
    public function setCaller($caller)
53
    {
54
        $this->caller = $caller;
55
    }
56
57
}
58