Skip to content

Commit

Permalink
Merge pull request #57 from adhocore/docs-collapsible
Browse files Browse the repository at this point in the history
Docs collapsible
  • Loading branch information
adhocore authored Sep 20, 2018
2 parents a443885 + 0485057 commit b2c2fa6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
15 changes: 13 additions & 2 deletions resources/docs/docs.twig
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,24 @@ use {{class.classFqcn}};
> {{method.title}}
{% endif %}

<details><summary>Details …</summary>
<div>

```php
{{method.isStatic ? '::' : ''}}{{name}}({{method.params|join(', ')}}){{method.return ? ': ' ~ method.return : ''}}
{% set params = call('array_column', method.params, 0) %}
{{method.isStatic ? '::' : ''}}{{name}}({{params|join(', ')}}){{method.return ? ': ' ~ method.return[0] : ''}}
```
{% if method.texts %}

{{method.texts|join("\n")|replace({'<code>':"```php",'</code>':"```"})|raw}}
{{method.texts|join("\n\n")|replace({'<code>':"```php", '</code>':"```"})|raw|nl2br}}
{% endif %}
{% if method.throws %}

> _Throws_ **{{method.throws[0]}}** {{ method.throws[1] ? '_' ~ method.throws[1] ~ '_' : '' }}
{% endif %}

</div>
</details>
{% endfor %}

**[⬆ back to top](#the-top)**
Expand Down
4 changes: 2 additions & 2 deletions src/Console/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ protected function prepareProjectPath(): string
}

if (\is_dir($path)) {
$this->projectExists($io);
$this->projectExists($io, $path);
} else {
\mkdir($path, 0777, true);
}

return $path;
}

protected function projectExists(Interactor $io)
protected function projectExists(Interactor $io, string $path)
{
if (!$this->force && !$this->sync) {
throw new InvalidArgumentException(
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/TwigGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ protected function initTwig()
return \gmdate($f ?? 'Y-m-d H:i:s');
}));

$this->twig->addFilter(new \Twig_SimpleFilter('call', function ($fn) {
return $fn(\array_slice(\func_get_args(), 1));
$this->twig->addFunction(new \Twig_Function('call', function ($fn) {
return $fn(...\array_slice(\func_get_args(), 1));
}));
}

Expand Down
18 changes: 10 additions & 8 deletions src/Util/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function forReflectionMethod(\ReflectionMethod $method): array
$parser = new DocBlock($method);
$texts = $parser->texts();
$title = \array_shift($texts);
$throws = $parser->first('throws');

$metadata = [
'name' => $method->name,
Expand All @@ -73,42 +74,43 @@ public function forReflectionMethod(\ReflectionMethod $method): array
'isPublic' => $method->isPublic(),
'isAbstract' => $method->isAbstract(),
'maybeMagic' => \substr($method->name, 0, 2) === '__',
'throws' => $throws ? \explode(' ', \trim($throws->getValue()), 2) : [],
'title' => $title,
'texts' => $texts,
];

$params = [];
foreach ($parser->find('param') as $param) {
if (\preg_match('/(.*)\$(\w+)/', $param->getValue(), $match)) {
$params[$match[2]] = \trim($match[1]);
if (\preg_match('/(.*)\$(\w+)(.*)/', $param->getValue(), $match)) {
$params[$match[2]] = [\trim($match[1]), \trim($match[3])];
}
}

if (null !== $return = $parser->first('return')) {
$return = \preg_replace('/(\S+)(.*)/', '$1', $return->getValue());
$return = \explode(' ', \trim($return->getValue()), 2);
}

return $metadata + $this->getMethodParameters($method, $params, $return ?? '');
return $metadata + $this->getMethodParameters($method, $params, $return ?? []);
}

protected function getMethodParameters(\ReflectionMethod $method, array $docParams, string $return)
protected function getMethodParameters(\ReflectionMethod $method, array $docParams, array $return)
{
$params = [];
$parser = new DocBlock($method);

foreach ($method->getParameters() as $param) {
$name = $param->name;
if (!$param->hasType()) {
$params[] = \trim(($docParams[$name] ?? '') . " \$$name");
$params[] = [\trim(($docParams[$name][0] ?? '') . " \$$name"), $docParams[$name][1] ?? ''];

continue;
}

$params[] = $this->getRealType($param) . " \$$name";
$params[] = [$this->getRealType($param) . " \$$name", $docParams[$name][1] ?? ''];
}

if ($returnType = $method->getReturnType()) {
$return = $this->getRealType($returnType);
$return = [$this->getRealType($returnType), $return[1] ?? ''];
}

return \compact('params', 'return');
Expand Down
14 changes: 4 additions & 10 deletions tests/Util/GitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,11 @@ class GitTest extends TestCase
{
public function testGetConfig()
{
$git = new Git;
$git = new Git;
$conf = $git->getConfig();

$this->assertArraySubset([
'core.repositoryformatversion' => '0',
'core.filemode' => 'true',
'core.bare' => 'false',
'core.logallrefupdates' => 'true',
'remote.origin.url' => 'https://github.com/adhocore/phint.git',
'remote.origin.fetch' => '+refs/heads/master:refs/remotes/origin/master',
'' => '',
], $git->getConfig());
$this->assertArrayHasKey('remote.origin.url', $conf);
$this->assertContains('adhocore/phint.git', $conf['remote.origin.url']);
}

public function testGetConfigOnSpecificKey()
Expand Down
5 changes: 3 additions & 2 deletions tests/Util/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ public function testForMethod()
'isPublic' => true,
'isAbstract' => false,
'maybeMagic' => false,
'throws' => [],
'title' => null,
'texts' => [],
'params' => [
'string $classFqcn',
['string $classFqcn', ''],
],
'return' => 'array',
'return' => ['array', ''],
], $result);
}
}

0 comments on commit b2c2fa6

Please sign in to comment.