Skip to content

Commit

Permalink
Issue #75: Replaced Psalm with PHPStan
Browse files Browse the repository at this point in the history
Signed-off-by: alexmerlin <alex.merlin.1985@gmail.com>
  • Loading branch information
alexmerlin committed Feb 20, 2025
1 parent aaf3967 commit bd2c03e
Show file tree
Hide file tree
Showing 33 changed files with 136 additions and 69 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
on:
- push

name: Run PHPStan checks

jobs:
mutation:
name: PHPStan ${{ matrix.php }}-${{ matrix.os }}

runs-on: ${{ matrix.os }}

strategy:
matrix:
os:
- ubuntu-latest

php:
- "8.2"
- "8.3"
- "8.4"

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: "${{ matrix.php }}"
coverage: pcov
ini-values: assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On
tools: composer:v2, cs2pr

- name: Determine composer cache directory
run: echo "COMPOSER_CACHE_DIR=$(composer config cache-dir)" >> $GITHUB_ENV

- name: Cache dependencies installed with composer
uses: actions/cache@v4
with:
path: ${{ env.COMPOSER_CACHE_DIR }}
key: php${{ matrix.php }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: |
php${{ matrix.php }}-composer-
- name: Install dependencies with composer
run: composer install --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run static analysis with PHPStan
run: vendor/bin/phpstan analyse
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Documentation is available at: https://docs.dotkernel.org/dot-log/.

[![Build Static](https://github.com/dotkernel/dot-log/actions/workflows/continuous-integration.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/dot-log/actions/workflows/continuous-integration.yml)
[![codecov](https://codecov.io/gh/dotkernel/dot-log/graph/badge.svg?token=JX19KTBRCZ)](https://codecov.io/gh/dotkernel/dot-log)
[![PHPStan](https://github.com/dotkernel/dot-log/actions/workflows/static-analysis.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/dot-log/actions/workflows/static-analysis.yml)

## Adding The Config Provider

Expand Down
11 changes: 5 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
},
"require-dev": {
"laminas/laminas-coding-standard": "^3.0",
"phpunit/phpunit": "^10.2",
"vimeo/psalm": "^6.0"
"phpstan/phpstan": "^2.1",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^10.2"
},
"autoload": {
"psr-4": {
Expand All @@ -40,8 +41,7 @@
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"laminas/laminas-dependency-plugin": false
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"scripts": {
Expand All @@ -53,7 +53,6 @@
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
"static-analysis": "psalm --shepherd --stats"
"static-analysis": "phpstan analyse --memory-limit 1G"
}
}
14 changes: 14 additions & 0 deletions docs/book/v5/overview.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Overview

Robust, composite PSR-3 compliant logger with filtering and formatting.

## Badges

![OSS Lifecycle](https://img.shields.io/osslifecycle/dotkernel/dot-log)
![PHP from Packagist (specify version)](https://img.shields.io/packagist/php-v/dotkernel/dot-log/5.0.0)

[![GitHub issues](https://img.shields.io/github/issues/dotkernel/dot-log)](https://github.com/dotkernel/dot-log/issues)
[![GitHub forks](https://img.shields.io/github/forks/dotkernel/dot-log)](https://github.com/dotkernel/dot-log/network)
[![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-log)](https://github.com/dotkernel/dot-log/stargazers)
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-log)](https://github.com/dotkernel/dot-log/blob/5.0/LICENSE.md)

[![Build Static](https://github.com/dotkernel/dot-log/actions/workflows/continuous-integration.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/dot-log/actions/workflows/continuous-integration.yml)
[![codecov](https://codecov.io/gh/dotkernel/dot-log/graph/badge.svg?token=JX19KTBRCZ)](https://codecov.io/gh/dotkernel/dot-log)
[![PHPStan](https://github.com/dotkernel/dot-log/actions/workflows/static-analysis.yml/badge.svg?branch=5.0)](https://github.com/dotkernel/dot-log/actions/workflows/static-analysis.yml)
21 changes: 21 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
parameters:
level: 5
paths:
- src
- test
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
message: '/Undefined variable: \$test/'
path: test/LoggerTest.php
-
message: '/Property Dot\\Log\\Factory\\WriterFactory::\$creationOptions is never read, only written./'
path: src/Factory/WriterFactory.php
-
message: '/Parameter #1 \$writerPlugins of method Dot\\Log\\Logger::setWriterPluginManager\(\) expects Dot\\Log\\Manager\\WriterPluginManager, Laminas\\ServiceManager\\AbstractPluginManager given./'
path: src/Logger.php
-
message: '/Parameter #1 \$plugins of method Dot\\Log\\Logger::setProcessorPluginManager\(\) expects class-string|Dot\\Log\\Manager\\ProcessorPluginManager, Laminas\\ServiceManager\\AbstractPluginManager given./'
path: src/Logger.php
17 changes: 0 additions & 17 deletions psalm.xml

This file was deleted.

2 changes: 1 addition & 1 deletion src/Factory/WriterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class WriterFactory implements FactoryInterface
/**
* Options to pass to the constructor if any.
*/
private null|array $creationOptions;
private ?array $creationOptions = null;

public function __construct(?array $creationOptions = null)
{
Expand Down
17 changes: 4 additions & 13 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function __construct(?iterable $options = null)
$this->processors = new SplPriorityQueue();

if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
$options = ArrayUtils::iteratorToArray((array) $options);
}

if (! $options) {
Expand Down Expand Up @@ -255,13 +255,8 @@ public function addWriter(WriterInterface|string $writer, int $level = 1, ?array
{
if (is_string($writer)) {
$writer = $this->writerPlugin($writer, $options);
} elseif (! $writer instanceof Writer\WriterInterface) {
throw new InvalidArgumentException(sprintf(
'Writer must implement %s\Writer\WriterInterface; received "%s"',
__NAMESPACE__,
$writer::class
));
}

$this->writers->insert($writer, $level);

return $this;
Expand Down Expand Up @@ -331,12 +326,8 @@ public function addProcessor(
): static {
if (is_string($processor)) {
$processor = $this->processorPlugin($processor, $options);
} elseif (! $processor instanceof Processor\ProcessorInterface) {
throw new InvalidArgumentException(sprintf(
'Processor must implement Laminas\Log\ProcessorInterface; received "%s"',
$processor::class
));
}

$this->processors->insert($processor, $level);

return $this;
Expand Down Expand Up @@ -364,7 +355,7 @@ public function log(mixed $level, string|Stringable $message, iterable $context
}

if ($context instanceof Traversable) {
$context = ArrayUtils::iteratorToArray($context);
$context = ArrayUtils::iteratorToArray((array) $context);
}

if ($this->writers->count() === 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/Manager/FilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Laminas\ServiceManager\ServiceManager;

use function gettype;
use function is_object;
Expand All @@ -20,6 +21,7 @@
/**
* @template F of FilterPluginManager
* @extends AbstractPluginManager<F>
* @psalm-import-type FactoriesConfiguration from ServiceManager
*/
class FilterPluginManager extends AbstractPluginManager
{
Expand All @@ -32,7 +34,7 @@ class FilterPluginManager extends AbstractPluginManager
'validator' => Validator::class,
];

/** @var string[]|callable[] */
/** @inheritDoc */
protected array $factories = [
Level::class => InvokableFactory::class,
Regex::class => InvokableFactory::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Manager/FormatterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FormatterPluginManager extends AbstractPluginManager
'simple' => Simple::class,
];

/** @var string[]|callable[] */
/** @inheritDoc */
protected array $factories = [
Simple::class => InvokableFactory::class,
];
Expand Down
2 changes: 1 addition & 1 deletion src/Manager/ProcessorPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProcessorPluginManager extends AbstractPluginManager
'requestid' => RequestId::class,
];

/** @var string[]|callable[] */
/** @inheritDoc */
protected array $factories = [
Backtrace::class => InvokableFactory::class,
PsrPlaceholder::class => InvokableFactory::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Manager/WriterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class WriterPluginManager extends AbstractPluginManager
'laminaslogwriternull' => Noop::class,
];

/** @var string[]|callable[] */
/** @inheritDoc */
protected array $factories = [
Noop::class => WriterFactory::class,
Stream::class => WriterFactory::class,
Expand Down
5 changes: 4 additions & 1 deletion src/Writer/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Dot\Log\Exception\RuntimeException;
use ErrorException;
use Laminas\Stdlib\ErrorHandler;
use Psr\Container\ContainerExceptionInterface;
use Traversable;

use function chmod;
Expand Down Expand Up @@ -41,6 +42,7 @@ class Stream extends AbstractWriter
protected mixed $stream;

/**
* @throws ContainerExceptionInterface
* @throws ErrorException
*/
public function __construct(
Expand Down Expand Up @@ -73,6 +75,7 @@ public function __construct(
));
}

$error = null;
if (is_resource($streamOrUrl)) {
if ('stream' !== get_resource_type($streamOrUrl)) {
throw new InvalidArgumentException(sprintf(
Expand All @@ -95,7 +98,7 @@ public function __construct(
touch($streamOrUrl);
chmod($streamOrUrl, $filePermissions);
}
$this->stream = fopen($streamOrUrl, $mode, false);
$this->stream = fopen($streamOrUrl, $mode);
$error = ErrorHandler::stop();
}

Expand Down
2 changes: 1 addition & 1 deletion test/Factory/FilterPluginManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ public function testWillInstantiate(): void

$factory = (new FilterPluginManagerFactory())($this->container);

$this->assertInstanceOf(FilterPluginManager::class, $factory);
$this->assertSame(FilterPluginManager::class, $factory::class);
}
}
2 changes: 1 addition & 1 deletion test/Factory/FormatterPluginManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ public function testWillInstantiate(): void

$factory = (new FormatterPluginManagerFactory())($this->container);

$this->assertInstanceOf(FormatterPluginManager::class, $factory);
$this->assertSame(FormatterPluginManager::class, $factory::class);
}
}
2 changes: 1 addition & 1 deletion test/Factory/LoggerAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function testWillInstantiate(): void

$factory = (new LoggerAbstractServiceFactory())($this->container, 'dot-log.test-log');

$this->assertInstanceOf(Logger::class, $factory);
$this->assertSame(Logger::class, $factory::class);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/Factory/ProcessorPluginManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ public function testWillInstantiate(): void

$factory = (new ProcessorPluginManagerFactory())($this->container);

$this->assertInstanceOf(ProcessorPluginManager::class, $factory);
$this->assertSame(ProcessorPluginManager::class, $factory::class);
}
}
3 changes: 2 additions & 1 deletion test/Factory/WriterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function testSetCreationOptions(): void
{
$input = [];

$this->assertNull($this->subject->setCreationOptions($input));
$this->expectNotToPerformAssertions();
$this->subject->setCreationOptions($input);
}
}
4 changes: 2 additions & 2 deletions test/Factory/WriterPluginManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testWillNotInstantiateWithoutDotLog(): void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function testWillNotInstantiateWithoutWritterConfig(): void
public function testWillNotInstantiateWithoutWriterConfig(): void
{
$this->container->expects($this->once())
->method('has')
Expand Down Expand Up @@ -96,6 +96,6 @@ public function testWillInstantiate(): void

$factory = (new WriterPluginManagerFactory())($this->container);

$this->assertInstanceOf(WriterPluginManager::class, $factory);
$this->assertSame(WriterPluginManager::class, $factory::class);
}
}
4 changes: 2 additions & 2 deletions test/Filter/LevelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function setUp(): void

public function testWillInstantiateWithInt(): void
{
$this->assertInstanceOf(Level::class, $this->subject);
$this->assertSame(Level::class, $this->subject::class);
}

public function testWillInstantiateWithArray(): void
Expand All @@ -28,7 +28,7 @@ public function testWillInstantiateWithArray(): void

$result = new Level($input);

$this->assertInstanceOf(Level::class, $result);
$this->assertSame(Level::class, $result::class);
}

public function testWillNotInstantiateWithEmptyArray(): void
Expand Down
2 changes: 1 addition & 1 deletion test/Filter/RegexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function setUp(): void

public function testWillInstantiate(): void
{
$this->assertInstanceOf(Regex::class, $this->subject);
$this->assertSame(Regex::class, $this->subject::class);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions test/Filter/SuppressFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function setUp(): void

public function testWillInstantiate(): void
{
$this->assertInstanceOf(SuppressFilter::class, $this->subject);
$this->assertSame(SuppressFilter::class, $this->subject::class);
}

public function testWillNotInstantiate(): void
Expand All @@ -40,6 +40,7 @@ public function testFilter(): void

public function testSuppress(): void
{
$this->assertNull($this->subject->suppress(true));
$this->expectNotToPerformAssertions();
$this->subject->suppress(true);
}
}
Loading

0 comments on commit bd2c03e

Please sign in to comment.