Issues (18)

src/DI/Config.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\Asset\DI;
6
7
use Nette;
8
9
final class Config
10
{
11
	use Nette\SmartObject;
12
13
	private const MAIN_PACKAGE_DEFAULT = [
14
		'base_path' => '',
15
		'base_urls' => [],
16
		'version' => NULL,
17
		'version_format' => '%s?%s',
18
		'version_strategy' => NULL,
19
		'json_manifest_path' => NULL,
20
		'packages' => [],
21
	];
22
23
	private const PACKAGE_DEFAULTS = [
24
		'base_path' => NULL,
25
		'base_urls' => [],
26
		'version' => NULL,
27
		'version_format' => NULL,
28
		'version_strategy' => NULL,
29
		'json_manifest_path' => NULL,
30
	];
31
32
	/**
33
	 * @return array
34
	 */
35
	public function getDefaults(): array
36
	{
37 1
		return self::MAIN_PACKAGE_DEFAULT;
38
	}
39
40
	/**
41
	 * @param \Nette\DI\CompilerExtension $extension
42
	 *
43
	 * @return array
44
	 * @throws \Nette\Utils\AssertionException
45
	 */
46 1
	public function getConfig(Nette\DI\CompilerExtension $extension): array
47
	{
48 1
		$config = $this->mergeConfig($extension, $this->getDefaults(), $extension->getConfig());
0 ignored issues
show
It seems like $extension->getConfig() can also be of type object; however, parameter $config of SixtyEightPublishers\Ass...I\Config::mergeConfig() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
		$config = $this->mergeConfig($extension, $this->getDefaults(), /** @scrutinizer ignore-type */ $extension->getConfig());
Loading history...
49
50 1
		$config = $this->validatePackage($config, TRUE);
51
52 1
		foreach ($config['packages'] as $name => $package) {
53 1
			Nette\Utils\Validators::assert($package, 'array');
54
55 1
			$config['packages'][(string) $name] = $this->validatePackage(
56 1
				$this->mergeConfig($extension, self::PACKAGE_DEFAULTS, $package),
57 1
				FALSE
58
			);
59
		}
60
61 1
		return $config;
62
	}
63
64
	/**
65
	 * @param \Nette\DI\CompilerExtension $extension
66
	 * @param array                       $defaults
67
	 * @param array                       $config
68
	 *
69
	 * @return array
70
	 */
71 1
	private function mergeConfig(Nette\DI\CompilerExtension $extension, array $defaults, array $config): array
72
	{
73
		/** @noinspection PhpInternalEntityUsedInspection */
74 1
		return $extension->validateConfig(
0 ignored issues
show
Deprecated Code introduced by
The function Nette\DI\CompilerExtension::validateConfig() has been deprecated: use getConfigSchema() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

74
		return /** @scrutinizer ignore-deprecated */ $extension->validateConfig(

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
75 1
			Nette\DI\Helpers::expand($defaults, $extension->getContainerBuilder()->parameters),
76 1
			$config
77
		);
78
	}
79
80
	/**
81
	 * @param array $package
82
	 * @param bool  $isDefault
83
	 *
84
	 * @return array
85
	 * @throws \Nette\Utils\AssertionException
86
	 */
87 1
	private function validatePackage(array $package, bool $isDefault): array
88
	{
89 1
		Nette\Utils\Validators::assertField($package, 'version_strategy', 'string|null|' . Nette\DI\Statement::class);
90 1
		Nette\Utils\Validators::assertField($package, 'version', 'string|null');
91 1
		Nette\Utils\Validators::assertField($package, 'version_format', TRUE === $isDefault ? 'string' : 'string|null');
92 1
		Nette\Utils\Validators::assertField($package, 'json_manifest_path', 'string|null');
93 1
		Nette\Utils\Validators::assertField($package, 'base_path', 'string|null|' . Nette\DI\Statement::class);
94 1
		Nette\Utils\Validators::assertField($package, 'base_urls', 'string|string[]|' . Nette\DI\Statement::class . '[]');
95
96 1
		if (!is_array($package['base_urls'])) {
97 1
			$package['base_urls'] = [ $package['base_urls'] ];
98
		}
99
100 1
		if (isset($package['version_strategy'], $package['version'])) {
101
			throw new \LogicException('You cannot use both "version_strategy" and "version" at the same time under "assets" packages.');
102
		}
103
104 1
		if (isset($package['version_strategy'], $package['json_manifest_path'])) {
105
			throw new \LogicException('You cannot use both "version_strategy" and "json_manifest_path" at the same time under "assets" packages.');
106
		}
107
108 1
		if (isset($package['version'], $package['json_manifest_path'])) {
109
			throw new \LogicException('You cannot use both "version" and "json_manifest_path" at the same time under "assets" packages.');
110
		}
111
112 1
		return $package;
113
	}
114
}
115