Completed
Push — master ( ef6746...5a635f )
by Ron
07:26
created

CallbackViewFactory::addPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace View\Factories;
3
4
use View\Helpers\Directories;
5
use View\Renderer;
6
use View\ViewFactory;
7
8
class CallbackViewFactory implements ViewFactory {
9
	/** @var callable */
10
	private $callback;
11
	/** @var null */
12
	private $baseDir;
13
	/** @var array */
14
	private $vars;
15
	/** @var array */
16
	private $config = [];
17
	
18
	/**
19
	 * @param callable $callback
20
	 * @param string $baseDir
21
	 * @param array $vars
22
	 * @param array $config
23
	 */
24
	public function __construct($callback, $baseDir = null, array $vars = [], array $config = []) {
25
		$this->callback = $callback;
26
		$this->baseDir = $baseDir;
0 ignored issues
show
Documentation Bug introduced by
It seems like $baseDir can also be of type string. However, the property $baseDir is declared as type null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
27
		$this->vars = $vars;
28
		$this->config = array_merge(['paths' => []], $config);
29
	}
30
	
31
	/**
32
	 * @param string $name
33
	 * @param string $path
34
	 * @return $this
35
	 */
36
	public function addPath($name, $path) {
37
		$this->config['paths'][$name] = $path;
38
		return $this;
39
	}
40
41
	/**
42
	 * @param string $baseDir
43
	 * @param array $vars
44
	 * @return Renderer
45
	 */
46 View Code Duplication
	public function create($baseDir = null, array $vars = []) {
0 ignored issues
show
Duplication introduced by
This method 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...
47
		if($baseDir === null) {
48
			$baseDir = $this->baseDir;
49
		} elseif($this->baseDir !== null) {
50
			$baseDir = Directories::concat($this->baseDir, $baseDir);
51
		}
52
		return call_user_func($this->callback, $baseDir, $vars, $this->config);
53
	}
54
55
	/**
56
	 * @param string $baseDir
57
	 * @param array $vars
58
	 * @return $this
59
	 */
60 View Code Duplication
	public function deriveFactory($baseDir = null, array $vars = []) {
0 ignored issues
show
Duplication introduced by
This method 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...
61
		if($baseDir === null) {
62
			$baseDir = $this->baseDir;
63
		} elseif($this->baseDir !== null) {
64
			$baseDir = Directories::concat($this->baseDir, $baseDir);
65
		}
66
		return new static($this->callback, $baseDir, array_merge($this->vars, $vars), $this->config);
67
	}
68
}
69