Completed
Push — master ( 83fa52...e2a1f2 )
by Ron
02:29
created

CallbackViewFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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
16
	/**
17
	 * @param callable $callback
18
	 * @param string $baseDir
19
	 * @param array $vars
20
	 */
21
	public function __construct($callback, $baseDir = null, array $vars = []) {
22
		$this->callback = $callback;
23
		$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...
24
		$this->vars = $vars;
25
	}
26
27
	/**
28
	 * @param string $baseDir
29
	 * @param array $vars
30
	 * @return Renderer
31
	 */
32 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...
33
		if($baseDir === null) {
34
			$baseDir = $this->baseDir;
35
		} elseif($this->baseDir !== null) {
36
			$baseDir = Directories::concat($this->baseDir, $baseDir);
37
		}
38
		return call_user_func($this->callback, $baseDir, $vars);
39
	}
40
41
	/**
42
	 * @param string $baseDir
43
	 * @param array $vars
44
	 * @return $this
45
	 */
46 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...
47
		if($baseDir === null) {
48
			$baseDir = $this->baseDir;
49
		} elseif($this->baseDir !== null) {
50
			$baseDir = Directories::concat($this->baseDir, $baseDir);
51
		}
52
		return new static($this->callback, $baseDir, array_merge($this->vars, $vars));
53
	}
54
}
55