-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce duplication / line-count. Html object dumping: Cases, Constant…
…s, Properties, & Methods now all extend new AbstractObjectSection. Improve code-coverage
- Loading branch information
Showing
37 changed files
with
1,788 additions
and
1,658 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.