Skip to content

Commit

Permalink
php 8.4 property hooks. properties now have hooks: array containing '…
Browse files Browse the repository at this point in the history
…get' and/or 'set' and 'isVirtual': (bool)

php 8.4 private/protected get
new property css classes:  getHook, isDeprecated, isVirtual, isWriteOnly, protected-set, private-set, setHook
  • Loading branch information
bkdotcom committed Sep 9, 2024
1 parent 728f5bc commit ea54194
Show file tree
Hide file tree
Showing 55 changed files with 1,215 additions and 419 deletions.
19 changes: 14 additions & 5 deletions src/Debug/Abstraction/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use bdk\Debug\Abstraction\Object\Helper;
use bdk\Debug\Abstraction\Object\Methods;
use bdk\Debug\Abstraction\Object\Properties;
use bdk\Debug\Abstraction\Object\PropertiesInstance;
use bdk\Debug\Abstraction\Object\Subscriber;
use ReflectionClass;
use ReflectionEnumUnitCase;
Expand All @@ -36,6 +37,7 @@
* @property-read Helper $helper
* @property-read Methods $methods
* @property-read Properties $properties
* @property-read PropertiesInstance $properties
*/
class AbstractObject extends AbstractComponent
{
Expand Down Expand Up @@ -66,14 +68,15 @@ class AbstractObject extends AbstractComponent
const OBJ_ATTRIBUTE_OUTPUT = 8;

const PARAM_ATTRIBUTE_COLLECT = 1048576;
const PARAM_ATTRIBUTE_OUTPUT = 2097152;
const PARAM_ATTRIBUTE_OUTPUT = 2097152; // 2^21

const PHPDOC_COLLECT = 1; // 2^0
const PHPDOC_OUTPUT = 2;

// PROPERTIES (2^13 - 2^14)
const PROP_ATTRIBUTE_COLLECT = 8192; // 2^13
const PROP_ATTRIBUTE_OUTPUT = 16384; // 2^14
const PROP_VIRTUAL_VALUE_COLLECT = 33554432; // 2^25

const TO_STRING_OUTPUT = 16; // 2^4

Expand Down Expand Up @@ -114,6 +117,7 @@ class AbstractObject extends AbstractComponent
// PROPERTIES
'propAttributeCollect' => self::PROP_ATTRIBUTE_COLLECT,
'propAttributeOutput' => self::PROP_ATTRIBUTE_OUTPUT,
'propVirtualValueCollect' => self::PROP_VIRTUAL_VALUE_COLLECT,

'toStringOutput' => self::TO_STRING_OUTPUT,
);
Expand All @@ -132,6 +136,8 @@ class AbstractObject extends AbstractComponent
protected $methods;
/** @var Properties */
protected $properties;
/** @var PropertiesInstance */
protected $propertiesInstance;

/** @var list<string> */
protected $readOnly = array(
Expand All @@ -142,6 +148,7 @@ class AbstractObject extends AbstractComponent
'helper',
'methods',
'properties',
'propertiesInstance',
);

