Skip to content

Commit

Permalink
Reduce duplication / line-count. Html object dumping: Cases, Constant…
Browse files Browse the repository at this point in the history
…s, Properties, & Methods now all extend new AbstractObjectSection. Improve code-coverage
  • Loading branch information
bkdotcom committed Sep 22, 2023
1 parent e315226 commit 9d1a11b
Show file tree
Hide file tree
Showing 37 changed files with 1,788 additions and 1,658 deletions.
3 changes: 2 additions & 1 deletion src/Debug/Abstraction/AbstractObjectConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace bdk\Debug\Abstraction;

use bdk\Debug;
use bdk\Debug\Abstraction\Abstracter;
use bdk\Debug\Abstraction\Abstraction;
use ReflectionClass;
use ReflectionClassConstant;
Expand Down Expand Up @@ -165,7 +166,7 @@ private function getCaseRefInfo(ReflectionEnumUnitCase $refCase)
'isFinal' => $refCase->isFinal(),
'value' => $refCase instanceof ReflectionEnumBackedCase
? $refCase->getBackingValue()
: null,
: Abstracter::UNDEFINED,
'visibility' => $this->helper->getVisibility($refCase),
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/CurlHttpMessageMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function onRedirect(RequestInterface $request, ResponseInterface $respons
*
* @return Promise
*/
public function onRequest(CurlReqRes $curlReqRes) // RequestInterface $request, array $options
public function onRequest(CurlReqRes $curlReqRes)
{
$request = $curlReqRes->getRequest();
$options = $curlReqRes->getOptions();
Expand Down
204 changes: 204 additions & 0 deletions src/Debug/Dump/Html/AbstractObjectSection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
<?php

/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2023 Brad Kent
* @version v3.1
*/

namespace bdk\Debug\Dump\Html;

use bdk\Debug\Abstraction\Abstracter;
use bdk\Debug\Abstraction\ObjectAbstraction;
use bdk\Debug\Dump\Html\Helper;
use bdk\Debug\Dump\Html\Value as ValDumper;
use bdk\Debug\Utility\Html as HtmlUtil;

/**
* Base class for dumping object constants/enum-cases/properties/methods
*/
abstract class AbstractObjectSection
{
protected $helper;
protected $html;
protected $valDumper;

/**
* Constructor
*
* @param ValDumper $valDumper Html dumper
* @param Helper $helper Html dump helpers
* @param HtmlUtil $html Html methods
*/
public function __construct(ValDumper $valDumper, Helper $helper, HtmlUtil $html)
{
$this->valDumper = $valDumper;
$this->helper = $helper;
$this->html = $html;
}

/**
* Iterate over cases, constants, properties, or methods
*
* @param ObjectAbstraction $abs Object abstraction
* @param string $what 'cases', 'constants', 'properties', or 'methods'
* @param array $cfg config options
*
* @return string
*/
public function dumpItems(ObjectAbstraction $abs, $what, array $cfg)
{
$html = '';
$items = $abs->sort($abs[$what], $abs['sort']);
foreach ($items as $name => $info) {
$info = \array_merge(array(
'className' => $abs['className'],
'declaredLast' => null,
'declaredPrev' => null,
), $info);
$info['isInherited'] = $info['declaredLast'] && $info['declaredLast'] !== $abs['className'];
$html .= $this->dumpItem($name, $info, $cfg) . "\n";
}
return $html;
}

/**
* Dump Property or Constant info as HTML
*
* @param string $name Property/costant name
* @param array $info Property/constant info
* @param array $cfg config options
*
* @return string html fragment
*/
protected function dumpItem($name, array $info, array $cfg)
{
$vis = (array) $info['visibility'];
$info['isPrivateAncestor'] = \in_array('private', $vis, true) && $info['isInherited'];
if ($info['isPrivateAncestor']) {
$info['isInherited'] = false;
}
return $this->html->buildTag(
'dd',
$this->getAttribs($info, $cfg),
$this->dumpItemInner($name, $info, $cfg)
);
}

/**
* Build property/constant/cate inner html
*
* @param string $name Property name
* @param array $info Property info
* @param array $cfg options (currently just attributeOutput)
*
* @return string html fragment
*/
protected function dumpItemInner($name, array $info, array $cfg)
{
$name = \str_replace('debug.', '', $name);
$title = $cfg['phpDocOutput']
? (string) $info['desc']
: '';
$parts = \array_filter(array(
'1_modifiers' => $this->dumpModifiers($info),
'2_type' => isset($info['type'])
? $this->helper->markupType($info['type'])
: '',
'3_name' => $this->valDumper->dump($name, array(
'addQuotes' => \preg_match('#[\s\r\n]#u', $name) === 1 || $name === '',
'attribs' => array(
'class' => array('t_identifier'),
'title' => $title,
),
)),
'4_value' => $info['value'] !== Abstracter::UNDEFINED
? '<span class="t_operator">=</span> ' . $this->valDumper->dump($info['value'])
: '',
));
return \implode(' ', $parts);
}

/**
* Dump "modifiers"
*
* @param array $info Abstraction info
*
* @return string html fragment
*/
protected function dumpModifiers(array $info)
{
$modifiers = $this->getModifiers($info);
return \implode(' ', \array_map(static function ($modifier) {
return '<span class="t_modifier_' . $modifier . '">' . $modifier . '</span>';
}, $modifiers));
}

/**
* Get html attributes
*
* @param array $info Abstraction info
* @param array $cfg config options
*
* @return array
*/
protected function getAttribs(array $info, array $cfg = array())
{
$attribs = array(
'class' => $this->getClasses($info),
'data-attributes' => $info['attributes'],
'data-declared-prev' => $info['declaredPrev'],
'data-inherited-from' => $info['declaredLast'],
);
$filter = \array_filter(array(
'class' => true,
'data-attributes' => $cfg['attributeOutput'] && $info['attributes'],
'data-declared-prev' => empty($info['isInherited']) && !empty($info['declaredPrev']),
'data-inherited-from' => !empty($info['isInherited']) || $info['isPrivateAncestor'],
));
return \array_intersect_key($attribs, $filter);
}

/**
* Get classes
*
* @param array $info Abstraction info
*
* @return string[]
*/
abstract protected function getClasses(array $info);

/**
* Get "modifiers" (final, readonly, static)
*
* @param array $info Abstraction info
*
* @return string[]
*/
abstract protected function getModifiers(array $info);

/**
* Generate some info regarding the given method names
*
* @param array $methods method names
*
* @return string html fragment
*/
protected function magicMethodInfo($methods)
{
if (!$methods) {
return '';
}
$methods = \array_map(static function ($method) {
return '<code>' . $method . '</code>';
}, $methods);
$methods = \count($methods) === 1
? 'a ' . $methods[0] . ' method'
: \implode(' and ', $methods) . ' methods';
return '<dd class="info magic">This object has ' . $methods . '</dd>' . "\n";
}
}
33 changes: 7 additions & 26 deletions src/Debug/Dump/Html/HtmlObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
class HtmlObject
{
public $valDumper;
protected $cases;
protected $constants;
protected $helper;
protected $html;
Expand All @@ -44,9 +45,10 @@ public function __construct(ValDumper $valDumper, Helper $helper, HtmlUtil $html
$this->valDumper = $valDumper;
$this->helper = $helper;
$this->html = $html;
$this->constants = new ObjectConstants($this, $helper, $html);
$this->methods = new ObjectMethods($this, $helper, $html);
$this->properties = new ObjectProperties($this, $helper, $html);
$this->cases = new ObjectCases($valDumper, $helper, $html);
$this->constants = new ObjectConstants($valDumper, $helper, $html);
$this->methods = new ObjectMethods($valDumper, $helper, $html);
$this->properties = new ObjectProperties($valDumper, $helper, $html);
}

/**
Expand Down Expand Up @@ -79,36 +81,15 @@ public function dump(Abstraction $abs)
. $this->dumpExtends($abs)
. $this->dumpImplements($abs)
. $this->dumpAttributes($abs)
. $this->constants->dumpConstants($abs)
. $this->constants->dumpCases($abs)
. $this->constants->dump($abs)
. $this->cases->dump($abs)
. $this->properties->dump($abs)
. $this->methods->dump($abs)
. $this->dumpPhpDoc($abs)
. '</dl>' . "\n";
return $this->cleanup($html);
}

/**
* Generate some info regarding the given method names
*
* @param array $methods method names
*
* @return string html fragment
*/
public function magicMethodInfo($methods)
{
if (!$methods) {
return '';
}
$methods = \array_map(static function ($method) {
return '<code>' . $method . '</code>';
}, $methods);
$methods = \count($methods) === 1
? 'a ' . $methods[0] . ' method'
: \implode(' and ', $methods) . ' methods';
return '<dd class="info magic">This object has ' . $methods . '</dd>' . "\n";
}

/**
* Remove empty tags and unneeded attributes
*
Expand Down
70 changes: 70 additions & 0 deletions src/Debug/Dump/Html/ObjectCases.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/**
* This file is part of PHPDebugConsole
*
* @package PHPDebugConsole
* @author Brad Kent <bkfake-github@yahoo.com>
* @license http://opensource.org/licenses/MIT MIT
* @copyright 2014-2023 Brad Kent
* @version v3.1
*/

namespace bdk\Debug\Dump\Html;

use bdk\Debug\Abstraction\Abstraction;
use bdk\Debug\Abstraction\AbstractObject;

/**
* Dump object constants and Enum cases as HTML
*/
class ObjectCases extends AbstractObjectSection
{
/**
* Dump enum cases
*
* @param Abstraction $abs Object Abstraction instance
*
* @return string html fragment
*/
public function dump(Abstraction $abs)
{
if (\in_array('UnitEnum', $abs['implements'], true) === false) {
return '';
}
$cfg = array(
'attributeOutput' => $abs['cfgFlags'] & AbstractObject::CASE_ATTRIBUTE_OUTPUT,
'collect' => $abs['cfgFlags'] & AbstractObject::CASE_COLLECT,
'output' => $abs['cfgFlags'] & AbstractObject::CASE_OUTPUT,
'phpDocOutput' => $abs['cfgFlags'] & AbstractObject::PHPDOC_OUTPUT,
);
if (!$cfg['output']) {
return '';
}
if (!$cfg['collect']) {
return '<dt class="cases">cases <i>not collected</i></dt>' . "\n";
}
if (!$abs['cases']) {
return '<dt class="cases"><i>no cases!</i></dt>' . "\n";
}
$html = '<dt class="cases">cases</dt>' . "\n";
$html .= $this->dumpItems($abs, 'cases', $cfg);
return $html;
}

/**
* {@inheritDoc}
*/
protected function getClasses(array $info)
{
return array('case');
}

/**
* {@inheritDoc}
*/
protected function getModifiers(array $info)
{
return array();
}
}
Loading

0 comments on commit 9d1a11b

Please sign in to comment.