Skip to content

Commit

Permalink
Merge pull request #9 from meritoo/chore/phpstan
Browse files Browse the repository at this point in the history
Fix PHPStan errors. Add PHPStan to GitHub Actions.
  • Loading branch information
meritoo authored Nov 14, 2024
2 parents 3f0e0b5 + 0308a4e commit 9f58df1
Show file tree
Hide file tree
Showing 43 changed files with 369 additions and 177 deletions.
40 changes: 36 additions & 4 deletions .github/workflows/verify-stability.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ permissions:
contents: read

jobs:
validate:
Validate:
runs-on: ubuntu-latest
strategy:
matrix:
Expand Down Expand Up @@ -59,16 +59,48 @@ jobs:
php_version: ${{ matrix.php-versions }}
php_extensions: intl
dev: no
phpunit:
PHPStan:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.0', '8.1', '8.2' ]
env:
extensions: dom, fileinfo, intl, json, pcre, simplexml
key: cache-v1 # can be any string, change to clear the extension cache.
needs: [ validate ]
if: needs.validate.result == 'success'
needs: [ Validate ]
if: needs.Validate.result == 'success'
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup cache environment
id: extcache
uses: shivammathur/cache-extensions@v1
with:
php-version: ${{ matrix.php-versions }}
extensions: ${{ env.extensions }}
key: ${{ env.key }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: ${{ env.extensions }}
- name: Install Composer dependencies
uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php-versions }}
php_extensions: intl
- name: Analyse code with PHPStan
run: vendor/bin/phpstan analyse --memory-limit 256M
PHPUnit:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: [ '8.0', '8.1', '8.2' ]
env:
extensions: dom, fileinfo, intl, json, pcre, simplexml
key: cache-v1 # can be any string, change to clear the extension cache.
needs: [ PHPStan ]
if: needs.PHPStan.result == 'success'
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
37 changes: 36 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,44 @@

Common and useful classes, methods, exceptions etc.

# 1.2.3
# 1.3.0

1. Support integers by the `BaseType::isCorrectType()` method
2. Add PHPStan to GitHub Actions
3. Fix PHPStan errors - level `1`
4. Make the `BaseType::isCorrectType()` static method non-static

Before:

```php
if (DatePartType::isCorrectType('day')) {
// ...
}
```

After:

```php
if ((new DatePartType())->isCorrectType('day')) {
// ...
}
```

5. Change of `Meritoo\Common\Utilities\Regex::arrayFilter()` method signature:
1. 3rd argument, the filter, is now a `\Closure|string`. Previously it was `string`.
2. 4th argument has been removed (not necessary).

Before:

```php
public static function arrayFilter(array $array, string $arrayColumnKey, string $filterExpression, bool $itsRegularExpression = false): array
```

After:

```php
public static function arrayFilter(array $array, string $arrayColumnKey, \Closure|string $filter): array
```

# 1.2.2

Expand Down
41 changes: 41 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Meritoo Common Library

## Things to do

- [ ] Bump PHP version: `8.0` -> `8.2`
- [ ] Run GitHub Actions for `8.2`, `8.3`, and `8.4` (do not run for `8.0` and `8.1`)
- [ ] Fix deprecations when running PHPUnit tests
- [ ] Replace all the `*Type` classes
with [enumerations](https://www.php.net/manual/en/language.types.enumerations.php) (
classes that extend `Meritoo\Common\Type\Base\BaseType`)

**A new `enum`:**

```php
<?php

declare(strict_types=1);

namespace Meritoo\Common\Enums;

enum OopVisibility: string
{
case Private = '3';
case Protected = '2';
case Public = '1';
}
```

**A new piece of `[CHANGELOG.md](CHANGELOG.md)`:**

6. Replace all the `*Type` classes
with [enumerations](https://www.php.net/manual/en/language.types.enumerations.php) (
classes that extend `Meritoo\Common\Type\Base\BaseType`)

