GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#1)
by Lukáš
01:53
created

ProxyExtension::beforeCompile()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 8.8571
cc 5
eloc 9
nc 3
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Lookyman\Nette\Proxy\DI;
5
6
use Lookyman\Nette\Proxy\Console\GenerateProxiesCommand;
7
use Nette\DI\CompilerExtension;
8
use Nette\DI\Container;
9
use Nette\DI\Helpers;
10
use Nette\DI\Statement;
11
use Nette\PhpGenerator\ClassType;
12
use Nette\PhpGenerator\Method;
13
use Nette\Utils\Validators;
14
use ProxyManager\Configuration;
15
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
16
use ProxyManager\FileLocator\FileLocator;
17
use ProxyManager\GeneratorStrategy\FileWriterGeneratorStrategy;
18
19
class ProxyExtension extends CompilerExtension
20
{
21
	const TAG_LAZY = 'lookyman.lazy';
22
23
	/**
24
	 * @var array
25
	 */
26
	private $defaults = [
27
		'proxyDir' => '%appDir%/../temp/proxies',
28
		'default' => false,
29
	];
30
31
	public function loadConfiguration()
32
	{
33
		$builder = $this->getContainerBuilder();
34
		$config = $this->validateConfig($this->defaults);
35
		Validators::assertField($config, 'default', 'bool');
36
37
		// create proxy dir
38
		Validators::assertField($config, 'proxyDir', 'string');
39
		$config['proxyDir'] = Helpers::expand($config['proxyDir'], $builder->parameters);
40
		if (!@mkdir($config['proxyDir'], 0777, true) && !is_dir($config['proxyDir'])) {
41
			// @codeCoverageIgnoreStart
42
			throw new \RuntimeException(sprintf('Cannot create proxy directory %s', $config['proxyDir']));
43
			// @codeCoverageIgnoreEnd
44
		}
45
46
		// generator strategy
47
		$builder->addDefinition($this->prefix('generatorStrategy'))
48
			->setClass(FileWriterGeneratorStrategy::class, [new Statement(FileLocator::class, [$config['proxyDir']])])
49
			->setAutowired(false)
50
			->addTag(self::TAG_LAZY, false);
51
52
		// configuration
53
		$builder->addDefinition($this->prefix('configuration'))
54
			->setClass(Configuration::class)
55
			->addSetup('setProxiesTargetDir', [$config['proxyDir']])
56
			->addSetup('setGeneratorStrategy', [$this->prefix('@generatorStrategy')])
57
			->setAutowired(false)
58
			->addTag(self::TAG_LAZY, false);
59
60
		// proxy factory
61
		$builder->addDefinition($this->prefix('lazyLoadingValueHolderFactory'))
62
			->setClass(LazyLoadingValueHolderFactory::class, [$this->prefix('@configuration')])
63
			->setAutowired(false)
64
			->addTag(self::TAG_LAZY, false);
65
66
		// command
67
		/** @var \Kdyby\Console\DI\ConsoleExtension $extension */
68
		foreach ($this->compiler->getExtensions('Kdyby\Console\DI\ConsoleExtension') as $extension) {
69
			$builder->addDefinition($this->prefix('generateProxiesCommand'))
70
				->setClass(GenerateProxiesCommand::class)
71
				->addTag($extension::TAG_COMMAND);
72
			break;
73
		}
74
	}
75
76
	public function beforeCompile()
77
	{
78
		$builder = $this->getContainerBuilder();
79
		$config = $this->getConfig();
80
81
		// add service type as tag attribute
82
		foreach (array_keys($config['default'] ? $builder->getDefinitions() : $builder->findByTag(self::TAG_LAZY)) as $name) {
83
			$def = $builder->getDefinition($name);
84
			if ($def->getTag(self::TAG_LAZY) === false) {
85
				$def->setTags(array_diff_key($def->getTags(), [self::TAG_LAZY => null]));
86
				continue;
87
			}
88
			$def->addTag(self::TAG_LAZY, $def->getImplement() ?: $def->getClass());
0 ignored issues
show
Documentation introduced by
$def->getImplement() ?: $def->getClass() is of type string|null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
89
		}
90
	}
91
92
	/**
93
	 * @param ClassType $class
94
	 */
95
	public function afterCompile(ClassType $class)
96
	{
97
		foreach ($this->getContainerBuilder()->findByTag(self::TAG_LAZY) as $name => $type) {
98
			// modify original method body to return proxy instead
99
			$method = $class->getMethod(Container::getMethodName($name));
100
			$method->setBody(sprintf(
101
				"return \$this->getService('%s')->createProxy(\n\t%s::class,\n\tfunction (&\$wrappedObject, \$proxy, \$method, \$parameters, &\$initializer) {\n\t\t\$wrappedObject = (%s)();\n\t\t\$initializer = null;\n\t}\n);",
102
				$this->prefix('lazyLoadingValueHolderFactory'), $type, ltrim(preg_replace('#^#m', "\t\t", (new Method())->addBody($method->getBody())))
103
			));
104
		}
105
106
		// register proxy autoloader
107
		$init = $class->getMethod('initialize');
108
		$body = $init->getBody();
109
		$init->setBody("spl_autoload_register(\$this->getService(?)->getProxyAutoloader());\n", [$this->prefix('configuration')])->addBody($body);
110
	}
111
}
112