Completed
Push — master ( 294889...618737 )
by Vojtěch
02:15
created

TOverloadTemplates::createComponent()   B

Complexity

Conditions 9
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 7.041
c 0
b 0
f 0
cc 9
eloc 13
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\Application\Components;
6
7
use Nette\Application\UI\Control;
8
use Nette\ComponentModel\IComponent;
9
use Nette\MemberAccessException;
10
use Nette\Reflection\Method;
11
use Nette\UnexpectedValueException;
12
13
trait TOverloadTemplates
14
{
15
	/** @var NULL|TemplateManager */
16
	protected $templateManager;
17
18
	/**
19
	 * @param \SixtyEightPublishers\Application\Components\TemplateManager $templateManager
20
	 *
21
	 * @return void
22
	 */
23
	public function injectTemplateManager(TemplateManager $templateManager)
24
	{
25
		if (!$this instanceof Control) {
0 ignored issues
show
Bug introduced by
The class Nette\Application\UI\Control does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
26
			$class = Control::class;
27
			throw new MemberAccessException("Trait " . __TRAIT__ . " can be used only in objects that are descendants of {$class}.");
28
		}
29
30
		$this->templateManager = $templateManager;
31
	}
32
33
34
	/**
35
	 * @param string $name
36
	 *
37
	 * @return NULL|IComponent
38
	 */
39
	protected function createComponent($name)
40
	{
41
		$ucName = ucfirst($name);
42
		$method = 'createComponent' . $ucName;
43
		if ($ucName !== $name && method_exists($this, $method) && (new \ReflectionMethod($this, $method))->getName() === $method) {
44
			$component = $this->$method($name);
45
			if (!$component instanceof IComponent && !isset($this->components[$name])) {
0 ignored issues
show
Bug introduced by
The property components does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The class Nette\ComponentModel\IComponent does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
46
				$class = get_class($this);
47
				throw new UnexpectedValueException("Method $class::$method() did not return or create the desired component.");
48
			}
49
			
50
			$annotations = (new Method($this, $method))->getAnnotations();
51
			if (array_key_exists('template', $annotations)) {
52
				foreach ($annotations['template'] as $value) {
53
					$this->templateManager->monitor($this->getName(), (is_string($value)) ? "{$name}-{$value}" : $name);
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54
				}
55
			}
56
57
			return $component;
58
		}
59
	}
60
}
61