Skip to content
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

Add return types for v3 #77

Merged
merged 1 commit into from
Jul 14, 2021
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.0.x-dev"
"dev-master": "3.x-dev"
}
}
}
2 changes: 1 addition & 1 deletion src/LoggerAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ interface LoggerAwareInterface
*
* @return void
*/
public function setLogger(LoggerInterface $logger);
public function setLogger(LoggerInterface $logger): void;
}
2 changes: 1 addition & 1 deletion src/LoggerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ trait LoggerAwareTrait
*
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}
Expand Down
18 changes: 9 additions & 9 deletions src/LoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface LoggerInterface
*
* @return void
*/
public function emergency(string|\Stringable $message, array $context = []);
public function emergency(string|\Stringable $message, array $context = []): void;
Copy link

Choose a reason for hiding this comment

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

Hi @Crell,
It would be way more useful to apply basic types and return types in let's say v2 to cover our needs in php 7.4.
As you mention Stringable interface is introduce in php 8, so we with v7.4 are out of luck to use types where we could:

  • return type
  • ?LoggerInterface $logger = null; in LoggerAwareTrait.

If you can publish v2 for php7.x without Stringable it would be awesome,
and keep v3 for php8.

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

A child class is allowed to widen argument types (on PHP 7.2+, you can remove it, and on PHP 7.4+ you have full variance rules). So you can implement your method as public function emergency($message, array $context = []): void and having your implementation compatible with interfaces v1, v2 and v3 on PHP 7.2+

Copy link

Choose a reason for hiding this comment

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

I did similar thing. But isn't the goal of this to abstract/extract/unify implementations so we don't have to do it one by one?
The version selector could do that for us, as said if you publish v2.x as PHP7.4 compatible, just without Stringable.

But anyway, thanks for the response.

Copy link
Contributor

Choose a reason for hiding this comment

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

@vukanac implementations are either expected to implement exactly the PSR interface of a single supported version of psr/log or to be nice to their user regarding dependency conflicts by relying on PHP variance rules (available in PHP 7.2+) to write an implementation compatible with multiple versions of psr/log. There is no reason for psr/log to publish another version of the interface supporting PHP 7.x (which cannot be 2.x as there is already a published 2.0 signature and changing it would be a BC break). If you want to keep compatibility with PHP 7.4, keep support for psr/log 1


/**
* Action must be taken immediately.
Expand All @@ -40,7 +40,7 @@ public function emergency(string|\Stringable $message, array $context = []);
*
* @return void
*/
public function alert(string|\Stringable $message, array $context = []);
public function alert(string|\Stringable $message, array $context = []): void;

/**
* Critical conditions.
Expand All @@ -52,7 +52,7 @@ public function alert(string|\Stringable $message, array $context = []);
*
* @return void
*/
public function critical(string|\Stringable $message, array $context = []);
public function critical(string|\Stringable $message, array $context = []): void;

/**
* Runtime errors that do not require immediate action but should typically
Expand All @@ -63,7 +63,7 @@ public function critical(string|\Stringable $message, array $context = []);
*
* @return void
*/
public function error(string|\Stringable $message, array $context = []);
public function error(string|\Stringable $message, array $context = []): void;

/**
* Exceptional occurrences that are not errors.
Expand All @@ -76,7 +76,7 @@ public function error(string|\Stringable $message, array $context = []);
*
* @return void
*/
public function warning(string|\Stringable $message, array $context = []);
public function warning(string|\Stringable $message, array $context = []): void;

/**
* Normal but significant events.
Expand All @@ -86,7 +86,7 @@ public function warning(string|\Stringable $message, array $context = []);
*
* @return void
*/
public function notice(string|\Stringable $message, array $context = []);
public function notice(string|\Stringable $message, array $context = []): void;

