Skip to content

Commit

Permalink
Replaced psalm with phpstan and fixed many static analysis errors
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Oct 31, 2023
1 parent 9eee96e commit 02a06f2
Show file tree
Hide file tree
Showing 24 changed files with 144 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-suite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: composer update

- name: Run Static Analysis
run: vendor/bin/psalm
run: vendor/bin/phpstan

tests:
name: Tests
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ prod production: # Build application for production

test: # Run coding standards/static analysis checks and tests
@vendor/bin/php-cs-fixer fix --diff --dry-run \
&& vendor/bin/psalm \
&& vendor/bin/phpstan \
&& vendor/bin/phpunit --coverage-text

coverage: # Generate an HTML coverage report
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"yosymfony/toml": "^1.0"
},
"require-dev": {
"phlak/coding-standards": "^2.0",
"phlak/coding-standards": "^2.2",
"phpstan/phpstan": "^1.10",
"psy/psysh": "^0.11",
"vimeo/psalm": "^5.15",
"yoast/phpunit-polyfills": "^2.0"
},
"autoload": {
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
ignoreErrors:
-
message: "#^Method PHLAK\\\\Config\\\\Config\\:\\:load\\(\\) throws checked exception PHLAK\\\\Config\\\\Exceptions\\\\InvalidFileException but it's missing from the PHPDoc @throws tag\\.$#"
count: 2
path: src/Config.php
27 changes: 27 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
parameters:

paths:
- src
- tests

level: max

checkFunctionNameCase: true
checkMissingIterableValueType: false

reportUnmatchedIgnoredErrors: false

exceptions:
implicitThrows: false

check:
missingCheckedExceptionInThrows: true
tooWideThrowType: true

uncheckedExceptionClasses:
- 'InvalidArgumentException'
- 'RuntimeException'
- 'PHPUnit\Framework\Exception'

includes:
- phpstan-baseline.neon
55 changes: 0 additions & 55 deletions psalm.xml

This file was deleted.

57 changes: 27 additions & 30 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
use ArrayAccess;
use DirectoryIterator;
use IteratorAggregate;
use PHLAK\Config\Exceptions\InvalidContextException;
use PHLAK\Config\Interfaces\ConfigInterface;
use PHLAK\Config\Loaders\Loader;
use PHLAK\Config\Traits\Arrayable;
use RuntimeException;
use SplFileInfo;

/**
* @implements ArrayAccess<string, mixed>
* @implements IteratorAggregate<string, mixed>
*/
class Config implements ConfigInterface, ArrayAccess, IteratorAggregate
{
use Arrayable;
Expand All @@ -21,28 +25,17 @@ class Config implements ConfigInterface, ArrayAccess, IteratorAggregate
/**
* Create a new Config object.
*
* @param mixed $context Raw array of configuration options or path to a
* configuration file or directory containing one or
* more configuration files
* @param string|array $prefix A key under which the loaded config will be nested
* @throws InvalidContextException
* @param array|string $context Raw array of configuration options or path to a configuration file
* or directory containing one or more configuration files
* @param string $prefix A key under which the loaded config will be nested
*/
public function __construct($context = null, string $prefix = null)
public function __construct(array|string $context = null, string $prefix = null)
{
switch (gettype($context)) {
case 'NULL':
break;
case 'array':
$this->config = $prefix ? [$prefix => $context] : $context;

break;
case 'string':
$this->load($context, $prefix);

break;
default:
throw new InvalidContextException('Failed to initialize config');
}
match (gettype($context)) {
'array' => $this->config = $prefix ? [$prefix => $context] : $context,
'string' => $this->load($context, $prefix),
'NULL' => null,
};
}

/**
Expand All @@ -54,7 +47,7 @@ public function __construct($context = null, string $prefix = null)
*/
public static function fromDirectory(string $path): ConfigInterface
{
$config = new self();
$config = new self;

foreach (new DirectoryIterator($path) as $file) {
if ($file->isFile()) {
Expand Down Expand Up @@ -93,7 +86,7 @@ public function set(string $key, mixed $value): bool
* Retrieve a configuration option via a provided key.
*
* @param string $key Unique configuration option key
* @param mixed|null $default Default value to return if option does not exist
* @param mixed $default Default value to return if option does not exist
*
* @return mixed Stored config item or $default value
*/
Expand Down Expand Up @@ -138,10 +131,9 @@ public function has(string $key): bool
* @param string $key Unique configuration option key
* @param mixed $value Config item value
*
* @return true
*
* @throws RuntimeException
*
* @return true
*/
public function append(string $key, mixed $value): bool
{
Expand All @@ -166,10 +158,9 @@ public function append(string $key, mixed $value): bool
* @param string $key Unique configuration option key
* @param mixed $value Config item value
*
* @return true
*
* @throws RuntimeException
*
* @return true
*/
public function prepend(string $key, mixed $value): bool
{
Expand Down Expand Up @@ -221,6 +212,7 @@ public function load(string $path, string $prefix = null, bool $override = true)
$className = $file->isDir() ? 'Directory' : ucfirst(strtolower($file->getExtension()));
$classPath = 'PHLAK\\Config\\Loaders\\' . $className;

/** @var Loader $loader */
$loader = new $classPath($file->getRealPath());

$newConfig = $prefix ? [$prefix => $loader->getArray()] : $loader->getArray();
Expand Down Expand Up @@ -259,12 +251,17 @@ public function merge(ConfigInterface $config, bool $override = true): ConfigInt
*
* @param string $key Unique configuration option key
*
* @return \PHLAK\Config\Interfaces\ConfigInterface A new ConfigInterface object
* @throws InvalidContextException
* @return ConfigInterface A new ConfigInterface object
*/
public function split(string $key): ConfigInterface
{
return new self($this->get($key));
$value = $this->get($key);

if (! is_array($value)) {
throw new RuntimeException(sprintf('Config item [%s] is not an array', $key));
}

return new self($value);
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Exceptions/ConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

use Exception;

abstract class ConfigException extends Exception
{
}
abstract class ConfigException extends Exception {}
7 changes: 0 additions & 7 deletions src/Exceptions/InvalidContextException.php

This file was deleted.

4 changes: 1 addition & 3 deletions src/Exceptions/InvalidFileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace PHLAK\Config\Exceptions;

class InvalidFileException extends ConfigException
{
}
class InvalidFileException extends ConfigException {}
3 changes: 1 addition & 2 deletions src/Interfaces/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ConfigInterface
* more configuration files
* @param string $prefix A key under which the loaded config will be nested
*/
public function __construct($context = null, string $prefix = null);
public function __construct(array|string $context = null, string $prefix = null);

/**
* Create a new instance of a ConfigInterface objet from a directory with
Expand Down Expand Up @@ -91,7 +91,6 @@ public function unset(string $key): bool;
* @param string $prefix A key under which the loaded config will be nested
* @param bool $override Whether to override existing options with
* values from the loaded file
* @return ConfigInterface
*/
public function load(string $path, string $prefix = null, bool $override = true): self;

Expand Down
7 changes: 3 additions & 4 deletions src/Loaders/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class Directory extends Loader
* and convert them to an array of configuration options. Any invalid files
* will be silently ignored.
*
* @throws \PHLAK\Config\Exceptions\InvalidFileException
*
* @return array Array of configuration options
*/
public function getArray(): array
Expand All @@ -28,12 +26,13 @@ public function getArray(): array
$className = $file->isDir() ? 'Directory' : ucfirst(strtolower($file->getExtension()));
$classPath = 'PHLAK\\Config\\Loaders\\' . $className;

/** @var Loader $loader */
$loader = new $classPath($file->getPathname());

try {
$contents = array_merge($contents, $loader->getArray());
} catch (InvalidFileException $e) {
// Ignore it and continue
} catch (InvalidFileException) {
continue;
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Loaders/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function getArray(): array
{
$contents = file_get_contents($this->context);

if ($contents === false) {
throw new InvalidFileException('Unable to parse invalid JSON file at ' . $this->context);
}

/** @var array|null $parsed */
$parsed = json_decode($contents, true);

if (is_null($parsed)) {
Expand Down
3 changes: 3 additions & 0 deletions src/Loaders/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHLAK\Config\Loaders;

use PHLAK\Config\Exceptions\InvalidFileException;
use PHLAK\Config\Interfaces\Loadable;

abstract class Loader implements Loadable
Expand All @@ -22,6 +23,8 @@ public function __construct(string $context)
/**
* Retrieve the context as an array of configuration options.
*
* @throws InvalidFileException
*
* @return array Array of configuration options
*/
abstract public function getArray(): array;
Expand Down
1 change: 1 addition & 0 deletions src/Loaders/Toml.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Toml extends Loader
public function getArray(): array
{
try {
/** @var array $parsed */
$parsed = TomlParser::parseFile($this->context);
} catch (ParseException $e) {
throw new InvalidFileException($e->getMessage());
Expand Down
14 changes: 12 additions & 2 deletions src/Loaders/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHLAK\Config\Loaders;

use JsonException;
use PHLAK\Config\Exceptions\InvalidFileException;

class Xml extends Loader
Expand All @@ -18,10 +19,19 @@ public function getArray(): array
{
$parsed = @simplexml_load_file($this->context);

if (! $parsed) {
if ($parsed === false) {
throw new InvalidFileException('Unable to parse invalid XML file at ' . $this->context);
}

return json_decode(json_encode($parsed), true);
try {
$json = json_encode($parsed, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $exception) {
throw new InvalidFileException(previous: $exception);
}

/** @var array $array */
$array = json_decode($json, true);

return $array;
}
}
Loading

0 comments on commit 02a06f2

Please sign in to comment.