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
12 changes: 11 additions & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ on:
- 'phpunit.xml.dist'

push:
branches:
- master
paths-ignore:
- 'docs/**'
- 'README.md'
Expand All @@ -28,4 +30,12 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.0', '8.1']
['8.0', '8.1', '8.2']
psalm83:
uses: yiisoft/actions/.github/workflows/psalm.yml@master
with:
psalm-config: 'psalm-8.3.xml'
os: >-
['ubuntu-latest']
php: >-
['8.3']
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## 2.0.1 under development

- Bug #84: Change the type of the `$level` parameter in the `Message` constructor to `string` (@dood-)
- New #104: Add new static methods `Logger::assertLevelIsValid()`, `Logger::assertLevelIsString()` and
`Logger::assertLevelIsSupported()` (@vjik)
- Chg #104: Deprecate method `Logger::validateLevel()` (@vjik)

## 2.0.0 May 22, 2022

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"require-dev": {
"maglnet/composer-require-checker": "^4.4",
"phpunit/phpunit": "^9.5",
"rector/rector": "^1.0.0",
"rector/rector": "1.0.*",
"roave/infection-static-analysis-plugin": "^1.18",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.22"
"vimeo/psalm": "^4.30|^5.24"
},
"provide": {
"psr/log-implementation": "1.0.0"
Expand Down
21 changes: 21 additions & 0 deletions psalm-8.3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
<MissingClassConstType errorLevel="suppress" />
</issueHandlers>
</psalm>

3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<MixedAssignment errorLevel="suppress" />
</issueHandlers>
</psalm>

61 changes: 59 additions & 2 deletions src/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function __construct(array $targets = [])
* @throws \Psr\Log\InvalidArgumentException for invalid log message level.
*
* @return string The text display of the level.
* @deprecated since 2.1, to be removed in 3.0. Use {@see LogLevel::assertLevelIsValid()} instead.
*/
public static function validateLevel(mixed $level): string
{
Expand Down Expand Up @@ -146,12 +147,13 @@ public function getTargets(): array
}

/**
* @param string $level
* @psalm-param LogMessageContext $context
* @psalm-suppress MoreSpecificImplementedParamType
* @psalm-suppress MoreSpecificImplementedParamType,MixedArgumentTypeCoercion
*/
public function log(mixed $level, string|Stringable $message, array $context = []): void
{
self::assertLevelIsString($level);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's public I think it's better to call isValid method as well

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not need, because level will be to check in Message constructor further.


$context['time'] ??= microtime(true);
$context['trace'] ??= $this->collectTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
$context['memory'] ??= memory_get_usage();
Expand Down Expand Up @@ -230,6 +232,61 @@ public function setExcludedTracePaths(array $excludedTracePaths): self
return $this;
}

/**
* Asserts that the log message level is valid.
*
* @param mixed $level The message level.
*
* @throws \Psr\Log\InvalidArgumentException When the log message level is not a string or is not supported.
*/
public static function assertLevelIsValid(mixed $level): void
{
self::assertLevelIsString($level);
self::assertLevelIsSupported($level);
}

/**
* Asserts that the log message level is a string.
*
* @param mixed $level The message level.
*
* @throws \Psr\Log\InvalidArgumentException When the log message level is not a string.
*
* @psalm-assert string $level
*/
public static function assertLevelIsString(mixed $level): void
{
if (is_string($level)) {
return;
}

throw new \Psr\Log\InvalidArgumentException(
sprintf('The log message level must be a string, %s provided.', gettype($level))
);
}

/**
* Asserts that the log message level is supported.
*
* @param string $level The message level.
*
* @throws \Psr\Log\InvalidArgumentException When the log message level is not supported.
*/
public static function assertLevelIsSupported(string $level): void
{
if (in_array($level, self::LEVELS, true)) {
return;
}

throw new \Psr\Log\InvalidArgumentException(
sprintf(
'Invalid log message level "%s" provided. The following values are supported: "%s".',
$level,
implode('", "', self::LEVELS)
)
);
}

/**
* Sets a target to {@see Logger::$targets}.
*
Expand Down
3 changes: 2 additions & 1 deletion src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ final class Message
*/
public function __construct(string $level, string|Stringable $message, array $context = [])
{
$this->level = Logger::validateLevel($level);
Logger::assertLevelIsSupported($level);
$this->level = $level;
$this->message = $this->parse($message, $context);
$this->context = $context;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Message/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private function getTrace(Message $message): string
/**
* Converts a value to a string.
*
* @param mixed $value The value to convert
* @param mixed $value The value to convert.
*
* @return string Converted string.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Target.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ public function setExcept(array $except): self
public function setLevels(array $levels): self
{
foreach ($levels as $key => $level) {
$levels[$key] = Logger::validateLevel($level);
Logger::assertLevelIsValid($level);
$levels[$key] = $level;
}

$this->levels = $levels;
Expand Down
2 changes: 0 additions & 2 deletions tests/TargetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ public function invalidCallableEnabledProvider(): array

/**
* @dataProvider invalidCallableEnabledProvider
*
* @param mixed $value
*/
public function testIsEnabledThrowExceptionForCallableReturnNotBoolean(callable $value): void
{
Expand Down