/**
Expand All @@ -164,7 +171,7 @@ class AbstractObject extends AbstractComponent
* @var array<string,mixed> Array of key/values
*/
protected static $values = array(
'cfgFlags' => 0, // will default to everything sans "brief"
'cfgFlags' => 0, // will default to everything sans "brief" & 'virtualValueCollect'
'className' => '',
'debugMethod' => '',
'interfacesCollapse' => array(), // cfg.interfacesCollapse
Expand Down Expand Up @@ -195,6 +202,7 @@ public function __construct(Abstracter $abstracter)
$this->constants = new Constants($this);
$this->methods = new Methods($this);
$this->properties = new Properties($this);
$this->propertiesInstance = new PropertiesInstance($this);
$this->definition = new Definition($this);
if ($abstracter->debug->parentInstance === null) {
// we only need to subscribe to these events from root channel
Expand Down Expand Up @@ -239,10 +247,10 @@ public function getAbstraction($obj, $method = null, array $hist = array())
public static function buildValues(array $values = array())
{
if (self::$values['cfgFlags'] === 0) {
// calculate default cfgFlags (everything except for "brief")
// calculate default cfgFlags (everything except for "brief" & virtualValueCollect)
self::$values['cfgFlags'] = \array_reduce(self::$cfgFlags, static function ($carry, $val) {
return $carry | $val;
}, 0) & ~self::BRIEF;
}, 0) & ~self::BRIEF & ~self::PROP_VIRTUAL_VALUE_COLLECT;
}
return \array_merge(self::$values, $values);
}
Expand Down Expand Up @@ -297,7 +305,7 @@ private function doAbstraction(ObjectAbstraction $abs)
$this->addTraverseValues($abs);
}
$this->methods->addInstance($abs); // method static variables
$this->properties->addInstance($abs);
$this->propertiesInstance->add($abs);
/*
Debug::EVENT_OBJ_ABSTRACT_END subscriber has free reign to modify abstraction values
*/
Expand Down Expand Up @@ -352,6 +360,7 @@ protected function getAbstractionValues(ReflectionClass $reflector, $obj, $metho
protected function getCfgFlags()
{
$flagVals = \array_intersect_key(self::$cfgFlags, \array_filter($this->cfg));
// see Abstracter::__construct which sets initial/default cfgFlags cfg vales
$bitmask = \array_reduce($flagVals, static function ($carry, $val) {
return $carry | $val;
}, 0);
Expand Down
3 changes: 3 additions & 0 deletions src/Debug/Abstraction/Abstracter.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class Abstracter extends AbstractComponent
/** @var array>string,mixed> */
protected $cfg = array(
'brief' => false, // collect & output less details
// see also AbstractObject::$cfgFlags where each key
// can be set to true/false as a cfg value here
'fullyQualifyPhpDocType' => false,
'interfacesCollapse' => array(
'ArrayAccess',
Expand Down Expand Up @@ -132,6 +134,7 @@ public function __construct(Debug $debug, $cfg = array())
),
array(
'brief' => false,
'propVirtualValueCollect' => false,
)
);
$this->setCfg(\array_merge($this->cfg, $cfg));
Expand Down
34 changes: 34 additions & 0 deletions src/Debug/Abstraction/Object/AbstractInheritable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use bdk\Debug\Abstraction\AbstractObject;
use ReflectionClass;
use Reflector;

/**
* Base class for collecting constants, properties, & methods
Expand All @@ -26,6 +27,9 @@ abstract class AbstractInheritable
/** @var Helper */
protected $helper;

/** @var array<string,mixed> */
protected static $values = array();

/**
* Constructor
*
Expand All @@ -37,6 +41,36 @@ public function __construct(AbstractObject $abstractObject)
$this->helper = $abstractObject->helper;
}

/**
* Build (constant,method,property) info by passing values
*
* @param array $values Values to apply
*
* @return array
*/
public static function buildValues($values = array())
{
return \array_merge(static::$values, $values);
}

/**
* Get constant/method/property visibility
*
* @param Reflector $reflector Reflection instance
*
* @return list<string>|'public'|'private'|'protected'
*/
protected static function getVisibility(Reflector $reflector)
{
if ($reflector->isPrivate()) {
return 'private';
}
if ($reflector->isProtected()) {
return 'protected';
}
return 'public';
}

/**
* Pass reflector and ancestor reflectors to callable
*
Expand Down
3 changes: 3 additions & 0 deletions src/Debug/Abstraction/Object/Abstraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ private function getCombinedValue($key)
$value = isset($this->values[$key])
? $this->values[$key]
: null;
if (\in_array($key, self::$keysTemp)) {
return $value;
}
$classVal = $this->inheritValue($key)
? $this->inherited[$key]
: array();
Expand Down
27 changes: 4 additions & 23 deletions src/Debug/Abstraction/Object/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ class Constants extends AbstractInheritable
/** @var bool */
private $attributeCollect = true;

/** @var bool */
private $phpDocCollect = true;

/** @var array<string,mixed> */
private static $baseConstInfo = array(
protected static $values = array(
'attributes' => array(),
'declaredLast' => null,
'declaredOrig' => null,
Expand Down Expand Up @@ -70,7 +67,6 @@ public function add(Abstraction $abs)
$this->abs = $abs;
$this->constants = array();
$this->attributeCollect = ($abs['cfgFlags'] & AbstractObject::CONST_ATTRIBUTE_COLLECT) === AbstractObject::CONST_ATTRIBUTE_COLLECT;
$this->phpDocCollect = ($abs['cfgFlags'] & AbstractObject::PHPDOC_COLLECT) === AbstractObject::PHPDOC_COLLECT;
/*
We trace our lineage to learn where constants are inherited from
(set brief to avoid recursion with enum values)
Expand Down Expand Up @@ -102,7 +98,6 @@ public function addCases(Abstraction $abs)
return;
}
$this->attributeCollect = ($abs['cfgFlags'] & AbstractObject::CASE_ATTRIBUTE_COLLECT) === AbstractObject::CASE_ATTRIBUTE_COLLECT;
$this->phpDocCollect = ($abs['cfgFlags'] & AbstractObject::PHPDOC_COLLECT) === AbstractObject::PHPDOC_COLLECT;
$cases = array();
foreach ($abs['reflector']->getCases() as $refCase) {
$name = $refCase->getName();
Expand All @@ -111,18 +106,6 @@ public function addCases(Abstraction $abs)
$abs['cases'] = $cases;
}

/**
* Build constant info by passing values
*
* @param array $values Values to apply
*
* @return array
*/
public static function buildValues($values = array())
{
return \array_merge(static::$baseConstInfo, $values);
}

/**
* Get constants (php < 7.1)
*
Expand Down Expand Up @@ -192,7 +175,7 @@ private function getCaseRefInfo(ReflectionEnumUnitCase $refCase)
'value' => $refCase instanceof ReflectionEnumBackedCase
? $refCase->getBackingValue()
: Abstracter::UNDEFINED,
'visibility' => $this->helper->getVisibility($refCase),
'visibility' => $this->getVisibility($refCase),
);
}

Expand All @@ -216,13 +199,11 @@ private function getConstantRefInfo(ReflectionClassConstant $refConstant)
'attributes' => $this->attributeCollect
? $this->helper->getAttributes($refConstant)
: array(),
'isFinal' => PHP_VERSION_ID >= 80100
? $refConstant->isFinal()
: false,
'isFinal' => PHP_VERSION_ID >= 80100 && $refConstant->isFinal(),
'phpDoc' => $phpDoc,
'type' => $type,
'value' => $value,
'visibility' => $this->helper->getVisibility($refConstant),
'visibility' => $this->getVisibility($refConstant),
));
}
}
2 changes: 1 addition & 1 deletion src/Debug/Abstraction/Object/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Definition
protected static $values = array(
'attributes' => array(),
'cases' => array(),
'cfgFlags' => 0, // __constructor will set to everything sans "brief"
'cfgFlags' => 0, // __constructor will set to everything sans "brief" and "propVirtualValueCollect"
// definition will collect with all options
'className' => "\x00default\x00",
'constants' => array(),
Expand Down
18 changes: 0 additions & 18 deletions src/Debug/Abstraction/Object/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,6 @@ public function isTraverseOnly(ObjectAbstraction $abs)
return false;
}

/**
* Get constant/method/property visibility
*
* @param Reflector $reflector Reflection instance
*
* @return 'public'|'private'|'protected'
*/
public static function getVisibility(Reflector $reflector)
{
if ($reflector->isPrivate()) {
return 'private';
}
if ($reflector->isProtected()) {
return 'protected';
}
return 'public';
}

/**
* Get Constant, Property, or Parameter's type or Method's return type
* Priority given to phpDoc type, followed by reflection type (if available)
Expand Down
16 changes: 2 additions & 14 deletions src/Debug/Abstraction/Object/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Methods extends AbstractInheritable
protected $params;

/** @var array<string,mixed> */
private static $baseMethodInfo = array(
protected static $values = array(
'attributes' => array(),
'declaredLast' => null,
'declaredOrig' => null,
Expand Down Expand Up @@ -116,18 +116,6 @@ public function addInstance(Abstraction $abs)
}
}

/**
* Return method info array
*
* @param array $values values to apply
*
* @return array
*/
public static function buildValues(array $values = array())
{
return \array_merge(static::$baseMethodInfo, $values);
}

/**
* Get object's __toString value if method is not deprecated
*
Expand Down Expand Up @@ -388,7 +376,7 @@ private function addViaRefBuildInit(Abstraction $abs, ReflectionMethod $refMetho
'desc' => $returnTag['desc'],
'type' => $this->helper->getType($returnTag['type'], $refMethod),
),
'visibility' => $this->helper->getVisibility($refMethod),
'visibility' => $this->getVisibility($refMethod),
));
}
}
Loading

0 comments on commit ea54194

Please sign in to comment.