Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #46 from tux-rampage/feature/phpstan
Browse files Browse the repository at this point in the history
Add phpstan and ensure level 7
  • Loading branch information
Ocramius committed Nov 21, 2018
2 parents 55e4472 + 401aa63 commit d82ccc9
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 75 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ matrix:
- DEPS=locked
- TEST_COVERAGE=true
- CHECK_CS=true
- STATIC_ANALYSIS=true
- php: 7.1
env:
- DEPS=latest
Expand Down Expand Up @@ -53,9 +54,9 @@ install:
- stty cols 120 && composer show

script:
- if [[ $STATIC_ANALYSIS == 'true' ]]; then composer analyse ; fi
- if [[ $TEST_COVERAGE == 'true' ]]; then composer test-coverage ; else composer test ; fi
- if [[ $CHECK_CS == 'true' ]]; then composer cs-check ; fi

after_script:
- if [[ $TEST_COVERAGE == 'true' ]]; then travis_retry php vendor/bin/php-coveralls -v ; fi

3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"require": {
"php": "^7.1",
"phpstan/phpstan-shim": "^0.10.5",
"psr/container": "^1.0",
"psr/log": "^1.0",
"zendframework/zend-stdlib": "^2.7 || ^3.0"
Expand Down Expand Up @@ -61,10 +62,12 @@
"scripts": {
"check": [
"@cs-check",
"@analyse",
"@test"
],
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"analyse": "phpstan analyse --no-progress -c phpstan.neon",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
}
Expand Down
46 changes: 45 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
level: 7
paths:
- src/
26 changes: 19 additions & 7 deletions src/CodeGenerator/AbstractInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
use Zend\Di\DefaultContainer;
use Zend\Di\InjectorInterface;

use function is_string;

/**
* Abstract class for code generated dependency injectors
*/
abstract class AbstractInjector implements InjectorInterface
{
/**
* @var string|FactoryInterface[]
* @var string[]|FactoryInterface[]
*/
protected $factories = [];

/**
* @var FactoryInterface[]
*/
private $factoryInstances = [];

/**
* @var ContainerInterface
*/
Expand All @@ -49,14 +52,23 @@ public function __construct(InjectorInterface $injector, ContainerInterface $con
*/
abstract protected function loadFactoryList() : void;

private function setFactory(string $type, FactoryInterface $factory) : void
{
$this->factoryInstances[$type] = $factory;
}

private function getFactory($type) : FactoryInterface
{
if (is_string($this->factories[$type])) {
$factory = $this->factories[$type];
$this->factories[$type] = new $factory();
if (isset($this->factoryInstances[$type])) {
return $this->factoryInstances[$type];
}

return $this->factories[$type];
$factoryClass = $this->factories[$type];
$factory = ($factoryClass instanceof FactoryInterface) ? $factoryClass : new $factoryClass();

$this->setFactory($type, $factory);

return $factory;
}

public function canCreate(string $name) : bool
Expand Down
8 changes: 7 additions & 1 deletion src/CodeGenerator/AutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use function array_map;
use function file_get_contents;
use function implode;
use function is_string;
use function sprintf;
use function str_repeat;
use function strtr;
use function var_export;
Expand Down Expand Up @@ -48,10 +50,14 @@ private function writeFile(string $filename, string $code) : void

private function buildFromTemplate(string $templateFile, string $outputFile, array $replacements) : void
{
$template = file_get_contents($templateFile);

assert(is_string($template));

$this->writeFile(
sprintf('%s/%s', $this->outputDirectory, $outputFile),
strtr(
file_get_contents($templateFile),
$template,
$replacements
)
);
Expand Down
11 changes: 8 additions & 3 deletions src/CodeGenerator/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Zend\Di\Resolver\TypeInjection;

use function file_get_contents;
use function is_string;
use function strrpos;
use function strtr;
use function substr;
Expand Down Expand Up @@ -103,7 +104,7 @@ private function splitFullyQualifiedClassName(string $class) : array
private function getClassName(string $type) : string
{
if ($this->config->isAlias($type)) {
return $this->config->getClassForAlias($type);
return $this->config->getClassForAlias($type) ?? $type;
}

return $type;
Expand Down Expand Up @@ -190,8 +191,12 @@ public function generate(string $class): string

$filename = $this->buildFileName($class);
$filepath = $this->outputDirectory . '/' . $filename;
$code = strtr(
file_get_contents(self::TEMPLATE_FILE),
$template = file_get_contents(self::TEMPLATE_FILE);

assert(is_string($template));

$code = strtr(
$template,
[
'%class%' => $absoluteClassName,
'%namespace%' => $namespace ? "namespace $namespace;\n" : '',
Expand Down
6 changes: 5 additions & 1 deletion src/CodeGenerator/InjectorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function array_map;
use function file_get_contents;
use function implode;
use function is_string;
use function str_repeat;
use function strtr;
use function var_export;
Expand Down Expand Up @@ -101,6 +102,9 @@ public function __construct(
private function buildFromTemplate(string $templateFile, string $outputFile, array $replacements) : void
{
$template = file_get_contents($templateFile);

assert(is_string($template));

$code = strtr($template, $replacements);
$file = new SplFileObject($outputFile, 'w');

Expand Down Expand Up @@ -193,7 +197,7 @@ public function generate($classes = []) : void
$factories = [];

foreach ($classes as $class) {
$this->generateTypeFactory((string)$class, $factories);
$this->generateTypeFactory($class, $factories);
}

foreach ($this->config->getConfiguredTypeNames() as $type) {
Expand Down
33 changes: 16 additions & 17 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use ArrayAccess;

use function is_array;

/**
* Provides a DI configuration from an array.
*
Expand Down Expand Up @@ -95,30 +97,18 @@ class Config implements ConfigInterface
*/
public function __construct($options = [])
{
if (! is_array($options) && ! ($options instanceof ArrayAccess)) {
throw new Exception\InvalidArgumentException(
'Config data must be of type array or ArrayAccess'
);
}

$this->ensureArrayOrArrayAccess($options);
$this->preferences = $this->getDataFromArray($options, 'preferences')?: [];
$this->types = $this->getDataFromArray($options, 'types')?: [];
}

/**
* @param array $data
* @param string $key
* @return array|ArrayAccess|null
* @param array|ArrayAccess $data
*/
private function getDataFromArray($data, $key)
private function getDataFromArray($data, string $key) : array
{
if (! isset($data[$key])
|| (! is_array($data[$key]) && ! ($data[$key] instanceof ArrayAccess))
) {
return null;
}

return $data[$key];
$result = $data[$key] ?? [];
return is_array($result) ? $result : [];
}

/**
Expand Down Expand Up @@ -241,4 +231,13 @@ public function setAlias(string $name, string $class) : self
$this->types[$name]['typeOf'] = $class;
return $this;
}

private function ensureArrayOrArrayAccess($options) : void
{
if (! is_array($options) && ! $options instanceof ArrayAccess) {
throw new Exception\InvalidArgumentException(
'Config data must be of type array or ArrayAccess'
);
}
}
}
3 changes: 0 additions & 3 deletions src/DefaultContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ class DefaultContainer implements ContainerInterface
*/
protected $services = [];

/**
* @param InjectorInterface $di
*/
public function __construct(InjectorInterface $injector)
{
$this->injector = $injector;
Expand Down
3 changes: 2 additions & 1 deletion src/Definition/Reflection/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public function isRequired() : bool
public function isBuiltin() : bool
{
if ($this->reflection->hasType()) {
return $this->reflection->getType()->isBuiltin();
$type = $this->reflection->getType();
return $type !== null ? $type->isBuiltin() : false;
}

return false;
Expand Down
7 changes: 4 additions & 3 deletions src/Definition/RuntimeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Zend\Di\Definition;

use Zend\Di\Definition\Reflection\ClassDefinition;
use Zend\Di\Exception;

/**
Expand All @@ -15,17 +16,17 @@
class RuntimeDefinition implements DefinitionInterface
{
/**
* @var Reflection\ClassDefinition[string]
* @var ClassDefinition[]
*/
private $definition = [];

/**
* @var bool[string]
* @var bool[]
*/
private $explicitClasses = null;

/**
* @param string[]|null $explicitClasses
* @param null|string[] $explicitClasses
*/
public function __construct(array $explicitClasses = null)
{
Expand Down
10 changes: 3 additions & 7 deletions src/Exception/ClassNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
namespace Zend\Di\Exception;

use DomainException;
use Throwable;

class ClassNotFoundException extends DomainException implements ExceptionInterface
{
/**
* @param string $classname
* @param int $code
* @param \Throwable|null $previous
*/
public function __construct($classname, $code = null, $previous = null)
public function __construct(string $classname, int $code = null, Throwable $previous = null)
{
parent::__construct("The class '$classname' does not exist.", $code, $previous);
parent::__construct("The class '$classname' does not exist.", $code ?? 0, $previous);
}
}
4 changes: 3 additions & 1 deletion src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Zend\Di\Exception;

interface ExceptionInterface
use Throwable;

interface ExceptionInterface extends Throwable
{
}
Loading

0 comments on commit d82ccc9

Please sign in to comment.