Skip to content

Commit

Permalink
Replaced psalm with phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
PHLAK committed Oct 31, 2023
1 parent 9eee96e commit e9a9448
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 112 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
Empty file added phpstan-baseline.neon
Empty file.
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.

31 changes: 19 additions & 12 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
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,13 +26,13 @@ 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
* @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
*
* @throws InvalidContextException
*/
public function __construct($context = null, string $prefix = null)
public function __construct(array|string $context = null, string $prefix = null)
{
switch (gettype($context)) {
case 'NULL':
Expand All @@ -50,11 +55,13 @@ public function __construct($context = null, string $prefix = null)
*
* @param string $path A path to a directory of configuration files
*
* @throws InvalidContextException
*
* @return \PHLAK\Config\Interfaces\ConfigInterface A new ConfigInterface object
*/
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 +100,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 +145,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 +172,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 +226,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,8 +265,9 @@ 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
{
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 {}
4 changes: 1 addition & 3 deletions src/Exceptions/InvalidContextException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@

namespace PHLAK\Config\Exceptions;

class InvalidContextException extends ConfigException
{
}
class InvalidContextException extends ConfigException {}
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
10 changes: 2 additions & 8 deletions src/Loaders/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace PHLAK\Config\Loaders;

use DirectoryIterator;
use PHLAK\Config\Exceptions\InvalidFileException;

class Directory extends Loader
{
Expand All @@ -12,8 +11,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,13 +25,10 @@ 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
}
$contents = array_merge($contents, $loader->getArray());
}

return $contents;
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
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
16 changes: 13 additions & 3 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 @@ -16,12 +17,21 @@ class Xml extends Loader
*/
public function getArray(): array
{
$parsed = @simplexml_load_file($this->context);
$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;
}
}
12 changes: 9 additions & 3 deletions src/Loaders/Yaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ class Yaml extends Loader
*/
public function getArray(): array
{
$contents = file_get_contents($this->context);

if ($contents === false) {
throw new InvalidFileException(sprintf('Unable to parse file [%s]', $this->context));
}

try {
$parsed = YamlParser::parse(file_get_contents($this->context));
} catch (ParseException $e) {
throw new InvalidFileException($e->getMessage());
$parsed = YamlParser::parse($contents);
} catch (ParseException $exception) {
throw new InvalidFileException($exception->getMessage());
}

if (! is_array($parsed)) {
Expand Down
12 changes: 4 additions & 8 deletions src/Traits/Arrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,19 @@ public function getIterator(): Traversable
/**
* Determine whether an item exists at a specific offset.
*
* @param int $offset Offset to check for existence
*
* @return bool
* @param mixed $offset Offset to check for existence
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
return isset($this->config[$offset]);
}

/**
* Retrieve an item at a specific offset.
*
* @param int $offset Position of character to get
*
* @return mixed
* @param mixed $offset Position of character to get
*/
public function offsetGet($offset): mixed
public function offsetGet(mixed $offset): mixed
{
return $this->config[$offset];
}
Expand Down
Loading

0 comments on commit e9a9448

Please sign in to comment.