Skip to content

Commit

Permalink
Increase Stan level to 8 (#8)
Browse files Browse the repository at this point in the history
* increase stan level to 8

* update action
  • Loading branch information
husamAwadhi authored Sep 2, 2023
1 parent 1d72ce9 commit ab1c3ba
Show file tree
Hide file tree
Showing 21 changed files with 134 additions and 125 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ jobs:
run: composer install --no-progress --prefer-dist --optimize-autoloader

- name: Static analysis with PHPStan
run: ./vendor/bin/phpstan analyse -l 5
run: ./vendor/bin/phpstan analyse --ansi
6 changes: 5 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
parameters:
level: 5
level: 8
paths:
- src/
- tests/
parallel:
processTimeout: 300.0
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
ignoreErrors:
- message: '#Left side of && is always false.#'
path: src/Blueprint/Components/Components.php
count: 1
- message: '#Parameter $length of function file_get_contents expects int<0, max>|null, int|null given.#'
path: src/Parser/Utils/IOCapable.php
count: 4
2 changes: 1 addition & 1 deletion src/Blueprint/Components/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Components implements ComponentInterface, Iterator
private const MISSING_ELEMENT = 'Blueprint section %s is missing a mandatory element %s';
private const INVALID_VALUE = 'Blueprint %s element has invalid value (%s). Acceptable value(s) [%s]';

private $position = 0;
private int $position = 0;

/** @var Component[] */
public readonly array $components;
Expand Down
9 changes: 3 additions & 6 deletions src/Blueprint/Components/Conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(
$this->position = 0;
}

protected function buildConditions($conditions): array
protected function buildConditions(array $conditions): array
{
$objectConditions = [];
foreach ($conditions as $condition) {
Expand Down Expand Up @@ -75,9 +75,6 @@ public static function validation(array &$conditions): void

$case = self::getConditionKeyword($condition);

if ($case === false) {
throw new InvalidFieldException('no valid condition found');
}
$finalConditions[] = [
'column' => $condition['column'],
'keyword' => $case,
Expand All @@ -87,7 +84,7 @@ public static function validation(array &$conditions): void
$conditions = $finalConditions;
}

protected static function getConditionKeyword(array $condition): ConditionKeyword | bool
protected static function getConditionKeyword(array $condition): ConditionKeyword
{
foreach (ConditionKeyword::cases() as $case) {
if (
Expand All @@ -99,7 +96,7 @@ protected static function getConditionKeyword(array $condition): ConditionKeywor
}
}

return false;
throw new InvalidFieldException('no valid condition keyword found');
}

public static function getMandatoryElements(): array
Expand Down
4 changes: 2 additions & 2 deletions src/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ final class Dictionary
private const FILE = 'en';
private const DICTIONARY_DIRECTORY = '/storage/dictionary/';

private readonly string $filePath;
private readonly ?string $filePath;

public function __construct(?string $filePath = null)
{
$this->filePath = $this->generateFilePath($filePath);
}

private function generateFilePath(?string $filePath = null)
private function generateFilePath(?string $filePath = null): ?string
{
return is_file($filePath ?? '')
? $filePath
Expand Down
4 changes: 2 additions & 2 deletions src/Parser/Extension/BlueprintInterpreter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function matchHit(Component $component, array $row): bool
{
$found = false;
/** @var Condition $condition */
foreach ($component->conditions as $condition) {
foreach ($component->conditions ?? [] as $condition) {
$passedCondition = false;
foreach ($condition->columns as $column) {
if (isset($row[$column - 1])) {
Expand All @@ -101,7 +101,7 @@ protected function matchHit(Component $component, array $row): bool
return $found;
}

protected function matchNext($index): bool
protected function matchNext(int $index): bool
{
return $this->lastHitIndex === $index - 1;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ public function getParsedContentPlugin(): ParserPluginInterface
*/
public function getAsObject(): stdClass
{
return json_decode(json_encode($this->getAsArray()));
$json = json_encode($this->getAsArray());

return $json === false ? new stdClass() : json_decode($json);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/ParserBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function addBlueprint(Blueprint $blueprint): self

public function addFile(string $path): self
{
$this->fileContent = $this->load($path, length: $this->maxFileLength);
$this->fileContent = $this->load($path);

return $this;
}
Expand Down
34 changes: 25 additions & 9 deletions src/Parser/Utils/IOCapable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ trait IOCapable

private string $path = '';

/** @var resource */
private $file;

/**
Expand All @@ -20,13 +21,22 @@ trait IOCapable
*/
protected function writeTemporaryFile(string $content): string
{
$this->path = tempnam(sys_get_temp_dir(), __FUNCTION__);
$this->file = fopen($this->path, 'r+b');
$path = tempnam(sys_get_temp_dir(), __FUNCTION__);

if (!is_resource($this->file)) {
if (!$path) {
throw new UnableToCreateFileException('Unable to get a temporary file path');
}

$this->path = $path;

$file = fopen($this->path, 'r+b');

if (!is_resource($file)) {
throw new UnableToCreateFileException("Unable to create temporary file in {$this->path}");
}

$this->file = $file;

if (!is_writable($this->path)) {
throw new UnableToCreateFileException("Unable to modify temporary file in {$this->path}");
}
Expand All @@ -41,10 +51,10 @@ protected function deleteTemporaryFile(): bool
return $this->path ? unlink($this->path) : true;
}

private function loadFromLink($url): string
private function loadFromLink(string $url): string
{
// TODO: use curl ... maybe
$content = \file_get_contents($url, length: $this->maxFileLength);
$content = \file_get_contents($url, length: $this->maxFileLength ?? null);

if ($content === false) {
throw new InvalidArgumentException("unable to load file from link: {$url}");
Expand All @@ -53,21 +63,27 @@ private function loadFromLink($url): string
return $content;
}

private function loadFromPath($path): string
private function loadFromPath(string $path): string
{
if (!is_readable($path)) {
throw new InvalidArgumentException("unable to read file from link: {$path}");
throw new InvalidArgumentException("unable to read file from path: {$path}");
}

return \file_get_contents($path, length: $this->maxFileLength);
$content = \file_get_contents($path, length: $this->maxFileLength ?? null);

if ($content === false) {
throw new InvalidArgumentException("unable to load file from path: {$path}");
}

return $content;
}

/**
* @throws InvalidArgumentException
*
* @return string the read data or false on failure
*/
protected function load(string $parameter, int $length): string
protected function load(string $parameter): string
{
if (filter_var($parameter, \FILTER_VALIDATE_URL)) {
return $this->loadFromLink($parameter);
Expand Down
10 changes: 5 additions & 5 deletions tests/Blueprint/BlueprintBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class BlueprintBuilderTest extends TestCase
protected string $blueprintsDirectory = STORAGE_DIRECTORY . '/blueprints/';


public function testCreateFromString()
public function testCreateFromString(): void
{
$rawFile = file_get_contents($this->blueprintsDirectory . 'valid.yaml');
$rawFile = (string) file_get_contents($this->blueprintsDirectory . 'valid.yaml');

$builder = new BlueprintBuilder(new BlueprintHelper());
$builder->parse($rawFile);
Expand All @@ -26,7 +26,7 @@ public function testCreateFromString()
$this->assertInstanceOf(BlueprintInterface::class, $blueprint);
}

public function testCreateFromPath()
public function testCreateFromPath(): void
{
$path = $this->blueprintsDirectory . 'valid.yaml';

Expand All @@ -40,7 +40,7 @@ public function testCreateFromPath()
/**
* @dataProvider emptyStreamsProvider
*/
public function testThrowingExceptionOnEmptyStream(string $stream, bool $isPath)
public function testThrowingExceptionOnEmptyStream(string $stream, bool $isPath): void
{
$this->expectException(InvalidBlueprintException::class);
$builder = new BlueprintBuilder(new BlueprintHelper());
Expand All @@ -51,7 +51,7 @@ public function testThrowingExceptionOnEmptyStream(string $stream, bool $isPath)
}
$_ = $builder->build();
}
public function emptyStreamsProvider()
public function emptyStreamsProvider(): array
{
return [
['', false],
Expand Down
9 changes: 5 additions & 4 deletions tests/Blueprint/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class BlueprintTest extends TestCase
{
protected string $blueprintsDirectory = STORAGE_DIRECTORY . '/blueprints/';

public function testSuccessfullyCreateBlueprint()
public function testSuccessfullyCreateBlueprint(): void
{
$rawFile = file_get_contents($this->blueprintsDirectory . 'valid.yaml');
$rawFile = (string) file_get_contents($this->blueprintsDirectory . 'valid.yaml');
$blueprint = Blueprint::from(
\yaml_parse($rawFile),
$this->blueprintsDirectory . 'valid.yaml',
Expand All @@ -29,14 +29,15 @@ public function testSuccessfullyCreateBlueprint()

/**
* @dataProvider invalidFilesProvider
* @param class-string<\Throwable> $exception
*/
public function testThrowingExceptionOnInvalidBlueprint(string $fileName, string $exception)
public function testThrowingExceptionOnInvalidBlueprint(string $fileName, string $exception): void
{
$this->expectException($exception);
$path = $this->blueprintsDirectory . $fileName . '.yaml';
Blueprint::from(\yaml_parse_file($path), 'some-path', new BlueprintHelper());
}
public function invalidFilesProvider()
public function invalidFilesProvider(): array
{
return [
['invalid_blueprint_1', InvalidBlueprintException::class],
Expand Down
16 changes: 9 additions & 7 deletions tests/Blueprint/Components/ComponentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ComponentsTest extends TestCase
{
protected string $blueprintsDirectory = STORAGE_DIRECTORY . '/blueprints/';

protected $helper;
protected mixed $helper;

public function setUp(): void
{
Expand All @@ -25,8 +25,9 @@ public function setUp(): void

/**
* @dataProvider invalidFilesProvider
* @param class-string<\Throwable> $exception
*/
public function testThrowingExceptionOnInvalidBlueprint(string $fileName, string $exception)
public function testThrowingExceptionOnInvalidBlueprint(string $fileName, string $exception): void
{
$this->expectException($exception);
$path = $this->blueprintsDirectory . $fileName . '.yaml';
Expand All @@ -35,7 +36,7 @@ public function testThrowingExceptionOnInvalidBlueprint(string $fileName, string
$_ = $builder->load($path)
->build();
}
public function invalidFilesProvider()
public function invalidFilesProvider(): array
{
return [
['invalid_component_1', InvalidComponentException::class],
Expand All @@ -45,7 +46,7 @@ public function invalidFilesProvider()
/**
* @dataProvider validParametersDataProvider
*/
public function testCreateFromParameters($parametersArray)
public function testCreateFromParameters(array $parametersArray): void
{
$components = Components::from($parametersArray, new BlueprintHelper());

Expand All @@ -69,7 +70,7 @@ public function testCreateFromParameters($parametersArray)
$components->components,
);
}
public function validParametersDataProvider()
public function validParametersDataProvider(): array
{
return [
[
Expand All @@ -92,13 +93,14 @@ public function validParametersDataProvider()

/**
* @dataProvider invalidParametersDataProvider
* @param class-string<\Throwable> $exception
*/
public function testCreateFromInvalidParameters($parametersArray, $exception)
public function testCreateFromInvalidParameters(array $parametersArray, string $exception): void
{
$this->expectException($exception);
$_ = Components::from($parametersArray, $this->helper);
}
public function invalidParametersDataProvider()
public function invalidParametersDataProvider(): array
{
return [
[
Expand Down
9 changes: 5 additions & 4 deletions tests/Blueprint/Components/ConditionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class ConditionsTest extends TestCase
/**
* @dataProvider validParametersDataProvider
*/
public function testCreateFromParameters($parametersArray, $expected)
public function testCreateFromParameters(array $parametersArray, array $expected): void
{
$conditions = Conditions::from($parametersArray, new BlueprintHelper());

$this->assertIsIterable($conditions);
$this->assertEquals($conditions->conditions, $expected);
}
public function validParametersDataProvider()
public function validParametersDataProvider(): array
{
return [
[
Expand Down Expand Up @@ -51,13 +51,14 @@ public function validParametersDataProvider()

/**
* @dataProvider invalidParametersDataProvider
* @param class-string<\Throwable> $exception
*/
public function testCreateFromInvalidParameters($parametersArray, $exception)
public function testCreateFromInvalidParameters(array $parametersArray, string $exception): void
{
$this->expectException($exception);
$_ = Conditions::from($parametersArray, new BlueprintHelper());
}
public function invalidParametersDataProvider()
public function invalidParametersDataProvider(): array
{
return [
[
Expand Down
Loading

0 comments on commit ab1c3ba

Please sign in to comment.