Skip to content

Commit

Permalink
PhpDoc now sanitized at parsing level vs sanitizing on output
Browse files Browse the repository at this point in the history
PhpDoc improve @method parsing
StatementInfo::appendLog adds id to the group, so we can append the group
Yii 1.1 LogRoute... experimentaly capture system.caching..yii:dbquery log entry and convert to or appnd prior statementInfo output
  • Loading branch information
bkdotcom committed Jun 16, 2024
1 parent 809501b commit 7ed5d4f
Show file tree
Hide file tree
Showing 34 changed files with 617 additions and 364 deletions.
4 changes: 2 additions & 2 deletions src/Debug/Abstraction/Object/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private function getCaseRefInfo(ReflectionEnumUnitCase $refCase)
? $this->helper->getAttributes($refCase)
: array(),
'desc' => $this->phpDocCollect
? $this->helper->getPhpDocVar($refCase)['desc']
? $this->helper->getPhpDocVar($refCase)['desc'] // actually the summary
: null,
'isFinal' => $refCase->isFinal(),
'value' => $refCase instanceof ReflectionEnumBackedCase
Expand Down Expand Up @@ -198,7 +198,7 @@ private function getConstantRefInfo(ReflectionClassConstant $refConstant)
? $this->helper->getAttributes($refConstant)
: array(),
'desc' => $this->phpDocCollect
? $this->helper->getPhpDocVar($refConstant)['desc']
? $this->helper->getPhpDocVar($refConstant)['desc'] // actually the summary
: null,
'isFinal' => PHP_VERSION_ID >= 80100
? $refConstant->isFinal()
Expand Down
6 changes: 2 additions & 4 deletions src/Debug/Abstraction/Object/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public function add(Abstraction $abs)
$abs['cfgFlags'] & AbstractObject::METHOD_COLLECT
? $this->addFull($abs)
: $this->addMin($abs);
$this->helper->clearPhpDoc($abs);
\ksort($abs['methods']);
if (isset($abs['methods']['__toString'])) {
$info = $abs['methods']['__toString'];
$info['returnValue'] = null;
Expand Down Expand Up @@ -167,8 +169,6 @@ private function addFull(Abstraction $abs)
$this->addViaRef($abs);
$this->addViaPhpDoc($abs);
$this->addImplements($abs);
$this->helper->clearPhpDoc($abs);
\ksort($abs['methods']);
}

/**
Expand All @@ -182,8 +182,6 @@ private function addMin(Abstraction $abs)
{
$this->minimal = true;
$this->addViaRef($abs);
$this->helper->clearPhpDoc($abs);
\ksort($abs['methods']);
$this->minimal = false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Abstraction/Object/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ private function buildViaRef(Abstraction $abs, ReflectionProperty $refProperty)
? $this->helper->getAttributes($refProperty)
: array(),
'desc' => $abs['cfgFlags'] & AbstractObject::PHPDOC_COLLECT
? $phpDoc['desc']
? $phpDoc['desc'] // actually the "summary"
: null,
'isPromoted' => PHP_VERSION_ID >= 80000
? $refProperty->isPromoted()
Expand Down
5 changes: 5 additions & 0 deletions src/Debug/Dump/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class Base extends AbstractComponent
/** @var string */
protected $channelNameRoot;

/** @var list<string> */
protected $readOnly = array(
'valDumper',
);

/** @var Substitution */
protected $substitution;