/**
* Interesting events.
Expand All @@ -98,7 +98,7 @@ public function notice(string|\Stringable $message, array $context = []);
*
* @return void
*/
public function info(string|\Stringable $message, array $context = []);
public function info(string|\Stringable $message, array $context = []): void;

/**
* Detailed debug information.
Expand All @@ -108,7 +108,7 @@ public function info(string|\Stringable $message, array $context = []);
*
* @return void
*/
public function debug(string|\Stringable $message, array $context = []);
public function debug(string|\Stringable $message, array $context = []): void;

/**
* Logs with an arbitrary level.
Expand All @@ -121,5 +121,5 @@ public function debug(string|\Stringable $message, array $context = []);
*
* @throws \Psr\Log\InvalidArgumentException
*/
public function log($level, string|\Stringable $message, array $context = []);
public function log($level, string|\Stringable $message, array $context = []): void;
}
18 changes: 9 additions & 9 deletions src/LoggerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait LoggerTrait
*
* @return void
*/
public function emergency(string|\Stringable $message, array $context = [])
public function emergency(string|\Stringable $message, array $context = []): void
{
$this->log(LogLevel::EMERGENCY, $message, $context);
}
Expand All @@ -36,7 +36,7 @@ public function emergency(string|\Stringable $message, array $context = [])
*
* @return void
*/
public function alert(string|\Stringable $message, array $context = [])
public function alert(string|\Stringable $message, array $context = []): void
{
$this->log(LogLevel::ALERT, $message, $context);
}
Expand All @@ -51,7 +51,7 @@ public function alert(string|\Stringable $message, array $context = [])
*
* @return void
*/
public function critical(string|\Stringable $message, array $context = [])
public function critical(string|\Stringable $message, array $context = []): void
{
$this->log(LogLevel::CRITICAL, $message, $context);
}
Expand All @@ -65,7 +65,7 @@ public function critical(string|\Stringable $message, array $context = [])
*
* @return void
*/
public function error(string|\Stringable $message, array $context = [])
public function error(string|\Stringable $message, array $context = []): void
{
$this->log(LogLevel::ERROR, $message, $context);
}
Expand All @@ -81,7 +81,7 @@ public function error(string|\Stringable $message, array $context = [])
*
* @return void
*/
public function warning(string|\Stringable $message, array $context = [])
public function warning(string|\Stringable $message, array $context = []): void
{
$this->log(LogLevel::WARNING, $message, $context);
}
Expand All @@ -94,7 +94,7 @@ public function warning(string|\Stringable $message, array $context = [])
*
* @return void
*/
public function notice(string|\Stringable $message, array $context = [])
public function notice(string|\Stringable $message, array $context = []): void
{
$this->log(LogLevel::NOTICE, $message, $context);
}
Expand All @@ -109,7 +109,7 @@ public function notice(string|\Stringable $message, array $context = [])
*
* @return void
*/
public function info(string|\Stringable $message, array $context = [])
public function info(string|\Stringable $message, array $context = []): void
{
$this->log(LogLevel::INFO, $message, $context);
}
Expand All @@ -122,7 +122,7 @@ public function info(string|\Stringable $message, array $context = [])
*
* @return void
*/
public function debug(string|\Stringable $message, array $context = [])
public function debug(string|\Stringable $message, array $context = []): void
{
$this->log(LogLevel::DEBUG, $message, $context);
}
Expand All @@ -138,5 +138,5 @@ public function debug(string|\Stringable $message, array $context = [])
*
* @throws \Psr\Log\InvalidArgumentException
*/
abstract public function log($level, string|\Stringable $message, array $context = []);
abstract public function log($level, string|\Stringable $message, array $context = []): void;
}
2 changes: 1 addition & 1 deletion src/NullLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NullLogger extends AbstractLogger
*
* @throws \Psr\Log\InvalidArgumentException
*/
public function log($level, string|\Stringable $message, array $context = [])
public function log($level, string|\Stringable $message, array $context = []): void
{
// noop
}
Expand Down