Skip to content

Commit 6e8f182

Browse files
committed
Copy constructor info to clipboard
1 parent 2bf42c0 commit 6e8f182

File tree

4 files changed

+130
-3
lines changed

4 files changed

+130
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ But with PHPFUI\InstaDoc, you can document your site in about a minute (OK, mayb
1313

1414
## PHPFUI\InstaDoc Features
1515
* Always up to date, even with code that is not yet checked in
16+
* Send constructor information including parameters and default values to clipboard
1617
* Child and Parent class hierarchy clearly displayed and accessable
1718
* Quick access to highlighted PHP source with user selectable highlighting
1819
* Quick access to the file's git history for the local repo

src/PHPFUI/InstaDoc/Controller.php

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,30 @@ public function display(array $classPagesToShow = Controller::VALID_CLASS_PAGES,
123123
return $this->getSection('GitDiff')->generate($page, $fullClassName);
124124
}
125125
$section = new Section($this);
126-
$mainColumn->add($section->getBreadCrumbs($fullClassName));
126+
127+
$div = new \PHPFUI\GridX('div');
128+
$div->add($section->getBreadCrumbs($fullClassName));
129+
130+
$cell = new \PHPFUI\Cell();
131+
$div->add('   ');
132+
$icon = new \PHPFUI\FAIcon('far', 'clipboard');
133+
$parameters = $this->getMethodParameters($fullClassName);
134+
$hidden = new \PHPFUI\Input('text', 'clipboard', "new \\{$fullClassName}({$parameters});");
135+
$hidden->addClass('hide');
136+
$div->add($hidden);
137+
$icon->setToolTip('Send Constructor to Clipboard');
138+
$div->add($icon);
139+
$callout = new \PHPFUI\Callout('success');
140+
$callout->add('Copied!');
141+
$callout->addClass('small');
142+
$callout->addClass('hide');
143+
$icon->setAttribute('onclick', 'copyText("' . $hidden->getId() . '","' . $callout->getId() .'")');
144+
$js = 'function copyText(id,callout){$("#"+callout).toggleClass("hide");$("#"+id).toggleClass("hide").select();document.execCommand("copy");$("#"+id).toggleClass("hide");setTimeout(function(){$("#"+callout).toggleClass("hide")},2000);}';
145+
$page->addJavaScript($js);
146+
$page->setDebug(1);
147+
$div->add($callout);
148+
$mainColumn->add($div);
149+
127150
$mainColumn->add($section->getMenu($fullClassName, $classPagesToShow));
128151

129152
if (Controller::DOC_PAGE == $this->getParameter(Controller::PAGE))
@@ -403,6 +426,108 @@ public function getParameters() : array
403426
return $this->parameters;
404427
}
405428

429+
/**
430+
* Get parameters for the class and method
431+
*/
432+
public function getMethodParameters(string $className, $methodName = '__construct') : string
433+
{
434+
$reflection = new \ReflectionClass($className);
435+
if (! $reflection->hasMethod($methodName))
436+
{
437+
return '';
438+
}
439+
$method = $reflection->getMethod($methodName);
440+
441+
$info = '';
442+
$comma = '';
443+
foreach ($method->getParameters() as $parameter)
444+
{
445+
$info .= $comma;
446+
$comma = ', ';
447+
448+
if ($parameter->hasType())
449+
{
450+
$type = $parameter->getType();
451+
$info .= $type->allowsNull() ? '?' : '';
452+
$info .= $type->getName();
453+
$info .= ' ';
454+
$name = $parameter->getName();
455+
$info .= '$' . $name;
456+
}
457+
if ($parameter->isDefaultValueAvailable())
458+
{
459+
$value = $parameter->getDefaultValue();
460+
$info .= ' = ' . $this->getValueString($value);
461+
}
462+
}
463+
464+
return $info;
465+
}
466+
467+
protected function getValueString($value) : string
468+
{
469+
switch (gettype($value))
470+
{
471+
case 'array':
472+
$index = 0;
473+
$text = '[';
474+
$comma = '';
475+
476+
foreach ($value as $key => $part)
477+
{
478+
$text .= $comma;
479+
480+
if ($index !== $key)
481+
{
482+
$text .= $this->getValueString($key) . ' => ';
483+
}
484+
++$index;
485+
$text .= $this->getValueString($part);
486+
$comma = ', ';
487+
}
488+
$text .= ']';
489+
$value = $text;
490+
491+
break;
492+
493+
case 'string':
494+
$value = "'{$value}'";
495+
496+
break;
497+
498+
case 'object':
499+
$class = get_class($value);
500+
501+
if ('ReflectionNamedType' == $class)
502+
{
503+
$value = ($value->allowsNull() ? '?' : '') . $value->getName();
504+
}
505+
else
506+
{
507+
$value = $class;
508+
}
509+
510+
break;
511+
512+
case 'resource':
513+
$value = 'resource';
514+
515+
break;
516+
517+
case 'boolean':
518+
$value = $value ? 'true' : 'false';
519+
520+
break;
521+
522+
case 'NULL':
523+
$value = 'NULL';
524+
525+
break;
526+
}
527+
528+
return $value;
529+
}
530+
406531
/**
407532
* Get a section for display. Override to change layout
408533
*/

src/PHPFUI/InstaDoc/Section/CodeCommon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ protected function getValueString($value) : string
382382
}
383383
else
384384
{
385-
$value = $this->getClassName(get_class($value));
385+
$value = $this->getClassName($class);
386386
}
387387

388388
break;

tests/SectionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ public function testHomePage() : void
9898

9999
public function testInvalidPage() : void
100100
{
101-
$this->controller->setParameters($this->controller->getClassParts('\\Fred\\Flintstone\\Bedrock'));
101+
$this->expectException(\ReflectionException::class);
102+
$this->controller->setParameters($this->controller->getClassParts('\\Fred\\Flintstone\\Bedrock'));
102103
$page = $this->controller->display(\PHPFUI\InstaDoc\Controller::VALID_CLASS_PAGES, $this->controller->getPage());
103104
$this->assertValidHtml("{$page}");
104105
$this->assertNotWarningHtml("{$page}");

0 commit comments

Comments
 (0)