Skip to content

Commit

Permalink
monolog collector trait regression / improvements to AssertSettingTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdotcom committed Oct 11, 2023
1 parent b69cf73 commit 5dabf3d
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 33 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"doctrine/dbal": "^2.5.0",
"guzzlehttp/psr7": "^1.6",
"jdorn/sql-formatter": "^1.2",
"monolog/monolog": "^1.0",
"monolog/monolog": "^1.0 | ^2.0 | ^3.0",
"php-curl-class/php-curl-class": ">=8.6",
"phpunit/phpunit": "^4.0 | ^5.0 | ^6.0 | ^7.0 | ^8.0 | ^9.0",
"psr/log": "^1.0 | ^2.0 | ^3.0",
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/MonologHandlerCompatTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

if (\method_exists($refMethod, 'hasReturnType') && $refMethod->hasReturnType()) {
require __DIR__ . '/MonologHandlerCompatTrait_return.php';
} elseif (\trait_exists(__NAMESPACE__ . '\\MethodSignatureCompatTrait', false) === false) {
} elseif (\trait_exists(__NAMESPACE__ . '\\MonologHandlerCompatTrait', false) === false) {
trait MonologHandlerCompatTrait
{
/**
Expand Down
32 changes: 19 additions & 13 deletions src/Debug/Collector/MonologHandlerCompatTrait_return.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@

namespace bdk\Debug\Collector;

/**
* Provide handle method (with return type-hint)
*/
trait MonologHandlerCompatTrait
{
/*
Wrap in condition.
PHPUnit code coverage scans all files and will conflict
*/
if (\trait_exists(__NAMESPACE__ . '\\MonologHandlerCompatTrait', false) === false) {
/**
* Handles a record.
*
* @param array $record The record to handle
*
* @return bool true means that this handler handled the record, and that bubbling is not permitted.
* false means the record was either not processed or that this handler allows bubbling.
* Provide handle method (with return type-hint)
*/
public function handle(array $record): bool
trait MonologHandlerCompatTrait
{
return $this->doHandle($record);
/**
* Handles a record.
*
* @param array $record The record to handle
*
* @return bool true means that this handler handled the record, and that bubbling is not permitted.
* false means the record was either not processed or that this handler allows bubbling.
*/
public function handle(array $record): bool
{
return $this->doHandle($record);
}
}
}
53 changes: 40 additions & 13 deletions src/Debug/Plugin/AssertSettingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace bdk\Debug\Plugin;

use bdk\Debug;

/**
* Provite assertSetting method
*/
Expand All @@ -28,18 +30,15 @@ protected function assertSetting($setting)
{
$setting = $this->assertSettingPrep(\array_merge(array(
'addParams' => array(),
'filter' => FILTER_VALIDATE_BOOLEAN,
'filter' => FILTER_DEFAULT, // filter applied if getting value from ini val
'msg' => '', // (optional) message displayed if assertion fails
'name' => '', // ini name
'operator' => '==',
'valActual' => '__use_ini_val__',
'valCompare' => true,
), $setting));
$assert = \is_array($setting['valCompare'])
? \in_array($setting['valActual'], $setting['valCompare'], true)
: $this->debug->stringUtil->compare($setting['valActual'], $setting['valCompare'], $setting['operator']);
$params = array(
$assert,
$this->debug->stringUtil->compare($setting['valActual'], $setting['valCompare'], $setting['operator']),
$setting['name']
? '%c' . $setting['name'] . '%c: ' . $setting['msg']
: $setting['msg'],
Expand All @@ -62,18 +61,46 @@ protected function assertSetting($setting)
*/
private function assertSettingPrep($setting)
{
if (\is_bool($setting['valCompare'])) {
$setting['filter'] = FILTER_VALIDATE_BOOLEAN;
} elseif (\is_int($setting['valCompare'])) {
$setting['filter'] = FILTER_VALIDATE_INT;
}
if ($setting['valActual'] === '__use_ini_val__') {
$setting['valActual'] = \filter_var(\ini_get($setting['name']), $setting['filter']);
}
$valFriendly = $setting['filter'] === FILTER_VALIDATE_BOOLEAN
? ($setting['valCompare'] ? 'enabled' : 'disabled')
: $setting['valCompare'];
if (!$setting['msg']) {
$msgDefault = $setting['operator'] === '=='
? 'should be ' . $valFriendly
: 'should not be ' . $valFriendly;
$setting['msg'] = $msgDefault;
if ($setting['msg']) {
return $setting;
}
$valFriendly = $this->valFriendly($setting);
$setting['msg'] = \sprintf(
'%s %s',
$setting['operator'] === '==' ? 'should be' : 'should not be',
$valFriendly
);
if (\substr($valFriendly, 0, 1) === '<') {
$setting['addParams'][] = Debug::meta('sanitize', false);
}
return $setting;
}

/**
* Get the friendly expected / not-expected value for default message
*
* @param array $setting setting options
*
* @return string
*/
private function valFriendly(array $setting)
{
if ($setting['filter'] === FILTER_VALIDATE_BOOLEAN) {
return $setting['valCompare'] ? 'enabled' : 'disabled';
}
if ($setting['valCompare'] === '') {
return 'empty';
}
return \is_string($setting['valCompare'])
? '<span class="t_string">' . \htmlspecialchars($setting['valCompare']) . '</span>'
: $setting['valCompare'];
}
}
2 changes: 0 additions & 2 deletions src/Debug/Plugin/LogEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,10 @@ private function logSessionSettings($namePassed)
$settings = array(
array('name' => 'session.cookie_httponly'),
array(
'filter' => FILTER_VALIDATE_INT,
'name' => 'session.cookie_lifetime',
'valCompare' => 0,
),
array(
'filter' => FILTER_DEFAULT,
'msg' => 'should not be PHPSESSID (just as %cexpose_php%c should be disabled)',
'name' => 'session.name',
'operator' => '!=',
Expand Down
16 changes: 14 additions & 2 deletions src/Debug/Plugin/LogPhp.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ protected function logPhpInfo()
'name' => 'expose_php',
'valCompare' => false,
));
$this->assertSetting(array(
'name' => 'default_charset',
'operator' => '!=',
'valCompare' => '',
));
$this->assertExtensions();
$this->logXdebug();
}
Expand Down Expand Up @@ -167,11 +172,18 @@ private function assertExtensions()
);
}
$this->assertSetting(array(
'filter' => FILTER_VALIDATE_INT,
'msg' => 'Multibyte string function overloading is enabled (is evil)',
'name' => 'mbstring.func_overload',
'valCompare' => array(0, false),
'valCompare' => false,
));
if (PHP_VERSION_ID < 50600) {
// default_charset should be used for php >= 5.6
$this->assertSetting(array(
'name' => 'mb_string.internal_encoding',
'operator' => '!=',
'valCompare' => '',
));
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Utility/StringUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ private static function compareTypeJuggle($valA, $valB)
* @param mixed $valB Value B
* @param string $operator (strcmp) Comparison operator
*
* @return bool
* @return bool|int
*/
private static function doCompare($valA, $valB, $operator)
{
Expand Down

0 comments on commit 5dabf3d

Please sign in to comment.