Skip to content

Commit

Permalink
code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergiu committed Sep 12, 2024
1 parent d2ef1ef commit 7a790a1
Show file tree
Hide file tree
Showing 20 changed files with 29 additions and 65 deletions.
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
],
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"laminas/laminas-filter": "^2.37",
"laminas/laminas-servicemanager": "^3.22",
"laminas/laminas-validator": "^2.64"
},
"require-dev": {
"phpunit/phpunit": "^10.2",
"laminas/laminas-coding-standard": "^2.5",
"vimeo/psalm": "^5.13",
"ext-curl": "*"
"vimeo/psalm": "^5.13"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/Filter/FilterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface FilterInterface
/**
* Returns TRUE to accept the message, FALSE to block it.
*/
public function filter(array $event);
public function filter(array $event): bool|int|null;
}
2 changes: 1 addition & 1 deletion src/Filter/Priority.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function __construct(iterable|int $priority, ?string $operator = null)
/**
* Returns TRUE to accept the message, FALSE to block it.
*/
public function filter(array $event): null|bool|int
public function filter(array $event): bool|int|null
{
return version_compare((string) $event['priority'], (string) $this->priority, $this->operator);
}
Expand Down
12 changes: 6 additions & 6 deletions src/Formatter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ protected function normalize(mixed $value): mixed
if ($value instanceof DateTime) {
$value = $value->format($this->getDateTimeFormat());
} elseif ($value instanceof Traversable) {
$value = @json_encode(iterator_to_array($value), $jsonFlags);
$value = json_encode(iterator_to_array($value), $jsonFlags);
} elseif (is_array($value)) {
$value = @json_encode($value, $jsonFlags);
$value = json_encode($value, $jsonFlags);
} elseif (is_object($value) && ! method_exists($value, '__toString')) {
$value = sprintf('object(%s) %s', $value::class, @json_encode($value));
$value = sprintf('object(%s) %s', $value::class, json_encode($value));
} elseif (is_resource($value)) {
$value = sprintf('resource(%s)', get_resource_type($value));
} elseif (! is_object($value)) {
Expand All @@ -111,17 +111,17 @@ protected function normalize(mixed $value): mixed
/**
* {@inheritDoc}
*/
public function getDateTimeFormat()
public function getDateTimeFormat(): string
{
return $this->dateTimeFormat;
}

/**
* {@inheritDoc}
*/
public function setDateTimeFormat($dateTimeFormat): static
public function setDateTimeFormat(string $dateTimeFormat): static
{
$this->dateTimeFormat = (string) $dateTimeFormat;
$this->dateTimeFormat = $dateTimeFormat;
return $this;
}
}
4 changes: 2 additions & 2 deletions src/Formatter/FormatterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function format(iterable $event): iterable|string;
/**
* Get the format specifier for DateTime objects
*/
public function getDateTimeFormat();
public function getDateTimeFormat(): string;

/**
* Set the format specifier for DateTime objects
*
* @see http://php.net/manual/en/function.date.php
*/
public function setDateTimeFormat(string $dateTimeFormat);
public function setDateTimeFormat(string $dateTimeFormat): static;
}
6 changes: 3 additions & 3 deletions src/Formatter/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function format(iterable $event): string
$event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat());
}

return @json_encode(
return json_encode(
$event,
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK | JSON_PRESERVE_ZERO_FRACTION
);
Expand All @@ -50,9 +50,9 @@ public function getDateTimeFormat(): string
/**
* {@inheritDoc}
*/
public function setDateTimeFormat($dateTimeFormat): static
public function setDateTimeFormat(string $dateTimeFormat): static
{
$this->dateTimeFormat = (string) $dateTimeFormat;
$this->dateTimeFormat = $dateTimeFormat;
return $this;
}
}
6 changes: 0 additions & 6 deletions src/Manager/FilterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Psr\Container\ContainerInterface;

use function gettype;
use function is_object;
Expand Down Expand Up @@ -51,11 +50,6 @@ class FilterPluginManager extends AbstractPluginManager
*/
protected $sharedByDefault = false;

public function __construct(ContainerInterface $container, array $config = [])
{
parent::__construct($container, $config);
}

/**
* Validate the plugin is of the expected type.
*
Expand Down
6 changes: 0 additions & 6 deletions src/Manager/FormatterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Psr\Container\ContainerInterface;

use function gettype;
use function is_object;
Expand Down Expand Up @@ -41,11 +40,6 @@ class FormatterPluginManager extends AbstractPluginManager
*/
protected $sharedByDefault = false;

public function __construct(ContainerInterface $container, array $config = [])
{
parent::__construct($container, $config);
}

