-
Notifications
You must be signed in to change notification settings - Fork 6
Allow mixed types for $level and normalize them in case non-int|string levels are passed in #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
cc @Bilge @derrabus @MidnightDesign @sasezaki any thoughts here? |
src/Test/TestLogger.php
Outdated
| /** | ||
| * @param mixed $level | ||
| */ | ||
| private function normalizeLevel($level): int|string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be static.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Marked static and protected as well because #10 shows this might come in handy
src/Test/TestLogger.php
Outdated
| $message = $this->interpolate($message, $context); | ||
| } | ||
|
|
||
| $level = $this->normalizeLevel($level); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a breaking change though..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I get it. The $recordsByLevel index could never have worked with anything else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I don't believe this carries much risk as it was broken already per #10
src/Test/TestLogger.php
Outdated
| * | ||
| * @psalm-type log_record_array array{level: string, message: string|\Stringable, context: mixed[]} | ||
| * @psalm-type has_record_array array{level?: string, message?: string|\Stringable, context?: array<array-key, mixed>} | ||
| * @psalm-type log_record_array array{level: mixed, message: string|\Stringable, context: mixed[]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're normalizing it to string|int, this can also be string|int, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, normalization means exactly "input may be everything (mixed) and output is string|int".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe I'm not understanding correctly, but what I'm saying is: the log_record_array is only used on $records and $recordsByLevel - and both of them can never have anything other than string|int in the level offset. At least not after this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes for this type @MidnightDesign is correct IMO, I have changed it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The $level of the query methods can be kept a bit narrower: string|int|Stringable|UnitEnum. They can't respond with true to anything else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
Seems fine. |
src/Test/TestLogger.php
Outdated
| /** | ||
| * @param mixed $level | ||
| */ | ||
| protected static function normalizeLevel($level): int|string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get that exposing this to extending classes is on purpose, so that they can implement custom normalization behavior, but is that really worth increasing the API surface?
Also, if this behavior should be allowed to be modified, wouldn't composition (optionally passing in some kind of normalizer) be better than the inheritance route?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
composition for a class that has so many public methods is a pain IMO if all you want to do is add normalization for a custom logger..
But maybe we're better off leaving this private until a real use case materializes, because enum+stringable support should already cover most use cases out there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're still calling normalizeLevel() with $this-> in multiple places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right 🙈
derrabus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
|
Alright then, thanks everyone. I'll wrap up a release to get all this out. |
| /** | ||
| * @param mixed $level | ||
| */ | ||
| private static function normalizeLevel($level): int|string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| /** | |
| * @param mixed $level | |
| */ | |
| private static function normalizeLevel($level): int|string | |
| private static function normalizeLevel(mixed $level): int|string |
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I thought this was from php8.1 somehow.. Not that huge a deal anyway
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes #10
Fixes #22