Skip to content

Instantly share code, notes, and snippets.

@jehoshua02
Last active December 12, 2015 08:59
Show Gist options
  • Select an option

  • Save jehoshua02/4747839 to your computer and use it in GitHub Desktop.

Select an option

Save jehoshua02/4747839 to your computer and use it in GitHub Desktop.
Decompose PHP error reporting level. I see uncommented error reporting integers in my apache configs and have a hard time knowing what they mean.
<?php
/**
* Decomposes a bitmask value into bitmask components
*
* @param int $value The bitmask value to decompose
* @param array $bitmasks An array of possible bitmask components
*
* @return array
*/
function bitmask_decompose($value, array $bitmasks)
{
return array_filter($bitmasks, function ($bitmask) use ($value) {
return $value & $bitmask;
});
}
<?php
require_once __DIR__ . '/func_get_error_level_constants.php';
require_once __DIR__ . '/func_bitmask_decompose.php';
/**
* Decomposes an error level value into an array of error level bitmasks
*
* @param int $value An error level value
*
* @return array An associative array with error level constant as key
*/
function error_level_decompose($value)
{
if ($value == E_ALL) {
return array('E_ALL' => E_ALL);
}
$constants = array_filter(get_error_level_constants(), function ($constant) {
return $constant != E_ALL;
});
return bitmask_decompose($value, $constants);
}
<?php
require_once __DIR__ . '/func_preg_grep_key.php';
/**
* Returns defined error level constants as an associative array
*
* @return array An associative array with constant name as key
*/
function get_error_level_constants() {
return preg_grep_key('/^E_/', get_defined_constants());
}
<?php
/**
* Return array entries having keys that match the pattern
*
* @param string $pattern The pattern to search for, as a string.
* @param array $input The input array.
*
* @return Returns an array indexed using the keys from the input array.
*/
function preg_grep_key($pattern, array $input)
{
$keys = preg_grep($pattern, array_keys($input));
return array_intersect_key($input, array_flip($keys));
}
<?php
require_once __DIR__ . '/func_error_level_decompose.php';
$testData = array(
'E_STRICT | E_NOTICE | E_WARNING' => array(
'E_STRICT' => E_STRICT,
'E_NOTICE' => E_NOTICE,
'E_WARNING' => E_WARNING
),
'E_ALL & ~E_NOTICE' => array (
'E_ERROR' => E_ERROR,
'E_RECOVERABLE_ERROR' => E_RECOVERABLE_ERROR,
'E_WARNING' => E_WARNING,
'E_PARSE' => E_PARSE,
'E_STRICT' => E_STRICT,
'E_DEPRECATED' => E_DEPRECATED,
'E_CORE_ERROR' => E_CORE_ERROR,
'E_CORE_WARNING' => E_CORE_WARNING,
'E_COMPILE_ERROR' => E_COMPILE_ERROR,
'E_COMPILE_WARNING' => E_COMPILE_WARNING,
'E_USER_ERROR' => E_USER_ERROR,
'E_USER_WARNING' => E_USER_WARNING,
'E_USER_NOTICE' => E_USER_NOTICE,
'E_USER_DEPRECATED' => E_USER_DEPRECATED,
),
// write more tests here ...
);
foreach ($testData as $value => $expected) {
$eval = eval("return {$value};");
$actual = error_level_decompose($eval);
$message = var_export(compact('value', 'expected', 'actual'), true);
assert('$actual == $expected /* ' . $message . ' */');
}
@jehoshua02
Copy link
Copy Markdown
Author

The error_level_decompose() function could probably be made more intelligent to compact what is returned ... but the current implementation just returns an array, assuming that each item in the array could be combined using the | bitwise operator to get the decomposed value. I wonder if the compacted result could be treated the same way, or if it would require other bitwise operators, in which case, the current array format no longer is sufficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment