Skip to content

Commit 195e730

Browse files
committed
Enum: improve output when enum is method param default value
1 parent d39cc49 commit 195e730

7 files changed

Lines changed: 95 additions & 65 deletions

File tree

src/Debug/Abstraction/AbstractObject.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ private function absClean(Abstraction $abs)
282282
'collectPropertyValues',
283283
'fullyQualifyPhpDocType',
284284
'hist',
285-
// 'isAnonymous',
286285
'isTraverseOnly',
287286
'propertyOverrideValues',
288287
'reflector',
@@ -455,9 +454,18 @@ private function doAbstraction(Abstraction $abs)
455454
private function getCfgFlags()
456455
{
457456
$flagVals = \array_intersect_key(self::$cfgFlags, \array_filter($this->cfg));
458-
return \array_reduce($flagVals, function ($carry, $val) {
457+
$bitmask = \array_reduce($flagVals, function ($carry, $val) {
459458
return $carry | $val;
460459
}, 0);
460+
if ($bitmask & self::BRIEF) {
461+
$bitmask &= ~self::CASE_COLLECT;
462+
$bitmask &= ~self::CONST_COLLECT;
463+
$bitmask &= ~self::METHOD_COLLECT;
464+
$bitmask &= ~self::OBJ_ATTRIBUTE_COLLECT;
465+
$bitmask &= ~self::PROP_ATTRIBUTE_COLLECT;
466+
$bitmask &= ~self::TO_STRING_OUTPUT;
467+
}
468+
return $bitmask;
461469
}
462470

463471
/**

src/Debug/Abstraction/AbstractObjectMethodParams.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@
1818
use bdk\Debug\Abstraction\AbstractObjectHelper;
1919
use ReflectionMethod;
2020
use ReflectionParameter;
21+
use UnitEnum;
2122

2223
/**
2324
* Get method parameter info
2425
*/
2526
class AbstractObjectMethodParams
2627
{
2728
protected $abs;
29+
protected $abstracter;
2830
protected $helper;
2931

3032
private static $baseParamInfo = array(
@@ -40,10 +42,12 @@ class AbstractObjectMethodParams
4042
/**
4143
* Constructor
4244
*
43-
* @param AbstractObjectHelper $helper helper class
45+
* @param Abstracter $abstracter Abstracter
46+
* @param AbstractObjectHelper $helper helper class
4447
*/
45-
public function __construct(AbstractObjectHelper $helper)
48+
public function __construct(Abstracter $abstracter, AbstractObjectHelper $helper)
4649
{
50+
$this->abstracter = $abstracter;
4751
$this->helper = $helper;
4852
}
4953

@@ -179,7 +183,9 @@ private function getParamDefaultVal(ReflectionParameter $refParameter)
179183
$defaultValue = Abstracter::UNDEFINED;
180184
if ($refParameter->isDefaultValueAvailable()) {
181185
$defaultValue = $refParameter->getDefaultValue();
182-
if (PHP_VERSION_ID >= 50406 && $refParameter->isDefaultValueConstant()) {
186+
if ($defaultValue instanceof UnitEnum) {
187+
$defaultValue = $this->abstracter->crate($defaultValue, $this->abs['debugMethod']);
188+
} elseif (PHP_VERSION_ID >= 50406 && $refParameter->isDefaultValueConstant()) {
183189
/*
184190
getDefaultValueConstantName() :
185191
php may return something like self::CONSTANT_NAME

src/Debug/Abstraction/AbstractObjectMethods.php

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626
class AbstractObjectMethods
2727
{
28-
protected $abs;
2928
protected $abstracter;
3029
protected $helper;
3130
protected $params;
@@ -62,7 +61,7 @@ public function __construct(Abstracter $abstracter, AbstractObjectHelper $helper
6261
{
6362
$this->abstracter = $abstracter;
6463
$this->helper = $helper;
65-
$this->params = new AbstractObjectMethodParams($helper);
64+
$this->params = new AbstractObjectMethodParams($abstracter, $helper);
6665
}
6766

6867
/**
@@ -77,11 +76,10 @@ public function add(Abstraction $abs)
7776
if ($abs['isTraverseOnly']) {
7877
return;
7978
}
80-
$this->abs = $abs;
8179
$abs['cfgFlags'] & AbstractObject::METHOD_COLLECT
82-
? $this->addMethodsFull()
83-
: $this->addMethodsMin();
84-
$this->addFinish();
80+
? $this->addMethodsFull($abs)
81+
: $this->addMethodsMin($abs);
82+
$this->addFinish($abs);
8583
}
8684

8785
/**
@@ -99,19 +97,22 @@ public static function buildMethodValues($values = array())
9997
/**
10098
* Adds methods to abstraction
10199
*
100+
* @param Abstraction $abs Object Abstraction instance
101+
*
102102
* @return void
103103
*/
104-
private function addMethodsFull()
104+
private function addMethodsFull(Abstraction $abs)
105105
{
106-
$abs = $this->abs;
107106
if ($this->abstracter->getCfg('methodCache') && isset(static::$methodCache[$abs['className']])) {
108107
$abs['methods'] = static::$methodCache[$abs['className']];
109-
$this->addFinish();
108+
$this->addFinish($abs);
110109
return;
111110
}
112-
$this->addViaReflection();
113-
$this->addViaPhpDoc();
114-
$this->addImplements();
111+
$briefBak = $this->abstracter->debug->setCfg('brief', true);
112+
$this->addViaReflection($abs);
113+
$this->abstracter->debug->setCfg('brief', $briefBak);
114+
$this->addViaPhpDoc($abs);
115+
$this->addImplements($abs);
115116
if ($abs['className'] !== 'Closure') {
116117
static::$methodCache[$abs['className']] = $abs['methods'];
117118
}
@@ -120,11 +121,12 @@ private function addMethodsFull()
120121
/**
121122
* Add minimal method information to abstraction
122123
*
124+
* @param Abstraction $abs Object Abstraction instance
125+
*
123126
* @return void
124127
*/
125-
private function addMethodsMin()
128+
private function addMethodsMin(Abstraction $abs)
126129
{
127-
$abs = $this->abs;
128130
$obj = $abs->getSubject();
129131
if (\method_exists($obj, '__toString')) {
130132
$abs['methods']['__toString'] = array(
@@ -143,16 +145,17 @@ private function addMethodsMin()
143145
/**
144146
* remove phpDoc[method]
145147
*
148+
* @param Abstraction $abs Object Abstraction instance
149+
*
146150
* @return void
147151
*/
148-
private function addFinish()
152+
private function addFinish(Abstraction $abs)
149153
{
150-
$abs = $this->abs;
151154
unset($abs['phpDoc']['method']);
152155
if (isset($abs['methods']['__toString'])) {
153-
$abs['methods']['__toString']['returnValue'] = $this->toString();
156+
$abs['methods']['__toString']['returnValue'] = $this->toString($abs);
154157
}
155-
$phpDocCollect = $this->abs['cfgFlags'] & AbstractObject::PHPDOC_COLLECT;
158+
$phpDocCollect = $abs['cfgFlags'] & AbstractObject::PHPDOC_COLLECT;
156159
if ($phpDocCollect) {
157160
return;
158161
}
@@ -170,11 +173,12 @@ private function addFinish()
170173
/**
171174
* Add `implements` value to common interface methods
172175
*
176+
* @param Abstraction $abs Object Abstraction instance
177+
*
173178
* @return void
174179
*/
175-
private function addImplements()
180+
private function addImplements(Abstraction $abs)
176181
{
177-
$abs = $this->abs;
178182
$interfaceMethods = array(
179183
'ArrayAccess' => array('offsetExists','offsetGet','offsetSet','offsetUnset'),
180184
'BackedEnum' => array('from', 'tryFrom'),
@@ -196,45 +200,48 @@ private function addImplements()
196200
* "Magic" methods may be defined in a class' doc-block
197201
* If so... move this information to method info
198202
*
203+
* @param Abstraction $abs Object Abstraction instance
204+
*
199205
* @return void
200206
*
201207
* @see http://docs.phpdoc.org/references/phpdoc/tags/method.html
202208
*/
203-
private function addViaPhpDoc()
209+
private function addViaPhpDoc(Abstraction $abs)
204210
{
205-
$abs = $this->abs;
206211
$inheritedFrom = null;
207212
if (
208213
empty($abs['phpDoc']['method'])
209214
&& \array_intersect_key($abs['methods'], \array_flip(array('__call', '__callStatic')))
210215
) {
211216
// phpDoc doesn't contain any @method tags,
212217
// we've got __call and/or __callStatic method: check if parent classes have @method tags
213-
$inheritedFrom = $this->addViaPhpDocInherit();
218+
$inheritedFrom = $this->addViaPhpDocInherit($abs);
214219
}
215220
if (empty($abs['phpDoc']['method'])) {
216221
// still undefined or empty
217222
return;
218223
}
219224
foreach ($abs['phpDoc']['method'] as $phpDocMethod) {
220-
$abs['methods'][$phpDocMethod['name']] = $this->buildMethodPhpDoc($phpDocMethod, $inheritedFrom);
225+
$abs['methods'][$phpDocMethod['name']] = $this->buildMethodPhpDoc($abs, $phpDocMethod, $inheritedFrom);
221226
}
222227
}
223228

224229
/**
225230
* Inspect inherited classes until we find methods defined in PhpDoc
226231
*
232+
* @param Abstraction $abs Object Abstraction instance
233+
*
227234
* @return string|null class where found
228235
*/
229-
private function addViaPhpDocInherit()
236+
private function addViaPhpDocInherit(Abstraction $abs)
230237
{
231238
$inheritedFrom = null;
232-
$reflector = $this->abs['reflector'];
239+
$reflector = $abs['reflector'];
233240
while ($reflector = $reflector->getParentClass()) {
234241
$parsed = $this->helper->getPhpDoc($reflector);
235242
if (isset($parsed['method'])) {
236243
$inheritedFrom = $reflector->getName();
237-
$this->abs['phpDoc']['method'] = $parsed['method'];
244+
$abs['phpDoc']['method'] = $parsed['method'];
238245
break;
239246
}
240247
}
@@ -244,19 +251,22 @@ private function addViaPhpDocInherit()
244251
/**
245252
* Add methods from reflection
246253
*
254+
* @param Abstraction $abs Object Abstraction instance
255+
*
247256
* @return void
248257
*/
249-
private function addViaReflection()
258+
private function addViaReflection(Abstraction $abs)
250259
{
251-
$abs = $this->abs;
252-
$obj = $abs->getSubject();
253260
$methods = array();
254261
foreach ($abs['reflector']->getMethods() as $refMethod) {
255-
$info = $this->buildMethodRef($obj, $refMethod);
262+
$info = $this->buildMethodRef($abs, $refMethod);
256263
if ($info['visibility'] === 'private' && $info['inheritedFrom']) {
257264
// getMethods() returns parent's private methods (#reasons).. we'll skip it
258265
continue;
259266
}
267+
unset($info['phpDoc']['param']);
268+
unset($info['phpDoc']['return']);
269+
\ksort($info['phpDoc']);
260270
$methodName = $refMethod->getName();
261271
$methods[$methodName] = $info;
262272
}
@@ -266,27 +276,28 @@ private function addViaReflection()
266276
/**
267277
* Build magic method info
268278
*
269-
* @param array $phpDocMethod parsed phpdoc method info
270-
* @param string $inheritedFrom classname or null
279+
* @param Abstraction $abs Object Abstraction instance
280+
* @param array $phpDocMethod parsed phpdoc method info
281+
* @param string $inheritedFrom classname or null
271282
*
272283
* @return array
273284
*/
274-
private function buildMethodPhpDoc($phpDocMethod, $inheritedFrom)
285+
private function buildMethodPhpDoc(Abstraction $abs, $phpDocMethod, $inheritedFrom)
275286
{
276287
$className = $inheritedFrom
277288
? $inheritedFrom
278-
: $this->abs['className'];
289+
: $abs['className'];
279290
return $this->buildMethodValues(array(
280291
'inheritedFrom' => $inheritedFrom,
281292
'isStatic' => $phpDocMethod['static'],
282-
'params' => $this->params->getParamsPhpDoc($this->abs, $phpDocMethod, $className),
293+
'params' => $this->params->getParamsPhpDoc($abs, $phpDocMethod, $className),
283294
'phpDoc' => array(
284295
'desc' => null,
285296
'summary' => $phpDocMethod['desc'],
286297
),
287298
'return' => array(
288299
'desc' => null,
289-
'type' => $this->helper->resolvePhpDocType($phpDocMethod['type'], $this->abs),
300+
'type' => $this->helper->resolvePhpDocType($phpDocMethod['type'], $abs),
290301
),
291302
'visibility' => 'magic',
292303
));
@@ -295,22 +306,25 @@ private function buildMethodPhpDoc($phpDocMethod, $inheritedFrom)
295306
/**
296307
* Get method info
297308
*
298-
* @param object|string $obj object (or classname) method belongs to
309+
* @param Abstraction $abs Object Abstraction instance
299310
* @param ReflectionMethod $refMethod ReflectionMethod instance
300311
*
301312
* @return array
302313
*/
303-
private function buildMethodRef($obj, ReflectionMethod $refMethod)
314+
private function buildMethodRef(Abstraction $abs, ReflectionMethod $refMethod)
304315
{
305-
// getDeclaringClass() returns LAST-declared/overridden
316+
$obj = $abs->getSubject();
317+
// get_class() returns "raw" classname
318+
// could be stdClass@anonymous/filepath.php:6$2e
319+
// $abs['className'] is a "friendly" name ... just stdClass@anonymous
306320
$className = \is_object($obj)
307321
? \get_class($obj)
308322
: $obj;
323+
// getDeclaringClass() returns LAST-declared/overridden
309324
$declaringClassName = $refMethod->getDeclaringClass()->getName();
310325
$phpDoc = $this->helper->getPhpDoc($refMethod);
311-
\ksort($phpDoc);
312-
$info = $this->buildMethodValues(array(
313-
'attributes' => $this->abs['cfgFlags'] & AbstractObject::METHOD_ATTRIBUTE_COLLECT
326+
return $this->buildMethodValues(array(
327+
'attributes' => $abs['cfgFlags'] & AbstractObject::METHOD_ATTRIBUTE_COLLECT
314328
? $this->helper->getAttributes($refMethod)
315329
: array(),
316330
'inheritedFrom' => $declaringClassName !== $className
@@ -320,34 +334,32 @@ private function buildMethodRef($obj, ReflectionMethod $refMethod)
320334
'isDeprecated' => $refMethod->isDeprecated() || isset($phpDoc['deprecated']),
321335
'isFinal' => $refMethod->isFinal(),
322336
'isStatic' => $refMethod->isStatic(),
323-
'params' => $this->params->getParams($this->abs, $refMethod, $phpDoc),
337+
'params' => $this->params->getParams($abs, $refMethod, $phpDoc),
324338
'phpDoc' => $phpDoc,
325-
'return' => $this->getReturn($refMethod, $phpDoc),
339+
'return' => $this->getReturn($abs, $refMethod, $phpDoc),
326340
'visibility' => $this->helper->getVisibility($refMethod),
327341
));
328-
unset($info['phpDoc']['param']);
329-
unset($info['phpDoc']['return']);
330-
return $info;
331342
}
332343

333344
/**
334345
* Get return type & desc
335346
*
347+
* @param Abstraction $abs Object Abstraction instance
336348
* @param ReflectionMethod $refMethod ReflectionMethod
337349
* @param array $phpDoc parsed phpDoc param info
338350
*
339351
* @return array
340352
*/
341-
private function getReturn(ReflectionMethod $refMethod, $phpDoc)
353+
private function getReturn(Abstraction $abs, ReflectionMethod $refMethod, $phpDoc)
342354
{
343355
$return = array(
344356
'desc' => null,
345357
'type' => null,
346358
);
347359
if (!empty($phpDoc['return'])) {
348360
$return = \array_merge($return, $phpDoc['return']);
349-
$return['type'] = $this->helper->resolvePhpDocType($return['type'], $this->abs);
350-
if (!($this->abs['cfgFlags'] & AbstractObject::PHPDOC_COLLECT)) {
361+
$return['type'] = $this->helper->resolvePhpDocType($return['type'], $abs);
362+
if (!($abs['cfgFlags'] & AbstractObject::PHPDOC_COLLECT)) {
351363
$return['desc'] = null;
352364
}
353365
} elseif (PHP_VERSION_ID >= 70000) {
@@ -359,11 +371,12 @@ private function getReturn(ReflectionMethod $refMethod, $phpDoc)
359371
/**
360372
* Get object's __toString value if method is not deprecated
361373
*
374+
* @param Abstraction $abs Object Abstraction instance
375+
*
362376
* @return string|Abstraction|null
363377
*/
364-
private function toString()
378+
private function toString(Abstraction $abs)
365379
{
366-
$abs = $this->abs;
367380
// abs['methods']['__toString'] may not exist if via addMethodsMin
368381
if (!empty($abs['methods']['__toString']['isDeprecated'])) {
369382
return null;

0 commit comments

Comments
 (0)