Skip to content

Commit

Permalink
added type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 21, 2018
1 parent 9d3a379 commit 8eb9072
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/DI/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function compile(): string


/** @internal */
public function processParameters()
public function processParameters(): void
{
$params = isset($this->config['parameters']) ? $this->config['parameters'] : [];
foreach ($this->dynamicParams as $key) {
Expand All @@ -185,7 +185,7 @@ public function processParameters()


/** @internal */
public function processExtensions()
public function processExtensions(): void
{
$this->config = Helpers::expand(array_diff_key($this->config, self::$reserved), $this->builder->parameters)
+ array_intersect_key($this->config, self::$reserved);
Expand Down Expand Up @@ -222,7 +222,7 @@ public function processExtensions()


/** @internal */
public function processServices()
public function processServices(): void
{
foreach ($this->serviceConfigs as $config) {
self::loadDefinitions($this->builder, $config);
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Config/Adapters/IniAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function dump(array $data): string
/**
* Recursive builds INI list.
*/
private static function build($input, &$output, $prefix): void
private static function build(array $input, &$output, string $prefix): void
{
foreach ($input as $key => $val) {
$key = str_replace(self::KEY_SEPARATOR, self::ESCAPED_KEY_SEPARATOR, $key);
Expand Down
17 changes: 8 additions & 9 deletions src/DI/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function hasDefinition(string $name): bool
}


public function addAlias(string $alias, string $service)
public function addAlias(string $alias, string $service): void
{
if (!$alias) { // builder is not ready for falsy names such as '0'
throw new Nette\InvalidArgumentException(sprintf('Alias name must be a non-empty string, %s given.', gettype($alias)));
Expand Down Expand Up @@ -352,7 +352,7 @@ public function prepareClassList(): void
}


private function resolveImplement(ServiceDefinition $def, $name)
private function resolveImplement(ServiceDefinition $def, $name): void
{
$interface = $def->getImplement();
if (!interface_exists($interface)) {
Expand Down Expand Up @@ -431,8 +431,7 @@ private function resolveImplement(ServiceDefinition $def, $name)
}


/** @return string|null */
private function resolveServiceType($name, array $recursive = [])
private function resolveServiceType($name, array $recursive = []): ?string
{
if (isset($recursive[$name])) {
throw new ServiceCreationException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($recursive))));
Expand All @@ -458,8 +457,7 @@ private function resolveServiceType($name, array $recursive = [])
}


/** @return string|null */
private function resolveEntityType($entity, array $recursive = [])
private function resolveEntityType($entity, array $recursive = []): ?string
{
$entity = $this->normalizeEntity($entity instanceof Statement ? $entity->getEntity() : $entity);
$serviceName = current(array_slice(array_keys($recursive), -1));
Expand All @@ -468,7 +466,7 @@ private function resolveEntityType($entity, array $recursive = [])
if (($service = $this->getServiceName($entity[0])) || $entity[0] instanceof Statement) {
$entity[0] = $this->resolveEntityType($entity[0], $recursive);
if (!$entity[0]) {
return;
return null;
} elseif (isset($this->definitions[$service]) && $this->definitions[$service]->getImplement()) { // @Implement::create
return $entity[1] === 'create' ? $this->resolveServiceType($service, $recursive) : null;
}
Expand Down Expand Up @@ -507,6 +505,7 @@ private function resolveEntityType($entity, array $recursive = [])
}
return $entity;
}
return null;
}


Expand Down Expand Up @@ -620,7 +619,7 @@ public function completeStatement(Statement $statement): Statement
}
}

array_walk_recursive($arguments, function (&$val) {
array_walk_recursive($arguments, function (&$val): void {
if ($val instanceof Statement) {
$val = $this->completeStatement($val);

Expand Down Expand Up @@ -724,7 +723,7 @@ public function getServiceName($arg): ?string
* Creates a list of arguments using autowiring.
* @internal
*/
public function autowireArguments($class, $method, array $arguments): array
public function autowireArguments(string $class, string $method, array $arguments): array
{
$rc = new ReflectionClass($class);
if (!$rc->hasMethod($method)) {
Expand Down
6 changes: 3 additions & 3 deletions src/DI/DependencyChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ public static function isExpired(int $version, array $files, array &$phpFiles, a
}


private static function calculateHash(array $classes, array $functions)
private static function calculateHash(array $classes, array $functions): ?string
{
$hash = [];
foreach ($classes as $name) {
try {
$class = new ReflectionClass($name);
} catch (\ReflectionException $e) {
return;
return null;
}
$hash[] = [
$name,
Expand Down Expand Up @@ -134,7 +134,7 @@ class_uses($name),
try {
$method = strpos($name, '::') ? new ReflectionMethod($name) : new \ReflectionFunction($name);
} catch (\ReflectionException $e) {
return;
return null;
}
$class = $method instanceof ReflectionMethod ? $method->getDeclaringClass() : null;
if ($class && isset($flip[$class->getName()])) {
Expand Down
6 changes: 3 additions & 3 deletions src/DI/Extensions/DecoratorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function beforeCompile()
}


public function addSetups($type, array $setups)
public function addSetups(string $type, array $setups): void
{
foreach ($this->findByType($type) as $def) {
foreach ($setups as $setup) {
Expand All @@ -51,7 +51,7 @@ public function addSetups($type, array $setups)
}


public function addTags($type, array $tags)
public function addTags(string $type, array $tags): void
{
$tags = Nette\Utils\Arrays::normalize($tags, true);
foreach ($this->findByType($type) as $def) {
Expand All @@ -60,7 +60,7 @@ public function addTags($type, array $tags)
}


private function findByType($type)
private function findByType(string $type): array
{
return array_filter($this->getContainerBuilder()->getDefinitions(), function ($def) use ($type) {
return is_a($def->getType(), $type, true) || is_a($def->getImplement(), $type, true);
Expand Down
10 changes: 5 additions & 5 deletions src/DI/Extensions/InjectExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function beforeCompile()
}


private function updateDefinition(DI\ServiceDefinition $def)
private function updateDefinition(DI\ServiceDefinition $def): void
{
$class = $def->getType();
$setups = $def->getSetup();
Expand Down Expand Up @@ -70,7 +70,7 @@ private function updateDefinition(DI\ServiceDefinition $def)
* Generates list of inject methods.
* @internal
*/
public static function getInjectMethods($class): array
public static function getInjectMethods(string $class): array
{
$res = [];
foreach (get_class_methods($class) as $name) {
Expand All @@ -91,7 +91,7 @@ public static function getInjectMethods($class): array
* Generates list of properties with annotation @inject.
* @internal
*/
public static function getInjectProperties($class): array
public static function getInjectProperties(string $class): array
{
$res = [];
foreach (get_class_vars($class) as $name => $foo) {
Expand All @@ -117,7 +117,7 @@ public static function callInjects(DI\Container $container, $service): void
throw new Nette\InvalidArgumentException(sprintf('Service must be object, %s given.', gettype($service)));
}

foreach (self::getInjectMethods($service) as $method) {
foreach (self::getInjectMethods(get_class($service)) as $method) {
$container->callMethod([$service, $method]);
}

Expand All @@ -129,7 +129,7 @@ public static function callInjects(DI\Container $container, $service): void


/** @internal */
private static function checkType($class, $name, $type, $container = null)
private static function checkType($class, string $name, ?string $type, $container = null): void
{
$propName = Reflection::toString(new \ReflectionProperty($class, $name));
if (!$type) {
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static function prefixServiceName($config, string $namespace)
/**
* Returns an annotation value.
*/
public static function parseAnnotation(\Reflector $ref, $name): ?string
public static function parseAnnotation(\Reflector $ref, string $name): ?string
{
if (!Reflection::areCommentsAvailable()) {
throw new Nette\InvalidStateException('You have to enable phpDoc comments in opcode cache.');
Expand Down
11 changes: 5 additions & 6 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@mkdir(TEMP_DIR);


function test(\Closure $function)
function test(\Closure $function): void
{
$function();
}
Expand All @@ -32,13 +32,13 @@ class Notes
public static $notes = [];


public static function add($message)
public static function add($message): void
{
self::$notes[] = $message;
}


public static function fetch()
public static function fetch(): array
{
$res = self::$notes;
self::$notes = [];
Expand All @@ -47,8 +47,7 @@ public static function fetch()
}


/** @return Nette\DI\Container */
function createContainer($source, $config = null, $params = [])
function createContainer($source, $config = null, $params = []): ?Nette\DI\Container
{
$class = 'Container' . md5((string) lcg_value());
if ($source instanceof Nette\DI\ContainerBuilder) {
Expand All @@ -63,7 +62,7 @@ function createContainer($source, $config = null, $params = [])
->setClassName($class)
->compile();
} else {
return;
return null;
}

file_put_contents(TEMP_DIR . '/code.php', "<?php\n\n$code");
Expand Down

0 comments on commit 8eb9072

Please sign in to comment.