Expand Down
4 changes: 2 additions & 2 deletions src/Debug/Dump/Html/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ public function dumpPhpDoc($markdown, array $opts = array())
}
// run through valDumper to highlight chars
return $this->dumper->valDumper->dump(
$this->debug->html->sanitize($markdown),
$markdown,
\array_merge(array(
'sanitize' => false, // we've already sanitized
'sanitize' => false, // phpDoc parser sanitized by removing non-whitelisted html tags & attributes
'tagName' => null,
'type' => Type::TYPE_STRING,
'visualWhiteSpace' => false,
Expand Down
6 changes: 3 additions & 3 deletions src/Debug/Dump/Html/HtmlObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public function dump(ObjectAbstraction $abs)
/**
* Build implements tree
*
* @param array $implements Implements structure
* @param array $interfacesCollapse Interfaces that should initially be hidden
* @param list<string> $implements Implements structure
* @param list<string> $interfacesCollapse Interfaces that should initially be hidden
*
* @return string
*/
Expand Down Expand Up @@ -306,7 +306,7 @@ protected function dumpImplements(ObjectAbstraction $abs)
return '';
}
return '<dt>implements</dt>' . "\n"
. '<dd>' . $this->buildImplementsTree($abs['implements'], $abs['interfacesCollapse']) . '</dd>' . "\n";
. '<dd class="implements">' . $this->buildImplementsTree($abs['implements'], $abs['interfacesCollapse']) . '</dd>' . "\n";
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/Debug/Dump/Html/HtmlStringBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ public function dump(Abstraction $abs)
{
$tagName = $this->valDumper->optionGet('tagName');
$this->valDumper->optionSet('tagName', null);
$str = $this->dumpBinaryBasic($abs);
$str = $this->dumpBasic($abs);
$strLenDiff = $abs['strlen'] - $abs['strlenValue'];
if ($abs['strlenValue'] && $strLenDiff) {
$str .= '<span class="maxlen">&hellip; ' . $strLenDiff . ' more bytes (not logged)</span>';
}
if ($abs['brief']) {
return $this->dumpBinaryBrief($str, $abs);
return $this->dumpBrief($str, $abs);
}
if ($abs['percentBinary'] > 33 || $abs['contentType']) {
$this->valDumper->optionSet('postDump', $this->dumpBinaryPost($abs, $tagName));
$this->valDumper->optionSet('postDump', $this->dumpPost($abs, $tagName));
}
return $str;
}
Expand All @@ -76,7 +76,7 @@ public function dump(Abstraction $abs)
*
* @return string
*/
private function dumpBinaryBasic(Abstraction $abs)
private function dumpBasic(Abstraction $abs)
{
if ($abs['strlenValue'] === 0) {
return '';
Expand All @@ -100,7 +100,7 @@ private function dumpBinaryBasic(Abstraction $abs)
*
* @return string
*/
private function dumpBinaryBrief($str, Abstraction $abs)
private function dumpBrief($str, Abstraction $abs)
{
return $abs['contentType']
? '<span class="t_keyword">string</span>'
Expand All @@ -118,7 +118,7 @@ private function dumpBinaryBrief($str, Abstraction $abs)
*
* @return Closure
*/
private function dumpBinaryPost(Abstraction $abs, $tagName)
private function dumpPost(Abstraction $abs, $tagName)
{
return function ($dumped) use ($abs, $tagName) {
$lis = array();
Expand Down
77 changes: 65 additions & 12 deletions src/Debug/Framework/Yii1_1/LogRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
namespace bdk\Debug\Framework\Yii1_1;

use bdk\Debug;
use bdk\Debug\LogEntry;
use bdk\Debug\Collector\StatementInfo;
use CLogger;
use CLogRoute;
use Exception;
Expand All @@ -23,8 +25,10 @@
*/
class LogRoute extends CLogRoute
{
/** @var string specify levels handled by route */
public $levels = 'error, info, profile, trace, warning';

/** @var Debug */
private $debug;

private $levelMap = array(
Expand All @@ -43,8 +47,8 @@ class LogRoute extends CLogRoute

/**
* @var array $except An array of categories to exclude from logging.
* Regex pattern matching is supported
* We exclude system.db categories... handled via pdo wrapper
* Regex pattern matching is supported
* We exclude system.db categories... handled via pdo wrapper
*/
protected $except = array(
'/^system\.db\./',
Expand Down Expand Up @@ -258,26 +262,70 @@ protected function processLogEntry(array $logEntry)
{
$logEntry = $this->normalizeMessage($logEntry);
$logEntry = $this->meta->messageMeta($logEntry);
if ($logEntry['level'] === CLogger::LEVEL_PROFILE) {
$this->processLogEntryProfile($logEntry);
return;
if (\strpos($logEntry['category'], 'system.caching') === 0 && \preg_match('/^(Saving|Serving) "yii:dbquery/', $logEntry['message'])) {
return $this->processSqlCachingLogEntry($logEntry);
}
if ($logEntry['level'] === CLogger::LEVEL_TRACE) {
if (\count($logEntry['trace']) > 1) {
$this->processLogEntryTrace($logEntry);
return;
}
$logEntry['level'] = CLogger::LEVEL_INFO;
$method = 'processLogEntry' . \ucfirst($logEntry['level']);
$method = \method_exists($this, $method)
? $method
: 'processLogEntryDefault';
$this->{$method}($logEntry);
}

/**
* Convert SQL caching log entry to a statementInfo log entry
*
* @param array $logEntry our key/value'd log entry
*
* @return void
*/
private function processSqlCachingLogEntry(array $logEntry)
{
// this is an accurate way to get channel for saved to cache... not so much for from cache
// we have no connectionString to channel mapping
$groupId = StatementInfo::lastGroupId();
$debug = $this->debug->data->get('log.' . $groupId)->getSubject();
$returnValue = 'saved to cache';

if (\strpos($logEntry['message'], 'Serving') === 0) {
$regEx = '/^Serving "yii:dbquery:\S+:\S*:\S+:(.*?)(?::(a:\d+:\{.+\}))?" from cache$/s';
\preg_match($regEx, $logEntry['message'], $matches);
$statementInfo = new StatementInfo($matches[1], $matches[2] ? \unserialize($matches[2]) : array());
$statementInfo->appendLog($debug);
$groupId = StatementInfo::lastGroupId();
$returnValue = 'from cache';
}

$debug->log(new LogEntry(
$debug,
'groupEndValue',
array($returnValue),
array(
'appendGroup' => $groupId,
'icon' => 'fa fa-cube',
'level' => 'info',
)
));
}

/**
* Process Yii log entry
*
* @param array $logEntry our key/value'd log entry
*
* @return void
*/
private function processLogEntryDefault(array $logEntry)
{
$debug = $logEntry['channel'];
$method = $this->levelMap[$logEntry['level']];
$args = \array_filter(array(
\ltrim($logEntry['category'] . ':', ':'),
$logEntry['message'],
));
if ($logEntry['meta']) {
$args[] = $debug->meta($logEntry['meta']);
}
$method = $this->levelMap[$logEntry['level']];
\call_user_func_array(array($debug, $method), $args);
}

Expand Down Expand Up @@ -316,6 +364,11 @@ private function processLogEntryProfile(array $logEntry)
*/
private function processLogEntryTrace(array $logEntry)
{
if (\count($logEntry['trace']) <= 1) {
$logEntry['level'] = CLogger::LEVEL_INFO;
return $this->processLogEntryDefault($logEntry);
}

$caption = $logEntry['category']
? $logEntry['category'] . ': ' . $logEntry['message']
: $logEntry['message'];
Expand Down
10 changes: 7 additions & 3 deletions src/Debug/Framework/Yii1_1/LogRouteMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public function messageMeta(array $logEntry)
$logEntry = $this->messageMetaTrace($logEntry);
$logEntry = $this->messageMetaCaller($logEntry);
$categoryFuncs = array(
'/^system\\./' => 'messageMetaSystem',
'/^system\\.caching/' => 'messageMetaSystemCaching',
'application' => 'messageMetaApplication',
'/^system\.caching/' => 'messageMetaSystemCaching',
'system.CModule' => 'messageMetaSystemCmodule',
'/^system\./' => 'messageMetaSystem',
'application' => 'messageMetaApplication',
);
foreach ($categoryFuncs as $match => $method) {
$isMatch = $match[0] === '/'
Expand Down Expand Up @@ -163,6 +163,10 @@ private function messageMetaSystem(array $logEntry)
*/
private function messageMetaSystemCaching(array $logEntry)
{
if (\preg_match('/^(Saving|Serving) "yii:dbquery/', $logEntry['message'])) {
// Leave as is for now. We'll convert to POO / statementInfo log entry
return $logEntry;
}
$channelName = \str_replace('system.caching.', '', $logEntry['category']);
$icon = 'fa fa-cube';
$logEntry['category'] = $channelName;
Expand Down
40 changes: 23 additions & 17 deletions src/Debug/Utility/ArrayUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,21 @@ public static function copy(array $source, $deep = true)
* Returns an array containing all the values from array that are not present in any of the other arrays.
*
* @param array $array1 array to compare from
* @param array ...$arrays arrays to compare against
* @param array ...$array2 arrays to compare against
*
* @return array
*
* @throws InvalidArgumentException
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
*/
public static function diffAssocRecursive(array $array1, $arrays)
public static function diffAssocRecursive(array $array1, $array2)
{
return \array_reduce(\array_slice(\func_get_args(), 1), static function (array $carry, $array2) {
$arrays = \array_slice(\func_get_args(), 1);
foreach ($arrays as $array2) {
if (\is_array($array2) === false) {
throw new InvalidArgumentException('diffAssocRecursive: non-array value passed');
}
return static::diffAssocRecursiveWalk($carry, $array2);
}, $array1);
}
return \array_reduce($arrays, array(__CLASS__, 'diffAssocRecursiveWalk'), $array1);
}

/**
Expand Down Expand Up @@ -103,18 +102,26 @@ public static function isList($val)
/**
* Applies the callback to all leafs of the given array
*
* Callable will receive the value and key
*
* keys are preserved
*
* @param callable $callback Callable to be applied
* @param array $input Input array
*
* @return array
*/
public static function mapRecursive($callback, array $input)
{
return \array_map(static function ($val) use ($callback) {
return \is_array($val)
? self::mapRecursive($callback, $val)
: $callback($val);
}, $input);
$keys = \array_keys($input);
return \array_combine(
$keys,
\array_map(static function ($val, $key) use ($callback) {
return \is_array($val)
? self::mapRecursive($callback, $val)
: $callback($val, $key);
}, $input, $keys)
);
}

/**
Expand All @@ -126,17 +133,16 @@ public static function mapRecursive($callback, array $input)
* @return array
*
* @throws InvalidArgumentException
*
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
*/
public static function mergeDeep(array $array1, $array2)
{
return \array_reduce(\array_slice(\func_get_args(), 1), static function (array $carry, $array2) {
$arrays = \array_slice(\func_get_args(), 1);
foreach ($arrays as $array2) {
if (\is_array($array2) === false) {
throw new InvalidArgumentException('mergeDeep: non-array value passed');
}
return static::mergeDeepWalk($carry, $array2);
}, $array1);
}
return \array_reduce($arrays, array(__CLASS__, 'mergeDeepWalk'), $array1);
}

/**
Expand Down
Loading

0 comments on commit 7ed5d4f

Please sign in to comment.