Skip to content

Commit

Permalink
Fix default values via reflection for LoggerInterface (PHP 8 only)
Browse files Browse the repository at this point in the history
Using ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE

See #78
  • Loading branch information
jbboehr committed Nov 5, 2020
1 parent 5be3030 commit 68663e1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
### Fixed
- Test failures on PHP 8.0.0alpha1
- Compile failures with clang
- Report proper default value in reflection for LoggerInterface - PHP 8 only ([GH-78](https://github.com/jbboehr/php-psr/issues/78))

## [1.0.0] - 2020-02-18

Expand Down
24 changes: 15 additions & 9 deletions psr_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,56 @@ extern PHP_PSR_API zend_class_entry * PsrLogLoggerAwareTrait_ce_ptr;

extern PHP_MINIT_FUNCTION(psr_log);

#if PHP_VERSION_ID < 80000
#define PHP_PSR_CONTEXT_PARAM ZEND_ARG_ARRAY_INFO(0, context, 0)
#else
#define PHP_PSR_CONTEXT_PARAM ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, context, IS_ARRAY, 0, "[]")
#endif

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerInterface, emergency, 1)
ZEND_ARG_INFO(0, message)
ZEND_ARG_ARRAY_INFO(0, context, 0)
PHP_PSR_CONTEXT_PARAM
PHP_PSR_END_ARG_INFO()

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerInterface, alert, 1)
ZEND_ARG_INFO(0, message)
ZEND_ARG_ARRAY_INFO(0, context, 0)
PHP_PSR_CONTEXT_PARAM
PHP_PSR_END_ARG_INFO()

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerInterface, critical, 1)
ZEND_ARG_INFO(0, message)
ZEND_ARG_ARRAY_INFO(0, context, 0)
PHP_PSR_CONTEXT_PARAM
PHP_PSR_END_ARG_INFO()

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerInterface, error, 1)
ZEND_ARG_INFO(0, message)
ZEND_ARG_ARRAY_INFO(0, context, 0)
PHP_PSR_CONTEXT_PARAM
PHP_PSR_END_ARG_INFO()

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerInterface, warning, 1)
ZEND_ARG_INFO(0, message)
ZEND_ARG_ARRAY_INFO(0, context, 0)
PHP_PSR_CONTEXT_PARAM
PHP_PSR_END_ARG_INFO()

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerInterface, notice, 1)
ZEND_ARG_INFO(0, message)
ZEND_ARG_ARRAY_INFO(0, context, 0)
PHP_PSR_CONTEXT_PARAM
PHP_PSR_END_ARG_INFO()

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerInterface, info, 1)
ZEND_ARG_INFO(0, message)
ZEND_ARG_ARRAY_INFO(0, context, 0)
PHP_PSR_CONTEXT_PARAM
PHP_PSR_END_ARG_INFO()

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerInterface, debug, 1)
ZEND_ARG_INFO(0, message)
ZEND_ARG_ARRAY_INFO(0, context, 0)
PHP_PSR_CONTEXT_PARAM
PHP_PSR_END_ARG_INFO()

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerInterface, log, 2)
ZEND_ARG_INFO(0, level)
ZEND_ARG_INFO(0, message)
ZEND_ARG_ARRAY_INFO(0, context, 0)
PHP_PSR_CONTEXT_PARAM
PHP_PSR_END_ARG_INFO()

PHP_PSR_BEGIN_ARG_INFO(PsrLogLoggerAwareInterface, setLogger, 1)
Expand Down
23 changes: 23 additions & 0 deletions tests/gh78_php7.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Psr\Log\LoggerInterface - Invalid context default value (PHP 7)
--SKIPIF--
<?php
include 'skip.inc';
if (PHP_VERSION_ID >= 80000) {
print "skip Due to version incompatibility";
}
?>
--FILE--
<?php
$reflectionMethod = new \ReflectionMethod(\Psr\Log\LoggerInterface::class, "emergency");
$reflectionParameter = $reflectionMethod->getParameters()[1];
var_dump($reflectionParameter->isOptional());
var_dump($reflectionParameter->isDefaultValueAvailable());
var_dump($reflectionParameter->getDefaultValue());
--EXPECT--
bool(true)
bool(true)
array(0) {
}
--XFAIL--
PHP 7 internal functions cannot have default values reported via reflection
21 changes: 21 additions & 0 deletions tests/gh78_php8.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
Psr\Log\LoggerInterface - Invalid context default value (PHP 8)
--SKIPIF--
<?php
include 'skip.inc';
if (PHP_VERSION_ID < 80000) {
print "skip Due to version incompatibility";
}
?>
--FILE--
<?php
$reflectionMethod = new \ReflectionMethod(\Psr\Log\LoggerInterface::class, "emergency");
$reflectionParameter = $reflectionMethod->getParameters()[1];
var_dump($reflectionParameter->isOptional());
var_dump($reflectionParameter->isDefaultValueAvailable());
var_dump($reflectionParameter->getDefaultValue());
--EXPECT--
bool(true)
bool(true)
array(0) {
}

0 comments on commit 68663e1

Please sign in to comment.