Completed
Push — master ( 70f26a...90e34c )
by Tomáš
05:03
created

src/DI/ModularLatteFiltersExtension.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of Zenify.
7
 * Copyright (c) 2015 Tomas Votruba (http://tomasvotruba.cz)
8
 */
9
10
namespace Zenify\ModularLatteFilters\DI;
11
12
use Latte\Engine;
13
use Nette\Bridges\ApplicationLatte\ILatteFactory;
14
use Nette\DI\CompilerExtension;
15
use Nette\DI\ServiceDefinition;
16
use Zenify\ModularLatteFilters\Contract\DI\LatteFiltersProviderInterface;
17
use Zenify\ModularLatteFilters\Exception\DI\MissingLatteDefinitionException;
18
19
20
final class ModularLatteFiltersExtension extends CompilerExtension
21
{
22
23
	/**
24
	 * @var string
25
	 */
26
	const APPLICATION_LATTE_FACTORY_INTERFACE = ILatteFactory::class;
27
28
29 3
	public function beforeCompile()
30
	{
31 3
		$containerBuilder = $this->getContainerBuilder();
32 3
		$containerBuilder->prepareClassList();
33
34 3
		$latteDefinition = $this->getLatteDefinition();
35 2
		foreach ($containerBuilder->findByType(LatteFiltersProviderInterface::class) as $latteFilterProviderDefinition) {
36 2
			$latteDefinition->addSetup(
37
				'foreach (?->getFilters() as $name => $callback) {
38
					?->addFilter($name, $callback);
39 2
				}',
40 2
				['@' . $latteFilterProviderDefinition->getClass(), '@self']
41
			);
42
		}
43 2
	}
44
45
46 3
	private function getLatteDefinition() : ServiceDefinition
47
	{
48 3
		$containerBuilder = $this->getContainerBuilder();
49
50 3
		if ($containerBuilder->getByType(Engine::class)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $containerBuilder->getBy...e(\Latte\Engine::class) of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
51 1
			$serviceName = $containerBuilder->getByType(Engine::class);
52
53 2
		} elseif ($containerBuilder->getByType(self::APPLICATION_LATTE_FACTORY_INTERFACE)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $containerBuilder->getBy...ATTE_FACTORY_INTERFACE) of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
54 1
			$serviceName = $containerBuilder->getByType(self::APPLICATION_LATTE_FACTORY_INTERFACE);
55
56
		} else {
57 1
			throw new MissingLatteDefinitionException(
58
				sprintf(
59 1
					'No services providing Latte\Engine was found. Register service either of %s or %s type.',
60 1
					Engine::class,
61 1
					ILatteFactory::class
62
				)
63
			);
64
		}
65
66 2
		return $containerBuilder->getDefinition($serviceName);
67
	}
68
69
}
70