From bfd940cdfab70a957d6310ab06319d6c78b851d4 Mon Sep 17 00:00:00 2001 From: Robin de Graaf Date: Thu, 11 Mar 2021 13:17:09 +0000 Subject: [PATCH] Add psalm --- .github/workflows/tests.yml | 3 +++ .gitignore | 3 ++- CHANGELOG.md | 4 ++++ Makefile | 8 ++++++++ composer.json | 3 ++- psalm.xml | 19 +++++++++++++++++++ src/Container.php | 12 +++++++----- 7 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 psalm.xml diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5d3a185..d181c60 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -32,3 +32,6 @@ jobs: - name: Run test suite run: vendor/bin/phpunit tests + + - name: Run static analysis + run: vendor/bin/psalm diff --git a/.gitignore b/.gitignore index 41f9a33..9c59449 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ +.DS_Store .idea composer.lock vendor -coverage \ No newline at end of file +coverage diff --git a/CHANGELOG.md b/CHANGELOG.md index c817688..dbc0b69 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Parable PHP DI +## 0.3.2 + +- Add static analysis using psalm. + ## 0.3.1 _Fixes_ diff --git a/Makefile b/Makefile index e0c55fe..289792f 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,10 @@ dependencies: --no-plugins \ --no-scripts +psalm: + vendor/bin/psalm --clear-cache + vendor/bin/psalm + tests: dependencies vendor/bin/phpunit --verbose tests @@ -13,3 +17,7 @@ coverage: dependencies tests-clean: vendor/bin/phpunit --verbose tests + +coverage-clean: + rm -rf ./coverage + XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html ./coverage tests diff --git a/composer.json b/composer.json index 0fde69a..7386288 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "php": ">=8.0" }, "require-dev": { - "phpunit/phpunit": "^9.0" + "phpunit/phpunit": "^9.0", + "vimeo/psalm": "^4.6" }, "autoload": { "psr-4": { diff --git a/psalm.xml b/psalm.xml new file mode 100644 index 0000000..8679d7a --- /dev/null +++ b/psalm.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + diff --git a/src/Container.php b/src/Container.php index 26e8904..5c82ca6 100644 --- a/src/Container.php +++ b/src/Container.php @@ -15,7 +15,7 @@ class Container protected array $instances = []; - /** @var string[][] */ + /** @var bool[][] */ protected array $relationships = []; /** @var string[] */ @@ -98,7 +98,7 @@ protected function createInstance(string $name, int $useStoredDependencies): obj try { $dependencies = $this->getDependenciesFor($name, $useStoredDependencies); } catch (Throwable $t) { - throw new ContainerException($t->getMessage(), $t->getCode(), $t); + throw new ContainerException($t->getMessage(), (int)$t->getCode(), $t); } return new $name(...$dependencies); @@ -146,14 +146,14 @@ public function getDependenciesFor( 'Could not create instance for class `%s`.', $name ), - $t->getCode(), + (int)$t->getCode(), $t ); } $constructor = $reflection->getConstructor(); - if (!$constructor) { + if ($constructor === null) { return []; } @@ -172,7 +172,9 @@ public function getDependenciesFor( $class = null; if ($parameter->getType() instanceof ReflectionNamedType && $builtIn === false) { - $class = new ReflectionClass($parameter->getType()->getName()); + /** @var ReflectionNamedType $type */ + $type = $parameter->getType(); + $class = new ReflectionClass($type->getName()); } if ($class === null) {