Skip to content

Remove private functions #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions accessTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);

namespace PhpStubs\WordPress\Reflection\DocBlock\Tag;

require_once(__DIR__ . '/vendor/autoload.php');

use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\DescriptionFactory;
use phpDocumentor\Reflection\DocBlock\Tags\BaseTag;
use phpDocumentor\Reflection\Types\Context;
use Webmozart\Assert\Assert;

final class Access extends BaseTag
{
protected $name = 'access';

public function __construct(Description $description = null)
{
$this->description = $description;
}

public static function create(string $body, DescriptionFactory $descriptionFactory = null, ?Context $context = null): self
{
Assert::notNull($descriptionFactory);
return new static($descriptionFactory->create($body, $context));
}

public function __toString(): string
{
return (string)$this->description;
}
}

$factory = DocBlockFactory::createInstance(['access' => Access::class]);
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"php-stubs/generator": "^0.8.3",
"phpdocumentor/reflection-docblock": "^5.3",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^9.5",
"webmozart/assert": "^1.11.0"
},
"suggest": {
"paragonie/sodium_compat": "Pure PHP implementation of libsodium",
Expand All @@ -23,7 +24,8 @@
},
"autoload-dev": {
"classmap": [
"tests/"
"tests/",
"accessTag.php"
]
},
"minimum-stability": "stable",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
parameters:
level: 8
paths:
- accessTag.php
- finder.php
- visitor.php
- tests/
Expand Down
40 changes: 40 additions & 0 deletions visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\DocBlock\Description;
use phpDocumentor\Reflection\DocBlock\Tag;
use phpDocumentor\Reflection\DocBlock\Tags\Param;
use phpDocumentor\Reflection\DocBlock\Tags\Return_;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
Expand All @@ -15,6 +16,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser;
use StubsGenerator\NodeVisitor;

abstract class WithChildren
Expand Down Expand Up @@ -296,6 +298,11 @@ public function enterNode(Node $node)
$this->additionalTags[ $symbolName ] = $additions;
}

if ($this->isPrivateFunction($node) === true) {
$node->setAttribute('isPrivateFunction', true);
return null;
}

$additions = $this->getAdditionalTagsFromMap($symbolName);

if (count($additions) > 0) {
Expand All @@ -305,6 +312,17 @@ public function enterNode(Node $node)
return null;
}

public function leaveNode(Node $node, bool $preserveStack = false)
{
if ($node->getAttribute('isPrivateFunction') === true) {
if (!$preserveStack) {
array_pop($this->stack);
}
return NodeTraverser::REMOVE_NODE;
}
return parent::leaveNode($node, $preserveStack);
}

private static function getNodeName(Node $node): string
{
if ((($node instanceof Function_) || ($node instanceof ClassMethod) || ($node instanceof Class_)) && $node->name instanceof Identifier) {
Expand Down Expand Up @@ -889,4 +907,26 @@ private static function isOptional(string $description): bool
|| (stripos($description, 'Defaults to ') !== false)
;
}

private function isPrivateFunction(Node $node): bool
{
if (!($node instanceof Function_)) {
return false;
}

$docComment = $node->getDocComment();
if (!($docComment instanceof Doc)) {
return false;
}

$docblock = $this->docBlockFactory->create($docComment->getText());
$access = array_map(
static function (Tag $tag): string {
return $tag->__toString();
},
$docblock->getTagsByName('access')
);

return in_array('private', $access, true);
}
};
Loading