Skip to content

Commit

Permalink
Fix psalm errors (wip)
Browse files Browse the repository at this point in the history
Signed-off-by: tux-rampage <tuxrampage@gmail.com>
  • Loading branch information
tux-rampage committed Nov 4, 2020
1 parent c2d04a2 commit 870688a
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 22 deletions.
5 changes: 5 additions & 0 deletions psalm.xml
Expand Up @@ -36,6 +36,11 @@
</InternalMethod>
-->
</issueHandlers>

<stubs>
<file name="./stubs/psr-container.phpstub" />
</stubs>

<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
Expand Down
9 changes: 8 additions & 1 deletion src/CodeGenerator/AutoloadGenerator.php
Expand Up @@ -55,6 +55,7 @@ private function buildFromTemplate(string $templateFile, string $outputFile, arr
$template = file_get_contents($templateFile);

assert(is_string($template));
assert(is_string($this->outputDirectory));

$this->writeFile(
sprintf('%s/%s', $this->outputDirectory, $outputFile),
Expand All @@ -65,6 +66,9 @@ private function buildFromTemplate(string $templateFile, string $outputFile, arr
);
}

/**
* @param array<string, string> $classmap
*/
private function generateClassmapCode(array &$classmap): string
{
$lines = array_map(
Expand All @@ -83,6 +87,9 @@ function (string $class, string $file): string {
return implode($indentation, $lines);
}

/**
* @param array<string, string> $classmap
*/
private function generateAutoloaderClass(array &$classmap): void
{
$this->buildFromTemplate(self::CLASS_TEMPLATE, 'Autoloader.php', [
Expand All @@ -99,7 +106,7 @@ private function generateAutoloadFile(): void
}

/**
* @param string[] $classmap
* @param array<string, string> $classmap
*/
public function generate(array &$classmap): void
{
Expand Down
7 changes: 6 additions & 1 deletion src/CodeGenerator/FactoryGenerator.php
Expand Up @@ -61,7 +61,7 @@ class FactoryGenerator
/** @var ConfigInterface */
private $config;

/** @var array */
/** @var array<string, string> */
private $classmap = [];

public function __construct(
Expand Down Expand Up @@ -189,6 +189,8 @@ public function generate(string $class): string
$factoryClassName = $this->namespace . '\\' . $this->buildClassName($class);
[$namespace, $unqualifiedFactoryClassName] = $this->splitFullyQualifiedClassName($factoryClassName);

assert(is_string($this->outputDirectory));

$filename = $this->buildFileName($class);
$filepath = $this->outputDirectory . '/' . $filename;
$template = file_get_contents(self::TEMPLATE_FILE);
Expand Down Expand Up @@ -216,6 +218,9 @@ public function generate(string $class): string
return $factoryClassName;
}

/**
* @return array<string, string>
*/
public function getClassmap(): array
{
return $this->classmap;
Expand Down
3 changes: 3 additions & 0 deletions src/CodeGenerator/GeneratorTrait.php
Expand Up @@ -14,6 +14,7 @@
use Laminas\Di\Exception\LogicException;

use function is_dir;
use function is_string;
use function mkdir;
use function sprintf;

Expand All @@ -38,6 +39,8 @@ trait GeneratorTrait
*/
protected function ensureDirectory(string $dir)
{
assert(is_string($this->outputDirectory));

if (! is_dir($dir) && ! mkdir($dir, $this->mode, true)) {
throw new GenerateCodeException(sprintf(
'Could not create output directory: %s',
Expand Down
16 changes: 13 additions & 3 deletions src/CodeGenerator/InjectorGenerator.php
Expand Up @@ -50,7 +50,7 @@ class InjectorGenerator
/**
* @deprecated
*
* @var DefinitionInterface
* @var DefinitionInterface|null
*/
protected $definition;

Expand Down Expand Up @@ -104,6 +104,8 @@ private function buildFromTemplate(string $templateFile, string $outputFile, arr

private function generateInjector(): void
{
assert(is_string($this->outputDirectory));

$this->buildFromTemplate(
self::INJECTOR_TEMPLATE,
sprintf('%s/GeneratedInjector.php', $this->outputDirectory),
Expand All @@ -113,6 +115,9 @@ private function generateInjector(): void
);
}

/**
* @param array<string, string> $factories
*/
private function generateFactoryList(array $factories): void
{
$indentation = sprintf("\n%s", str_repeat(' ', self::INDENTATION_SPACES));
Expand All @@ -124,11 +129,16 @@ function (string $key, string $value): string {
$factories
);

assert(is_string($this->outputDirectory));

$this->buildFromTemplate(self::FACTORY_LIST_TEMPLATE, sprintf('%s/factories.php', $this->outputDirectory), [
'%factories%' => implode($indentation, $codeLines),
]);
}

/**
* @param array<string, string> $factories
*/
private function generateTypeFactory(string $class, array &$factories): void
{
if (isset($factories[$class])) {
Expand All @@ -154,12 +164,12 @@ private function generateTypeFactory(string $class, array &$factories): void

private function generateAutoload(): void
{
$addFactoryPrefix = function ($value) {
$addFactoryPrefix = function (string $value): string {
return 'Factory/' . $value;
};

$classmap = array_map($addFactoryPrefix, $this->factoryGenerator->getClassmap());

$classmap[$this->namespace . '\\GeneratedInjector'] = 'GeneratedInjector.php';

$this->autoloadGenerator->generate($classmap);
Expand Down
9 changes: 6 additions & 3 deletions src/Container/AutowireFactory.php
Expand Up @@ -57,7 +57,9 @@ public function canCreate(ContainerInterface $container, $requestedName)
/**
* Create an instance
*
* @return object
* @template T
* @param string|class-string<T> $requestedName
* @return T
*/
public function create(ContainerInterface $container, string $requestedName, ?array $options = null)
{
Expand All @@ -67,8 +69,9 @@ public function create(ContainerInterface $container, string $requestedName, ?ar
/**
* Make invokable and implement the laminas-service factory pattern
*
* @param string $requestedName
* @return object
* @template T
* @param string|class-string<T> $requestedName
* @return T
*/
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Container/ServiceManager/AutowireFactory.php
Expand Up @@ -44,7 +44,7 @@ public function canCreate(ContainerInterface $container, $requestedName)
* Make invokable and implement the laminas-service factory pattern
*
* @param string $requestedName
* @return bool
* @return object
*/
public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Definition/DefinitionInterface.php
Expand Up @@ -20,7 +20,7 @@ interface DefinitionInterface
/**
* All class names in this definition
*
* @return string[]
* @return list<string>
*/
public function getClasses(): array;

Expand Down
10 changes: 5 additions & 5 deletions src/Definition/Reflection/ClassDefinition.php
Expand Up @@ -22,14 +22,14 @@ class ClassDefinition implements ClassDefinitionInterface
/** @var ReflectionClass */
private $reflection;

/** @var Parameter[] */
/** @var Parameter[]|null */
private $parameters;

/** @var string[] */
/** @var list<string>|null */
private $supertypes;

/**
* @param string|ReflectionClass $class
* @param class-string|ReflectionClass $class
*/
public function __construct($class)
{
Expand All @@ -40,7 +40,7 @@ public function __construct($class)
$this->reflection = $class;
}

private function reflectSupertypes()
private function reflectSupertypes(): void
{
$this->supertypes = [];
$class = $this->reflection;
Expand Down Expand Up @@ -75,7 +75,7 @@ public function getInterfaces(): array
return $this->reflection->getInterfaceNames();
}

private function reflectParameters()
private function reflectParameters(): void
{
$this->parameters = [];

Expand Down
6 changes: 5 additions & 1 deletion src/Definition/Reflection/Parameter.php
Expand Up @@ -13,6 +13,8 @@
use Laminas\Di\Definition\ParameterInterface;
use ReflectionParameter;

use function assert;

/**
* This class specifies a method parameter for the di definition
*/
Expand Down Expand Up @@ -66,7 +68,9 @@ public function getPosition(): int
public function getType(): ?string
{
if ($this->reflection->hasType()) {
return $this->reflection->getType()->getName();
$reflectionType = $this->reflection->getType();
assert($reflectionType !== null);
return $reflectionType->getName();
}

return null;
Expand Down
11 changes: 7 additions & 4 deletions src/Definition/RuntimeDefinition.php
Expand Up @@ -22,10 +22,10 @@
*/
class RuntimeDefinition implements DefinitionInterface
{
/** @var ClassDefinition[] */
/** @var array<string, ClassDefinition> */
private $definition = [];

/** @var bool[] */
/** @var array<string, bool>|null */
private $explicitClasses;

/**
Expand Down Expand Up @@ -83,7 +83,7 @@ public function addExplicitClass(string $class): self
* @param string $class The class name to load
* @throws Exception\ClassNotFoundException
*/
private function loadClass(string $class)
private function loadClass(string $class): void
{
if (! $this->hasClass($class)) {
throw new Exception\ClassNotFoundException($class);
Expand All @@ -93,7 +93,7 @@ private function loadClass(string $class)
}

/**
* @return string[]
* @return list<string>
*/
public function getClasses(): array
{
Expand All @@ -104,6 +104,9 @@ public function getClasses(): array
return array_keys(array_merge($this->definition, $this->explicitClasses));
}

/**
* @psalm-assert-if-true class-string $class
*/
public function hasClass(string $class): bool
{
return class_exists($class);
Expand Down
5 changes: 3 additions & 2 deletions src/Resolver/DependencyResolver.php
Expand Up @@ -41,7 +41,7 @@ class DependencyResolver implements DependencyResolverInterface
/** @var DefinitionInterface */
protected $definition;

/** @var ContainerInterface */
/** @var ContainerInterface|null */
protected $container;

/** @var string[] */
Expand All @@ -57,6 +57,7 @@ class DependencyResolver implements DependencyResolverInterface
'iterable',
];

/** @var array<string, string> */
private $gettypeMap = [
'boolean' => 'bool',
'integer' => 'int',
Expand Down Expand Up @@ -299,7 +300,7 @@ public function resolveParameters(string $requestedType, array $callTimeParamete
throw new Exception\UnexpectedValueException(sprintf(
'Unusable configured injection for parameter "%s" of type "%s"',
$name,
$type
$type ?? 'null'
));
}

Expand Down
24 changes: 24 additions & 0 deletions stubs/psr-container.phpstub
@@ -0,0 +1,24 @@
<?php

namespace Psr\Container;

/**
* Stub for giving psalm additional info about PSR containers
*/
interface ContainerInterface
{
/**
* @template T
* @param string|class-string<T> $id
* @return T
* @throws ContainerExceptionInterface Error while retrieving the entry.
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
*/
public function get($id);

/**
* @param string $id
* @return bool
*/
public function has($id);
}

0 comments on commit 870688a

Please sign in to comment.