Skip to content

Commit

Permalink
* consistently named buildValues method for Object, Constants, Method…
Browse files Browse the repository at this point in the history
…Params, Methods, & Params classes

* HtmlBuild::buildAttribNameValue()   no longer output title=""   (omit title attrib if empty string)  + better handling of boolean attributes
  • Loading branch information
bkdotcom committed Aug 16, 2024
1 parent 88aa589 commit 4fa87f4
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 130 deletions.
2 changes: 1 addition & 1 deletion src/Debug/Abstraction/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public function getAbstraction($obj, $method = null, array $hist = array())
*
* @return array<string,mixed>
*/
public static function buildObjValues(array $values = array())
public static function buildValues(array $values = array())
{
if (self::$values['cfgFlags'] === 0) {
// calculate default cfgFlags (everything except for "brief")
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Abstraction/Object/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function __construct(AbstractObject $abstractObject)
$this->methods = $abstractObject->methods;
$this->properties = $abstractObject->properties;

$defaultValues = $abstractObject->buildObjValues();
$defaultValues = $abstractObject->buildValues();
self::$values['cfgFlags'] = $defaultValues['cfgFlags'];
}

Expand Down
8 changes: 4 additions & 4 deletions src/Debug/Abstraction/Object/MethodParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function __construct(AbstractObject $abstractObject)
*
* @return array
*/
public static function buildParamValues($values = array())
public static function buildValues($values = array())
{
return \array_merge(static::$baseParamInfo, $values);
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public function getParams(Abstraction $abs, ReflectionMethod $refMethod, $phpDoc
$phpDocCount = \count($phpDocParams);
for ($i = \count($params); $i < $phpDocCount; $i++) {
$phpDocParam = $phpDoc['param'][$i];
$params[] = $this->buildParamValues(array(
$params[] = $this->buildValues(array(
'defaultValue' => $this->phpDocParamValue($phpDocParam, $this->abs['className']),
'desc' => $phpDocParam['desc'],
'isOptional' => true,
Expand All @@ -132,7 +132,7 @@ public function getParamsPhpDoc(Abstraction $abs, $parsedMethodTag, $className)
{
$this->abs = $abs;
return \array_map(function ($phpDocParam) use ($className) {
return $this->buildParamValues(array(
return $this->buildValues(array(
'defaultValue' => $this->phpDocParamValue($phpDocParam, $className),
'name' => $phpDocParam['name'],
'type' => $phpDocParam['type'],
Expand Down Expand Up @@ -161,7 +161,7 @@ private function getParamsReflection(ReflectionMethod $refMethod, array $phpDocP
foreach ($refMethod->getParameters() as $refParameter) {
$name = $refParameter->getName();
$phpDocParam = $this->phpDocParam($name, $phpDocParamsByName);
$params[] = $this->buildParamValues(array(
$params[] = $this->buildValues(array(
'attributes' => $collectAttribute
? $this->helper->getAttributes($refParameter)
: array(),
Expand Down
60 changes: 28 additions & 32 deletions src/Debug/Dump/Html/HtmlObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,23 @@ public function __construct(ValDumper $valDumper, Helper $helper, HtmlUtil $html
*/
public function dump(ObjectAbstraction $abs)
{
$classname = $this->dumpClassname($abs);
$className = $this->dumpClassName($abs);
if ($abs['isRecursion']) {
return $classname . "\n" . '<span class="t_recursion">*RECURSION*</span>';
return $className . "\n" . '<span class="t_recursion">*RECURSION*</span>';
}
if ($abs['isMaxDepth']) {
return $classname . "\n" . '<span class="t_maxDepth">*MAX DEPTH*</span>';
return $className . "\n" . '<span class="t_maxDepth">*MAX DEPTH*</span>';
}
if ($abs['isExcluded']) {
return $this->dumpToString($abs)
. $classname . "\n" . '<span class="excluded">NOT INSPECTED</span>';
. $className . "\n" . '<span class="excluded">NOT INSPECTED</span>';
}
if (($abs['cfgFlags'] & AbstractObject::BRIEF) && \strpos(\json_encode($abs['implements']), '"UnitEnum"') !== false) {
$this->valDumper->optionSet('tagName', null);
return $classname;
return $className;
}
$html = $this->dumpToString($abs)
. $classname . "\n"
. $className . "\n"
. $this->dumpInner($abs);
if (\strpos($abs['sort'], 'inheritance') === 0) {
$this->valDumper->optionSet('attribs.class.__push__', 'groupByInheritance');
Expand All @@ -135,7 +135,7 @@ private function buildImplementsTree(array $implements, array $interfacesCollaps
{
$str = '<ul class="list-unstyled">' . "\n";
foreach ($implements as $k => $v) {
$classname = \is_array($v)
$className = \is_array($v)
? $k
: $v;
$str .= '<li>'
Expand All @@ -144,10 +144,10 @@ private function buildImplementsTree(array $implements, array $interfacesCollaps
array(
'class' => array(
'interface' => true,
'toggle-off' => \in_array($classname, $interfacesCollapse, true),
'toggle-off' => \in_array($className, $interfacesCollapse, true),
),
),
$this->valDumper->markupIdentifier($classname, 'classname')
$this->valDumper->markupIdentifier($className, 'classname')
)
. (\is_array($v) ? "\n" . self::buildImplementsTree($v, $interfacesCollapse) : '')
. '</li>' . "\n";
Expand All @@ -172,7 +172,6 @@ private function cleanup($html)
' data-chars="[]"',
' data-declared-prev="null"',
' data-inherited-from="null"',
' title=""',
), '', $html);
return $html;
}
Expand Down Expand Up @@ -249,20 +248,19 @@ protected function dumpAttributeArgs($args)
}

/**
* Dump classname of object
* Classname may be wrapped in a span that includes phpDoc summary / desc
* Dump className of object
* ClassName may be wrapped in a span that includes phpDoc summary / desc
*
* @param ObjectAbstraction $abs Object Abstraction instance
*
* @return string html fragment
*/
protected function dumpClassname(ObjectAbstraction $abs)
protected function dumpClassName(ObjectAbstraction $abs)
{
$phpDocOutput = $abs['cfgFlags'] & AbstractObject::PHPDOC_OUTPUT;
$title = $phpDocOutput
? $this->helper->dumpPhpDoc($abs['phpDoc']['summary'] . "\n\n" . $abs['phpDoc']['desc'])
: null;
$title = $title ?: null;
if (\strpos(\json_encode($abs['implements']), '"UnitEnum"') !== false) {
return $this->html->buildTag(
'span',
Expand All @@ -279,7 +277,7 @@ protected function dumpClassname(ObjectAbstraction $abs)
}

/**
* Dump classnames of classes obj extends
* Dump classNames of classes obj extends
*
* @param ObjectAbstraction $abs Object Abstraction instance
*
Expand All @@ -288,25 +286,24 @@ protected function dumpClassname(ObjectAbstraction $abs)
protected function dumpExtends(ObjectAbstraction $abs)
{
return '<dt>extends</dt>' . "\n"
. \implode(\array_map(function ($classname) {
return '<dd class="extends">' . $this->valDumper->markupIdentifier($classname, 'classname') . '</dd>' . "\n";
. \implode(\array_map(function ($className) {
return '<dd class="extends">' . $this->valDumper->markupIdentifier($className, 'classname') . '</dd>' . "\n";
}, $abs['extends']));
}

/**
* Dump classnames of interfaces obj extends
* Dump classNames of interfaces obj extends
*
* @param ObjectAbstraction $abs Object Abstraction instance
*
* @return string html fragment
*/
protected function dumpImplements(ObjectAbstraction $abs)
{
if (empty($abs['implements'])) {
return '';
}
return '<dt>implements</dt>' . "\n"
. '<dd class="implements">' . $this->buildImplementsTree($abs['implements'], $abs['interfacesCollapse']) . '</dd>' . "\n";
return empty($abs['implements'])
? ''
: '<dt>implements</dt>' . "\n"
. '<dd class="implements">' . $this->buildImplementsTree($abs['implements'], $abs['interfacesCollapse']) . '</dd>' . "\n";
}

/**
Expand All @@ -325,13 +322,12 @@ protected function dumpModifiers(ObjectAbstraction $abs)
'readonly' => $abs['isReadOnly'],
'trait' => $abs['isTrait'],
)));
if (empty($modifiers)) {
return '';
}
return '<dt class="modifiers">modifiers</dt>' . "\n"
. \implode('', \array_map(static function ($modifier) {
return '<dd class="t_modifier_' . $modifier . '">' . $modifier . '</dd>' . "\n";
}, $modifiers));
return empty($modifiers)
? ''
: '<dt class="modifiers">modifiers</dt>' . "\n"
. \implode('', \array_map(static function ($modifier) {
return '<dd class="t_modifier_' . $modifier . '">' . $modifier . '</dd>' . "\n";
}, $modifiers));
}

/**
Expand All @@ -345,11 +341,11 @@ protected function dumpToString(ObjectAbstraction $abs)
{
$len = 0;
$val = $this->getToStringVal($abs, $len);
$valAppend = '';
$classes = array('t_stringified');
if ($val === $abs['className']) {
return '';
}
$valAppend = '';
$classes = array('t_stringified');
if ($len > 100) {
$classes[] = 't_string_trunc'; // truncated
$val = \substr($val, 0, 100);
Expand Down
1 change: 0 additions & 1 deletion src/Debug/Dump/Html/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ protected function buildBody($rows)
);
$tBody .= $this->buildRow($row, $rowInfo, $k);
}
$tBody = \str_replace(' title=""', '', $tBody);
return '<tbody>' . "\n" . $tBody . '</tbody>' . "\n";
}

Expand Down
5 changes: 3 additions & 2 deletions src/Debug/Utility/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Html

/**
* The presence of these attributes = true / absence = false
* value not necessary, but may be written as key="key" (ie autofocus="autofocus" )
*
* Value not necessary, but may be written as key="key" (ie autofocus="autofocus" )
*
* @var array
*
Expand Down Expand Up @@ -80,7 +81,7 @@ class Html
);

/**
* enum attributes that behave like bool, but have "true" / "false" value
* Enum attributes that behave like bool, but have "true" / "false" value
*
* @var array
*/
Expand Down
79 changes: 57 additions & 22 deletions src/Debug/Utility/HtmlBuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
*/
class HtmlBuild
{
/**
* Omit these attributes if the value is an empty string
*
* We never include the attribute if the value is null (unless it's a data or boolean enum attribute)
*
* @var list<string>
*/
public static $omitIfEmptyAttrib = array('class', 'style', 'title');

/**
* Build name="value"
*
Expand All @@ -36,7 +45,7 @@ public static function buildAttribNameValue(&$name, $value)
if ($value === null) {
return '';
}
if ($value === '' && \in_array($name, array('class', 'style'), true)) {
if ($value === '' && \in_array($name, self::$omitIfEmptyAttrib, true)) {
return '';
}
return $name . '="' . \htmlspecialchars($value) . '"';
Expand All @@ -52,7 +61,7 @@ public static function buildAttribNameValue(&$name, $value)
*/
private static function buildAttribVal($name, $val)
{
switch (static::getType($name, $val)) {
switch (static::getAttribType($name, $val)) {
case 'array':
/** @var string[] $val */
return static::buildAttribValArray($name, $val);
Expand All @@ -78,7 +87,7 @@ private static function buildAttribVal($name, $val)
/**
* Convert array attribute value to string
*
* This function is not meant for data attributes
* Currently this method is only used for the "style" attribute
*
* @param string $name attribute name ("style")
* @param string[] $values key/value for style
Expand All @@ -102,29 +111,36 @@ private static function buildAttribValArray($name, $values = array())
* Convert boolean attribute value to string
*
* @param string $name Attribute name
* @param bool $value true|false
* @param mixed $value Attribute value
*
* @return string|null
*/
private static function buildAttribValBool($name, $value = true)
{
// opposite behavior of filter_var FILTER_VALIDATE_BOOLEAN
// treat as true unless explicitly falsy value
$boolValue = !\in_array(\strtolower((string) $value), array('', 0, '0', 'false', 'no', 'off'), true);
if (\in_array($name, Html::$htmlBoolAttrEnum, true) === false) {
return $boolValue
? $name // even if not a recognized boolean attribute... we will output name="name"
: null; // null = attribute won't be output
}
// non "true"/"false" bool attributes
$enumValues = array(
'autocapitalize' => array('on','off'), // also takes other values (sentences, words, characters)
'autocomplete' => array('on','off'), // other values also accepted
'translate' => array('yes','no'),
'autocapitalize' => array('off', 'on', true), // also takes other values ("sentences", "words", "characters")
'autocomplete' => array('off', 'on', true), // other values also accepted
'translate' => array('no', 'yes', false),
);
if (isset($enumValues[$name])) {
return $value
? $enumValues[$name][0]
: $enumValues[$name][1];
}
if (\in_array($name, Html::$htmlBoolAttrEnum, true)) {
if (isset($enumValues[$name]) === false) {
// "true" or "false"
return \json_encode($value);
return \json_encode($boolValue);
}
$opts = $enumValues[$name];
if ($opts[2] && \is_string($value)) {
// attribute may be "arbitrary" value
return $value;
}
return $value
? $name // even if not a recognized boolean attribute
: null; // null = attribute won't be output
return $opts[(int) $boolValue];
}

/**
Expand Down Expand Up @@ -164,25 +180,44 @@ private static function buildAttribValClass($values)
* @param string $name Attribute name
* @param mixed $val Attribute value
*
* @return string 'class', data', 'array', 'bool', 'string', or 'null'
* @return string 'array', 'bool', 'class', data', 'id', 'null', 'string'
*/
private static function getType($name, $val)
private static function getAttribType($name, $val)
{
if (\substr($name, 0, 5) === 'data-') {
return 'data';
}
if (self::isAttribBool($name, $val)) {
return 'bool';
}
if ($val === null) {
return 'null';
}
if (\in_array($name, array('class', 'id'), true)) {
return $name;
}
if (\is_bool($val)) {
return 'bool';
}
if (\is_array($val)) {
return 'array';
}
return 'string';
}

/**
* Treat this attribute as boolean (incl boolean enum)?
*
* @param string $name Attribute name
* @param mixed $val Attribute value
*
* @return bool
*/
private static function isAttribBool($name, $val)
{
if (\is_bool($val)) {
return true;
}
if (\in_array($name, Html::$htmlBoolAttr, true)) {
return true;
}
return \in_array($name, Html::$htmlBoolAttrEnum, true);
}
}
2 changes: 1 addition & 1 deletion src/Debug/Utility/SerializeLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private static function importLegacy(array $vals)
*/
private static function importLegacyObj(array $absValues)
{
$absValues = AbstractObject::buildObjValues($absValues);
$absValues = AbstractObject::buildValues($absValues);
/**
* @var array<string,array<string,mixed>> $absValues['methods']
* @var array<string,mixed> $meth
Expand Down
Loading

0 comments on commit 4fa87f4

Please sign in to comment.