| Before | After |
|---------------------------|----------------------|
| `class OopVisibilityType` | `enum OopVisibility` |
| ... | ... |
| ... | ... |

- [ ] Bump PHPStan level: `1` -> `2`
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.3
1.3.0
14 changes: 14 additions & 0 deletions docs/Development.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ Metrics:
* `build/reports/infection/infection-log.txt`
* `build/reports/infection/summary-log.txt`
# PHPStan
### Running analysis
```bash
docker-compose exec php vendor/bin/phpstan analyse --memory-limit 256M
```
### Generating the baseline file
```bash
docker-compose exec php vendor/bin/phpstan --generate-baseline
```
# Other
Rebuild project and run tests by running command:
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: "#^Undefined variable\\: \\$isEqual$#"
count: 1
path: src/Utilities/Regex.php
File renamed without changes.
4 changes: 2 additions & 2 deletions src/Collection/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public static function fromArray(array $templates): Templates
{
// No templates. Nothing to do.
if (empty($templates)) {
return new static();
return new self();
}

$result = new static();
$result = new self();

foreach ($templates as $index => $template) {
$result->add(new Template($template), $index);
Expand Down
5 changes: 2 additions & 3 deletions src/Exception/Base/UnknownTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ abstract class UnknownTypeException extends Exception
* @param string $typeName Name of the something
* @return UnknownTypeException
*/
protected static function create($unknownType, BaseType $typeInstance, string $typeName): UnknownTypeException
protected static function createMessage($unknownType, BaseType $typeInstance, string $typeName): string
{
$template = 'The \'%s\' type of %s is unknown. Probably doesn\'t exist or there is a typo. You should use one'
.' of these types: %s.';

$allTypes = $typeInstance->getAll();
$types = Arrays::values2string($allTypes, '', ', ') ?? '[types not found]';
$message = sprintf($template, $unknownType, $typeName, $types);

return new static($message);
return sprintf($template, $unknownType, $typeName, $types);
}
}
2 changes: 1 addition & 1 deletion src/Exception/Bundle/IncorrectBundleNameException.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public static function create(string $bundleName): IncorrectBundleNameException

$message = sprintf($template, $bundleName);

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/File/EmptyFileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public static function create(string $emptyFilePath): EmptyFileException
$template = 'File with path \'%s\' is empty (has no content). Did you provide path of proper file?';
$message = sprintf($template, $emptyFilePath);

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/File/EmptyFilePathException.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ class EmptyFilePathException extends Exception
*/
public static function create(): EmptyFilePathException
{
return new static('Path of the file is empty. Did you provide path of proper file?');
return new self('Path of the file is empty. Did you provide path of proper file?');
}
}
2 changes: 1 addition & 1 deletion src/Exception/File/NotExistingFileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public static function create(string $notExistingFilePath): NotExistingFileExcep
$template = 'File with path \'%s\' does not exist (or is not readable). Did you provide path of proper file?';
$message = sprintf($template, $notExistingFilePath);

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/Method/DisabledMethodException.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public static function create(string $disabledMethod, string $alternativeMethod
$message = sprintf($template, $message, $alternativeMethod);
}

