From fafa3e3a9858dea4f6f73d7e24d4bc761eee3fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Jura=CC=81sek?= Date: Tue, 9 Apr 2019 21:51:45 +0200 Subject: [PATCH] Add typehints, cleanup --- .travis.yml | 29 +------------------ composer.json | 4 +-- .../Autowired/AutowireComponentFactories.php | 15 +++------- src/Kdyby/Autowired/AutowireProperties.php | 27 +++++++++-------- src/Kdyby/Autowired/DI/AutowiredExtension.php | 21 ++++++++------ src/Kdyby/Autowired/Diagnostics/Panel.php | 2 +- 6 files changed, 33 insertions(+), 65 deletions(-) diff --git a/.travis.yml b/.travis.yml index 85a6cfb..1bcc4cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,36 +7,9 @@ cache: - $HOME/.composer/cache php: - - 5.4 - - 5.5 - - 5.6 - - 7.0 - 7.1 - 7.2 - -env: - matrix: - - NETTE=nette-2.4-dev - - NETTE=nette-2.4 - -matrix: - include: - - php: 5.6 - env: NETTE=nette-2.4 COMPOSER_EXTRA_ARGS="--prefer-lowest --prefer-stable" - - php: 7.0 - env: NETTE=nette-2.4 COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg" - exclude: - - php: 5.4 - env: NETTE=nette-2.4-dev - - php: 5.4 - env: NETTE=nette-2.4 - - php: 5.5 - env: NETTE=nette-2.4-dev - - php: 5.5 - env: NETTE=nette-2.4 - allow_failures: - - php: 7.0 - env: NETTE=nette-2.4 COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg" + - 7.3 before_install: - travis_retry composer self-update diff --git a/composer.json b/composer.json index 856995c..5e5ae43 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,8 @@ }, "require": { "php": "^7.1", - "nette/di": "^3.0-RC2", - "nette/application": "3.0-RC3", + "nette/di": "^3.0", + "nette/application": "3.0", "nette/reflection": "^2.4", "tracy/tracy": "^2.6" }, diff --git a/src/Kdyby/Autowired/AutowireComponentFactories.php b/src/Kdyby/Autowired/AutowireComponentFactories.php index e0f79d7..feee7c3 100644 --- a/src/Kdyby/Autowired/AutowireComponentFactories.php +++ b/src/Kdyby/Autowired/AutowireComponentFactories.php @@ -1,4 +1,4 @@ -autowireComponentFactoriesLocator === NULL) { $this->injectComponentFactories($this->getPresenter()->getContext()); @@ -50,11 +47,10 @@ protected function getComponentFactoriesLocator() /** - * @param \Nette\DI\Container $dic * @throws MemberAccessException * @internal */ - public function injectComponentFactories(Nette\DI\Container $dic) + public function injectComponentFactories(Nette\DI\Container $dic): void { if (!$this instanceof Nette\Application\UI\PresenterComponent && !$this instanceof Nette\Application\UI\Component) { throw new MemberAccessException('Trait ' . __TRAIT__ . ' can be used only in descendants of PresenterComponent.'); @@ -103,10 +99,9 @@ public function injectComponentFactories(Nette\DI\Container $dic) /** - * @param string $type * @return string|bool */ - private function findByTypeForFactory($type) + private function findByTypeForFactory(string $type) { if (method_exists($this->autowireComponentFactoriesLocator, 'findByType')) { $found = $this->autowireComponentFactoriesLocator->findByType($type); @@ -124,8 +119,6 @@ private function findByTypeForFactory($type) /** - * @param $name - * @return Nette\ComponentModel\IComponent * @throws Nette\UnexpectedValueException */ protected function createComponent(string $name): ?IComponent diff --git a/src/Kdyby/Autowired/AutowireProperties.php b/src/Kdyby/Autowired/AutowireProperties.php index a6f8bdb..62b2d07 100644 --- a/src/Kdyby/Autowired/AutowireProperties.php +++ b/src/Kdyby/Autowired/AutowireProperties.php @@ -1,4 +1,4 @@ -getDeclaringClass()->getName(), $ignore, TRUE)) { return FALSE; @@ -121,10 +120,9 @@ private function validateProperty(Property $property, array $ignore) /** - * @param string $type * @return string|bool */ - private function findByTypeForProperty($type) + private function findByTypeForProperty(string $type) { if (method_exists($this->autowirePropertiesLocator, 'findByType')) { $found = $this->autowirePropertiesLocator->findByType($type); @@ -142,11 +140,10 @@ private function findByTypeForProperty($type) /** - * @param Property $prop * @throws MissingServiceException * @throws UnexpectedValueException */ - private function resolveProperty(Property $prop) + private function resolveProperty(Property $prop): void { $type = $this->resolveAnnotationClass($prop, $prop->getAnnotation('var'), 'var'); $metadata = [ @@ -182,7 +179,7 @@ private function resolveProperty(Property $prop) - private function resolveAnnotationClass(\Reflector $prop, $annotationValue, $annotationName) + private function resolveAnnotationClass(\Reflector $prop, string $annotationValue, string $annotationName): string { /** @var Property|Method $prop */ @@ -217,20 +214,23 @@ private function resolveAnnotationClass(\Reflector $prop, $annotationValue, $ann /** - * @param string $name * @param mixed $value * @throws MemberAccessException * @return mixed */ - public function __set($name, $value) + public function __set(string $name, $value) { if (!isset($this->autowireProperties[$name])) { return parent::__set($name, $value); - } elseif ($this->autowireProperties[$name]['value']) { + } + + if ($this->autowireProperties[$name]['value']) { throw new MemberAccessException("Property \$$name has already been set."); - } elseif (!$value instanceof $this->autowireProperties[$name]['type']) { + } + + if (!$value instanceof $this->autowireProperties[$name]['type']) { throw new MemberAccessException("Property \$$name must be an instance of " . $this->autowireProperties[$name]['type'] . "."); } @@ -240,11 +240,10 @@ public function __set($name, $value) /** - * @param $name * @throws MemberAccessException * @return mixed */ - public function &__get($name) + public function &__get(string $name) { if (!isset($this->autowireProperties[$name])) { return parent::__get($name); diff --git a/src/Kdyby/Autowired/DI/AutowiredExtension.php b/src/Kdyby/Autowired/DI/AutowiredExtension.php index 4be563a..69618c9 100644 --- a/src/Kdyby/Autowired/DI/AutowiredExtension.php +++ b/src/Kdyby/Autowired/DI/AutowiredExtension.php @@ -1,4 +1,4 @@ - '@Nette\Caching\IStorage', - ]; - - public function loadConfiguration() { $builder = $this->getContainerBuilder(); - $config = Nette\DI\Config\Helpers::merge($this->getConfig(), $this->defaults); + $config = (array) $this->getConfig(); $storage = $builder->addDefinition($this->prefix('cacheStorage')) ->setType('Nette\Caching\IStorage') ->setAutowired(FALSE); - $storage->factory = is_string($config['cacheStorage']) + $storage->setFactory(is_string($config['cacheStorage']) ? new Nette\DI\Definitions\Statement($config['cacheStorage']) - : $config['cacheStorage']; + : $config['cacheStorage']); } @@ -51,6 +46,14 @@ public function afterCompile(Code\ClassType $class) + public function getConfigSchema(): Nette\Schema\Schema + { + return Nette\Schema\Expect::structure([ + 'cacheStorage' => Nette\Schema\Expect::string('@Nette\Caching\IStorage'), + ]); + } + + /** * @param \Nette\Configurator $configurator */ diff --git a/src/Kdyby/Autowired/Diagnostics/Panel.php b/src/Kdyby/Autowired/Diagnostics/Panel.php index d30ec7e..ed3e40a 100644 --- a/src/Kdyby/Autowired/Diagnostics/Panel.php +++ b/src/Kdyby/Autowired/Diagnostics/Panel.php @@ -1,4 +1,4 @@ -