Skip to content

Commit

Permalink
Upgrade psalm to level 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Chemaclass committed Jun 26, 2021
1 parent 9396380 commit b2d21bb
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 18 deletions.
10 changes: 1 addition & 9 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand All @@ -12,12 +12,4 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<UnresolvableInclude errorLevel="suppress"/>
<ImplementedReturnTypeMismatch errorLevel="suppress"/>
<ImplicitToStringCast errorLevel="suppress"/>
<UndefinedMagicMethod errorLevel="suppress"/>
<InvalidStringClass errorLevel="suppress"/>
</issueHandlers>
</psalm>
2 changes: 1 addition & 1 deletion src/CodeGenerator/CodeGeneratorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ public function getComposerJsonContentAsArray(): array
throw new LogicException('composer.json file not found but it is required');
}

return json_decode(file_get_contents($filename), true);
return (array)json_decode(file_get_contents($filename), true);
}
}
8 changes: 7 additions & 1 deletion src/CodeGenerator/Domain/CommandArgumentsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public function parse(string $desiredNamespace): CommandArguments
throw CommandArgumentsException::noAutoloadPsr4Found();
}

$psr4 = $this->composerJson['autoload']['psr-4'];
/** @var array{psr-4: string[]} $composerAutoload */
$composerAutoload = $this->composerJson['autoload'];

/** @var string[] $psr4 */
$psr4 = $composerAutoload['psr-4'];
$allPsr4Combinations = $this->allPossiblePsr4Combinations($desiredNamespace);

foreach ($allPsr4Combinations as $psr4Combination) {
Expand All @@ -53,6 +57,8 @@ public function parse(string $desiredNamespace): CommandArguments
* 'App/TestModule',
* 'App',
* ].
*
* @return string[]
*/
private function allPossiblePsr4Combinations(string $desiredNamespace): array
{
Expand Down
8 changes: 6 additions & 2 deletions src/Framework/ClassResolver/AbstractClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

abstract class AbstractClassResolver
{
/** @var array<string,mixed> */
/** @var array<string,object|mixed> */
protected static array $cachedInstances = [];

protected static ?ClassResolverFactory $classResolverFactory = null;
Expand All @@ -20,7 +20,10 @@ abstract public function resolve(object $callerClass): ?object;

abstract protected function getResolvableType(): string;

public function doResolve(object $callerClass): ?object
/**
* @return mixed|null
*/
public function doResolve(object $callerClass)
{
$this->setCallerObject($callerClass);
$cacheKey = $this->getCacheKey();
Expand Down Expand Up @@ -91,6 +94,7 @@ public function getClassInfo(): ClassInfo
private function createInstance(string $resolvedClassName)
{
if (class_exists($resolvedClassName)) {
/** @psalm-suppress MixedMethodCall */
return new $resolvedClassName();
}

Expand Down
9 changes: 8 additions & 1 deletion src/Framework/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public static function init(): void
foreach (self::scanAllConfigFiles() as $filename) {
$fileNameOrDir = self::fullPath($filename);
if (is_dir($fileNameOrDir)) {
/** @var array{0:string} $fileInfo */
foreach (self::createRecursiveIterator($fileNameOrDir) as $fileInfo) {
if (self::isPhpFile($fileInfo[0])) {
$configs[] = self::readConfigFromFile($fileInfo[0]);
Expand All @@ -82,17 +83,22 @@ public static function init(): void
self::$config = array_merge(...$configs);
}

/**
* @return string[]
*/
private static function scanAllConfigFiles(): array
{
$configDir = self::getApplicationRootDir() . '/config/';
if (!is_dir($configDir)) {
throw new RuntimeException('"config" directory not found on application root dir');
}

return array_diff(
$paths = array_diff(
scandir($configDir),
['..', '.', self::CONFIG_LOCAL_FILENAME]
);

return array_map(static fn ($p) => (string)$p, $paths);
}

public static function getApplicationRootDir(): string
Expand Down Expand Up @@ -131,6 +137,7 @@ private static function isPhpFile(string $path): bool
private static function readConfigFromFile(string $file): array
{
if (file_exists($file)) {
/** @var array|null $content */
$content = include $file;
return is_array($content) ? $content : [];
}
Expand Down
6 changes: 5 additions & 1 deletion src/Framework/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ public function get(string $id)
}

$rawService = $this->services[$id];

/** @psalm-suppress InvalidFunctionCall */
$resolvedService = $this->services[$id] = $rawService($this);
$this->services[$id] = $rawService($this);

/** @var mixed $resolvedService */
$resolvedService = $this->services[$id];
$this->raw[$id] = $rawService;

return $resolvedService;
Expand Down
5 changes: 4 additions & 1 deletion src/Framework/Container/Locator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class Locator

private static ?Locator $instance = null;

/** @var mixed[] */
/** @var array<string, mixed> */
private static array $instanceCache = [];

public static function getInstance(): self
Expand Down Expand Up @@ -44,7 +44,9 @@ public function get(string $className)
return self::$instanceCache[$concreteClass];
}

/** @var mixed $newInstance */
$newInstance = $this->newInstance($concreteClass);
/** @psalm-suppress MixedAssignment */
self::$instanceCache[$concreteClass] = $newInstance;

return $newInstance;
Expand All @@ -65,6 +67,7 @@ private function getConcreteClass(string $className): string
private function newInstance(string $className)
{
if (class_exists($className)) {
/** @psalm-suppress MixedMethodCall */
return new $className();
}

Expand Down
6 changes: 4 additions & 2 deletions src/Framework/Exception/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ private function __construct()
private function getTraceLine(array $backtrace): string
{
if (isset($backtrace['file'])) {
return $backtrace['file'] . ':' . $backtrace['line'];
return ((string)$backtrace['file']) . ':' . ((string)$backtrace['line']);
}

return $this->getTraceLineFromTestCase($backtrace);
}

private function getTraceLineFromTestCase(array $backtrace): string
{
return $backtrace['class'] . $backtrace['type'] . $backtrace['function'];
return ((string)$backtrace['class'])
. ((string)$backtrace['type'])
. ((string)$backtrace['function']);
}
}

0 comments on commit b2d21bb

Please sign in to comment.