return new static($message);
return new self($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public static function create(string $source, bool $forClass = true): CannotReso
$template = 'Name of %s from given \'%s\'%s cannot be resolved. Is there everything ok?';
$message = sprintf($template, $forWho, gettype($source), $value);

return new static($message);
return new self($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public static function create(string $className): ClassWithoutConstructorExcepti
$template = 'Oops, class \'%s\' hasn\'t constructor. Did you use proper class?';
$message = sprintf($template, $className);

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/Reflection/MissingChildClassesException.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public static function create($parentClass): MissingChildClassesException
$parentClassName = Reflection::getClassName($parentClass) ?? '[unknown class]';
$message = sprintf($template, $parentClassName);

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/Reflection/NotExistingPropertyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public static function create($object, ?string $property): NotExistingPropertyEx
$template = 'Property \'%s\' does not exist in instance of class \'%s\'. Did you use proper name of property?';
$message = sprintf($template, $property, get_class($object));

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/Reflection/TooManyChildClassesException.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public static function create($parentClass, array $childClasses): TooManyChildCl
$parentClassName = Reflection::getClassName($parentClass) ?? '[unknown class]';
$message = sprintf($template, $parentClassName, implode("\n- ", $childClasses), $parentClassName);

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/Regex/IncorrectColorHexLengthException.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public static function create(string $color): IncorrectColorHexLengthException

$message = sprintf($template, $color, strlen($color));

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/Regex/InvalidColorHexValueException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public static function create(string $color): InvalidColorHexValueException
{
$message = sprintf('Hexadecimal value of color \'%s\' is invalid. Is there everything ok?', $color);

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/Regex/InvalidHtmlAttributesException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public static function create(string $htmlAttributes): InvalidHtmlAttributesExce
{
$message = sprintf('HTML attributes \'%s\' are invalid. Is there everything ok?', $htmlAttributes);

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Exception/Regex/InvalidUrlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public static function create(string $url): InvalidUrlException
{
$message = sprintf('Url \'%s\' is invalid. Is there everything ok?', $url);

return new static($message);
return new self($message);
}
}
8 changes: 7 additions & 1 deletion src/Exception/Type/UnknownDatePartTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ class UnknownDatePartTypeException extends UnknownTypeException
*/
public static function createException(string $unknownDatePart, string $value): UnknownDatePartTypeException
{
return parent::create($unknownDatePart, new DatePartType(), sprintf('date part (with value %s)', $value));
$message = parent::createMessage(
$unknownDatePart,
new DatePartType(),
sprintf('date part (with value %s)', $value),
);

return new self($message);
}
}
4 changes: 3 additions & 1 deletion src/Exception/Type/UnknownOopVisibilityTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class UnknownOopVisibilityTypeException extends UnknownTypeException
*/
public static function createException(string $unknownType): UnknownOopVisibilityTypeException
{
return parent::create($unknownType, new OopVisibilityType(), 'OOP-related visibility');
$message = parent::createMessage($unknownType, new OopVisibilityType(), 'OOP-related visibility');

return new self($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public static function create(int $width, int $height): InvalidSizeDimensionsExc
$template = 'Dimensions of size should be positive, but they are not: %d, %d. Is there everything ok?';
$message = sprintf($template, $width, $height);

return new static($message);
return new self($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public static function create(string $content): InvalidContentException
$template = 'Content of template \'%s\' is invalid. Did you use string with 1 placeholder at least?';
$message = sprintf($template, $content);

return new static($message);
return new self($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public static function create(string $content, array $missingPlaceholders): Miss
.' required values?';
$message = sprintf($template, $content, implode(', ', $missingPlaceholders));

return new static($message);
return new self($message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public static function create(string $index): TemplateNotFoundException
$template = 'Template with \'%s\' index was not found. Did you provide all required templates?';
$message = sprintf($template, $index);

return new static($message);
return new self($message);
}
}
2 changes: 1 addition & 1 deletion src/Traits/Test/Base/BaseTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ protected static function assertMethodArgumentsCount(
protected static function assertMethodVisibility(ReflectionMethod $method, string $visibilityType): void
{
// Type of visibility is not correct?
if (!OopVisibilityType::isCorrectType($visibilityType)) {
if (!(new OopVisibilityType())->isCorrectType($visibilityType)) {
throw UnknownOopVisibilityTypeException::createException($visibilityType);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Type/Base/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function getAll(): ?array
return $this->all;
}

public static function isCorrectType(null|string|int $type): bool
public function isCorrectType(null|string|int $type): bool
{
return in_array($type, (new static())->getAll(), true);
return in_array($type, $this->getAll(), true);
}
}
Loading

0 comments on commit 9f58df1

Please sign in to comment.