/**
* Validate the plugin is of the expected type.
*
Expand Down
6 changes: 0 additions & 6 deletions src/Manager/ProcessorPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Psr\Container\ContainerInterface;

use function gettype;
use function is_object;
Expand Down Expand Up @@ -50,11 +49,6 @@ class ProcessorPluginManager extends AbstractPluginManager
*/
protected $sharedByDefault = false;

public function __construct(ContainerInterface $container, array $config = [])
{
parent::__construct($container, $config);
}

/**
* Validate the plugin is of the expected type.
*
Expand Down
6 changes: 0 additions & 6 deletions src/Manager/WriterPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Dot\Log\Writer\WriterInterface;
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\Exception\InvalidServiceException;
use Psr\Container\ContainerInterface;

use function gettype;
use function is_object;
Expand Down Expand Up @@ -49,11 +48,6 @@ class WriterPluginManager extends AbstractPluginManager
*/
protected $sharedByDefault = false;

public function __construct(ContainerInterface $container, array $config = [])
{
parent::__construct($container, $config);
}

/**
* Validate the plugin is of the expected type.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Processor/ProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ interface ProcessorInterface
/**
* Processes a log message before it is given to the writers
*/
public function process(array $event);
public function process(array $event): array;
}
2 changes: 1 addition & 1 deletion src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function setConvertWriteErrorsToExceptions(bool $convertErrors): void
/**
* Perform shutdown activities such as closing open resources
*/
public function shutdown()
public function shutdown(): void
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/Writer/WriterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public function setFormatter(FormatterInterface|string $formatter): WriterInterf
/**
* Write a log message
*/
public function write(array $event);
public function write(array $event): void;

/**
* Perform shutdown activities
*/
public function shutdown();
public function shutdown(): void;
}
2 changes: 1 addition & 1 deletion test/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testDependenciesHasAliases()
$this->assertArrayHasKey(WriterPluginManager::class, $this->config['dependencies']['aliases']);
}

public function testDependenciesHasAbstractFactories()
public function testDependenciesHasAbstractFactories(): void
{
$this->assertArrayHasKey('abstract_factories', $this->config['dependencies']);
}
Expand Down
2 changes: 1 addition & 1 deletion test/Factory/LoggerAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected function setUp(): void
/**
* @throws ContainerExceptionInterface
*/
public function testWillInstantiate()
public function testWillInstantiate(): void
{
$this->container
->method('has')
Expand Down
2 changes: 0 additions & 2 deletions test/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,6 @@ public static function provideTestFilters(): array
['regex', ['regex' => '/[0-9]+/']],
];

// Conditionally enabled until laminas-validator is forwards-compatible
// with laminas-servicemanager v3.
if (class_exists(Digits::class)) {
$data[] = ['validator', ['validator' => new Digits()]];
}
Expand Down
12 changes: 3 additions & 9 deletions test/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ class Mock extends AbstractWriter
{
/**
* array of log events
*
* @var array
*/
public $events = [];
public array $events = [];

/**
* shutdown called?
*
* @var bool
*/
public $shutdown = false;
public bool $shutdown = false;

/**
* Write a message to the log.
Expand All @@ -32,10 +28,8 @@ public function doWrite(array $event): void

/**
* Record shutdown
*
* @return void
*/
public function shutdown()
public function shutdown(): void
{
$this->shutdown = true;
}
Expand Down
4 changes: 1 addition & 3 deletions test/MockFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ class MockFilter implements FilterInterface
{
/**
* array of log events
*
* @var array
*/
public $events = [];
public array $events = [];

/**
* Returns TRUE to accept the message
Expand Down
6 changes: 3 additions & 3 deletions test/Processor/BackTraceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ protected function setUp(): void
$this->subject = new Backtrace();
}

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

public function testProcess()
public function testProcess(): void
{
$result = $this->subject->process([]);
$this->assertArrayHasKey('extra', $result);
}

public function testGetIgnoredNamespaces()
public function testGetIgnoredNamespaces(): void
{
$result = $this->subject->getIgnoredNamespaces();
$this->assertIsArray($result);
Expand Down
4 changes: 2 additions & 2 deletions test/Writer/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Dot\Log\Writer\Stream;
use ErrorException;
use PHPUnit\Framework\TestCase;
use stdClass;

use function curl_init;
use function fopen;

class StreamTest extends TestCase
Expand Down Expand Up @@ -59,7 +59,7 @@ public function testWillNotInstantiateWithWrongResource(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Resource is not a stream nor a string; received "object"');
new Stream(['stream' => curl_init('url')]);
new Stream(['stream' => new stdClass()]);
}

/**
Expand Down

0 comments on commit 7a790a1

Please sign in to comment.