Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/code-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ jobs:
strategy:
matrix:
php:
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
Expand Down
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/vendor
/vendor/
/.phpunit.cache/
/composer.lock
/phpunit.xml
/.phpunit.result.cache
2 changes: 1 addition & 1 deletion .runConfigurations/All unit tests.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All unit tests" type="PHPUnitRunConfigurationType" factoryName="PHPUnit">
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" scope="XML" options="--verbose" />
<TestRunner configuration_file="$PROJECT_DIR$/phpunit.xml.dist" scope="XML" />
<method v="2" />
</configuration>
</component>
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Type declarations for parameters and return types.
### Removed
- **BC break**: Removed support for PHP versions <= v8.0 as they are no longer
[actively supported](https://php.net/supported-versions.php) by the PHP project.

## [4.1.2] - 2025-01-30
### Changed
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@
}
],
"require": {
"php": "^7.3|^8",
"php": "^8.1",
"ext-json": "*",
"psr/log": "^1 || ^2 || ^3",
"psr/log": "^2 || ^3",
"graylog2/gelf-php": "^1.3",
"symfony/console": "^2.5.12|^3|^4|^5 || ^6 || ^7"
"symfony/console": "^6 || ^7"
},
"autoload": {
"psr-4": {
"Phlib\\Logger\\": "src"
}
},
"provide": {
"psr/log-implementation": "1.0 || 2.0 || 3.0"
"psr/log-implementation": "2.0 || 3.0"
},
"require-dev": {
"phpunit/phpunit": "^9",
"phpunit/phpunit": "^10",
"symplify/easy-coding-standard": "^12"
},
"autoload-dev": {
Expand Down
7 changes: 4 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache"
colors="true"
>
<testsuites>
<testsuite name="Phlib Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</source>
</phpunit>
11 changes: 3 additions & 8 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@
*/
class Config implements ConfigInterface
{
/**
* @var array
*/
protected $config;

public function __construct(array $config)
{
$this->config = $config;
public function __construct(
protected array $config,
) {
}

public function getLoggerConfig(string $name): array
Expand Down
14 changes: 4 additions & 10 deletions src/Decorator/AbstractDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@
*/
abstract class AbstractDecorator extends AbstractLogger
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* Stores the Logger for re-use in the concrete via getInnerLogger()
*
* If the concrete requires use of the config value, override the constructor
* to add validation and store for re-use
*
* @param mixed $config
*/
public function __construct(LoggerInterface $logger, $config)
{
$this->logger = $logger;
public function __construct(
private readonly LoggerInterface $logger,
mixed $config,
) {
}

protected function getInnerLogger(): LoggerInterface
Expand Down
5 changes: 1 addition & 4 deletions src/Decorator/DefaultContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
*/
class DefaultContext extends AbstractDecorator
{
/**
* @var array
*/
private $decorations;
private readonly array $decorations;

public function __construct(LoggerInterface $logger, array $decorations)
{
Expand Down
12 changes: 5 additions & 7 deletions src/Decorator/LevelFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class LevelFilter extends AbstractDecorator
*
* @var string[] Logging levels
*/
private static $levels = [
private static array $levels = [
LogLevel::EMERGENCY, // 0
LogLevel::ALERT, // 1
LogLevel::CRITICAL, // 2
Expand All @@ -29,24 +29,22 @@ class LevelFilter extends AbstractDecorator
LogLevel::DEBUG, // 7
];

/**
* @var int
*/
private $logLevel;
private readonly int $logLevel;

public function __construct(LoggerInterface $logger, string $level)
{
parent::__construct($logger, $level);

$this->logLevel = array_search($level, self::$levels, true);
if ($this->logLevel === false) {
$logLevel = array_search($level, self::$levels, true);
if ($logLevel === false) {
throw new InvalidArgumentException(
sprintf(
'Cannot use logging level "%s"',
$level
)
);
}
$this->logLevel = $logLevel;
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
*/
class Factory
{
/**
* @var array
*/
private $decorators = [
private array $decorators = [
'defaultContext' => \Phlib\Logger\Decorator\DefaultContext::class,
'level' => \Phlib\Logger\Decorator\LevelFilter::class,
];
Expand Down Expand Up @@ -58,9 +55,7 @@ public function createLogger(string $name, array $config): LoggerInterface
}
$logger = $this->{$methodName}($name, $config);

$logger = $this->applyDecorators($logger, $config);

return $logger;
return $this->applyDecorators($logger, $config);
}

private function applyDecorators(LoggerInterface $logger, array $config): LoggerInterface
Expand Down
11 changes: 2 additions & 9 deletions src/LoggerType/CliColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
*/
class CliColor extends Stream
{
/**
* @var OutputFormatter
*/
private $formatter;
private readonly OutputFormatter $formatter;

/**
* @see Stream::__construct()
Expand All @@ -39,11 +36,7 @@ public function __construct(string $name, $stream = STDERR)
]);
}

/**
* @see Stream::getMessageFormat()
* @param mixed $level
*/
protected function getMessageFormat($level, array $context = []): string
protected function getMessageFormat(mixed $level, array $context = []): string
{
$parentFormat = parent::getMessageFormat($level, $context);
$consoleFormat = "<{$level}>{$parentFormat}</{$level}>";
Expand Down
5 changes: 1 addition & 4 deletions src/LoggerType/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
*/
class Collection extends AbstractLogger
{
/**
* @var \SplObjectStorage
*/
protected $loggerInstances;
protected \SplObjectStorage $loggerInstances;

public function __construct()
{
Expand Down
34 changes: 8 additions & 26 deletions src/LoggerType/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@
*/
class Stream extends AbstractLogger
{
/**
* @var string
*/
private $name;
private readonly string $name;

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

/**
* @var string
*/
private $messageFormat = '[{datetime}] {name}.{level}: {message} {context}';
private string $messageFormat = '[{datetime}] {name}.{level}: {message} {context}';

/**
* @var string
*/
private $dateFormat = 'Y-m-d H:i:s';
private string $dateFormat = 'Y-m-d H:i:s';

/**
* @param resource|string $stream
Expand Down Expand Up @@ -58,10 +49,8 @@ public function setMessageFormat(string $format): self
* Get current message format
*
* This method can be overridden by extending classes to modify the behaviour
*
* @param mixed $level
*/
protected function getMessageFormat($level, array $context = []): string
protected function getMessageFormat(mixed $level, array $context = []): string
{
return $this->messageFormat;
}
Expand All @@ -73,11 +62,7 @@ public function setDateFormat(string $format): self
return $this;
}

/**
* @param mixed $level
* @param string|\Stringable $message
*/
public function log($level, $message, array $context = []): void
public function log(mixed $level, string|\Stringable $message, array $context = []): void
{
$meta = [
'datetime' => date($this->dateFormat),
Expand All @@ -95,9 +80,8 @@ public function log($level, $message, array $context = []): void
protected function formatMessage(string $message, array $context): string
{
$message = static::interpolate($message, $context);
$message = trim(str_replace(["\r", "\n"], ' ', $message));

return $message;
return trim(str_replace(["\r", "\n"], ' ', $message));
}

protected function formatContext(array $context): string
Expand All @@ -117,10 +101,8 @@ protected function formatContext(array $context): string
*
* Expanded on reference implementation to include handling for complex types,
* trying to ensure no error, warning or notice is happening
*
* @param mixed $message
*/
private static function interpolate($message, array $context): string
private static function interpolate(string $message, array $context): string
{
// build a replacement array with braces around the context keys
$replace = [];
Expand Down Expand Up @@ -151,7 +133,7 @@ private static function contextValueToString($val): string
if (is_callable([$val, '__toString'])) {
return (string)$val;
}
return get_class($val);
return $val::class;
}
return gettype($val);
}
Expand Down
25 changes: 6 additions & 19 deletions src/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,17 @@
*/
class Pool
{
/**
* @var ConfigInterface
*/
protected $config;

/**
* @var string
*/
protected $prefix = '';
protected string $prefix = '';

/**
* @var LoggerInterface[]
*/
protected $loggerInstances = [];
protected array $loggerInstances = [];

/**
* @var Factory
*/
protected $loggerFactory;

public function __construct(ConfigInterface $config, Factory $loggerFactory)
{
$this->config = $config;
$this->loggerFactory = $loggerFactory;
public function __construct(
protected ConfigInterface $config,
protected Factory $loggerFactory,
) {
}

public function setPrefix(string $prefix): self
Expand Down
Loading