Skip to content

Commit

Permalink
Add typehints, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelJurasek committed Apr 9, 2019
1 parent d1f949c commit fafa3e3
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 65 deletions.
29 changes: 1 addition & 28 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
15 changes: 4 additions & 11 deletions src/Kdyby/Autowired/AutowireComponentFactories.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/**
* This file is part of the Kdyby (http://www.kdyby.org)
Expand Down Expand Up @@ -35,10 +35,7 @@ trait AutowireComponentFactories



/**
* @return Nette\DI\Container
*/
protected function getComponentFactoriesLocator()
protected function getComponentFactoriesLocator(): Nette\DI\Container
{
if ($this->autowireComponentFactoriesLocator === NULL) {
$this->injectComponentFactories($this->getPresenter()->getContext());
Expand All @@ -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.');
Expand Down Expand Up @@ -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);
Expand All @@ -124,8 +119,6 @@ private function findByTypeForFactory($type)


/**
* @param $name
* @return Nette\ComponentModel\IComponent
* @throws Nette\UnexpectedValueException
*/
protected function createComponent(string $name): ?IComponent
Expand Down
27 changes: 13 additions & 14 deletions src/Kdyby/Autowired/AutowireProperties.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/**
* This file is part of the Kdyby (http://www.kdyby.org)
Expand Down Expand Up @@ -38,7 +38,6 @@ trait AutowireProperties


/**
* @param \Nette\DI\Container $dic
* @throws MemberAccessException
* @throws MissingServiceException
* @throws InvalidStateException
Expand Down Expand Up @@ -93,7 +92,7 @@ public function injectProperties(Nette\DI\Container $dic)



private function validateProperty(Property $property, array $ignore)
private function validateProperty(Property $property, array $ignore): bool
{
if (in_array($property->getDeclaringClass()->getName(), $ignore, TRUE)) {
return FALSE;
Expand Down Expand Up @@ -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);
Expand All @@ -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 = [
Expand Down Expand Up @@ -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 */

Expand Down Expand Up @@ -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'] . ".");
}

Expand All @@ -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);
Expand Down
21 changes: 12 additions & 9 deletions src/Kdyby/Autowired/DI/AutowiredExtension.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/**
* This file is part of the Kdyby (http://www.kdyby.org)
Expand All @@ -22,23 +22,18 @@
class AutowiredExtension extends Nette\DI\CompilerExtension
{

public $defaults = [
'cacheStorage' => '@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']);
}


Expand All @@ -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
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Kdyby/Autowired/Diagnostics/Panel.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

/**
* This file is part of the Kdyby (http://www.kdyby.org)
Expand Down

0 comments on commit fafa3e3

Please sign in to comment.