Browse Source

Better checking for callable for messageTransform

main v1.1
Dustin Wilson 12 months ago
parent
commit
2a540cad2b
  1. 2
      README.md
  2. 14
      lib/Logger/Handler.php
  3. 11
      lib/Logger/TypeError.php
  4. 6
      test
  5. 5
      tests/Bootstrap.php
  6. 11
      tests/cases/TestHandler.php

2
README.md

@ -237,7 +237,7 @@ abstract class Handler {
protected array $levels;
protected bool $_bubbles = true;
protected string|\Closure|null $_messageTransform = null;
protected callable $_messageTransform = null;
protected string $_timeFormat = 'M d H:i:s';
public function __construct(array $levels = [ 0, 1, 2, 3, 4, 5, 6, 7 ], array $options = []);

14
lib/Logger/Handler.php

@ -12,7 +12,7 @@ abstract class Handler {
protected array $levels;
protected bool $_bubbles = true;
protected string|\Closure|null $_messageTransform = null;
protected $_messageTransform = null;
protected string $_timeFormat = 'M d H:i:s';
@ -20,14 +20,8 @@ abstract class Handler {
public function __construct(array $levels = [ 0, 1, 2, 3, 4, 5, 6, 7 ], array $options = []) {
$this->levels = $this->verifyLevels($levels);
$class = get_class($this);
foreach ($options as $key => $value) {
$name = "_$key";
if (!property_exists($class, $name)) {
trigger_error(sprintf('Undefined option in %s: %s', $class, $key), \E_USER_WARNING);
continue;
}
$this->$name = $value;
$this->setOption($key, $value);
}
}
@ -59,6 +53,10 @@ abstract class Handler {
return;
}
if ($name === 'messageTransform' && !is_callable($value)) {
throw new TypeError(sprintf('Value of messageTransform option must be callable'));
}
$name = "_$name";
$this->$name = $value;
}

11
lib/Logger/TypeError.php

@ -0,0 +1,11 @@
<?php
/**
* @license MIT
* Copyright 2022 Dustin Wilson, et al.
* See LICENSE and AUTHORS files for details
*/
declare(strict_types=1);
namespace MensBeam\Logger;
class TypeError extends \TypeError {}

6
test

@ -1,5 +1,11 @@
#!/usr/bin/env php
<?php
/**
* @license MIT
* Copyright 2022 Dustin Wilson, et al.
* See LICENSE and AUTHORS files for details
*/
$dir = ini_get('extension_dir');
$php = escapeshellarg(\PHP_BINARY);
$code = escapeshellarg(__DIR__ . '/lib');

5
tests/Bootstrap.php

@ -6,6 +6,5 @@ ini_set('memory_limit', '2G');
ini_set('zend.assertions', '1');
ini_set('assert.exception', 'true');
error_reporting(\E_ALL);
require_once dirname(__DIR__) . '/vendor/autoload.php';
define('CWD', getcwd());
define('CWD', dirname(__DIR__));
require_once CWD . '/vendor/autoload.php';

11
tests/cases/TestHandler.php

@ -13,7 +13,8 @@ use MensBeam\Logger\{
InvalidArgumentException,
Level,
RangeException,
StreamHandler
StreamHandler,
TypeError
};
@ -99,6 +100,14 @@ class TestHandler extends ErrorHandlingTestCase {
public static function provideFatalErrorTests(): iterable {
$iterable = [
[
TypeError::class,
0,
'Value of messageTransform option must be callable',
function (Handler $h): void {
$h->setOption('messageTransform', 42);
}
],
[
InvalidArgumentException::class,
0,

Loading…
Cancel
Save