-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix(OC): Use @inheritDoc for OCP implementations #47798
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
Draft
provokateurin
wants to merge
3
commits into
master
Choose a base branch
from
fix/oc/inheritdoc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| */ | ||
|
|
||
| include __DIR__ . '/../lib/composer/autoload.php'; | ||
| include __DIR__ . '/../build/integration/vendor/autoload.php'; | ||
| include __DIR__ . '/../3rdparty/autoload.php'; | ||
|
|
||
| use PhpParser\Node\Name\FullyQualified; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use PhpParser\Node\Stmt\Namespace_; | ||
| use PhpParser\NodeTraverser; | ||
| use PhpParser\NodeVisitor\CloningVisitor; | ||
| use PhpParser\NodeVisitor\NameResolver; | ||
| use PhpParser\ParserFactory; | ||
| use PhpParser\PrettyPrinter; | ||
|
|
||
| $parser = (new ParserFactory())->createForHostVersion(); | ||
|
|
||
| $nodeTraverser = new NodeTraverser(); | ||
| $nodeTraverser->addVisitor(new CloningVisitor()); | ||
| $nodeTraverser->addVisitor(new NameResolver()); | ||
|
|
||
| $prettyPrinter = new PrettyPrinter\Standard(); | ||
|
|
||
| $iterators = [ | ||
| new RecursiveIteratorIterator(new RecursiveDirectoryIterator('lib/private')), | ||
| new RecursiveIteratorIterator(new RecursiveDirectoryIterator('core')), | ||
| ]; | ||
| foreach ($iterators as $iterator) { | ||
| /** @var SplFileInfo $info */ | ||
| foreach ($iterator as $info) { | ||
| if ($info->getType() !== 'file' || $info->getExtension() !== 'php') { | ||
| continue; | ||
| } | ||
|
|
||
| $code = file_get_contents($info->getRealPath()); | ||
| $oldStmts = $parser->parse($code); | ||
| $oldTokens = $parser->getTokens(); | ||
| $newStmts = $nodeTraverser->traverse($oldStmts); | ||
|
|
||
| $changed = false; | ||
| foreach ($newStmts as $stmt) { | ||
| if (!$stmt instanceof Namespace_) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($stmt->stmts as $childClass) { | ||
| if (!$childClass instanceof Class_) { | ||
| continue; | ||
| } | ||
|
|
||
| $childClassName = $childClass->namespacedName->toString(); | ||
| if (!str_starts_with($childClassName, 'OC\\')) { | ||
| continue; | ||
| } | ||
|
|
||
| /** @var FullyQualified $impl */ | ||
| foreach ($childClass->implements as $impl) { | ||
| $parentClassName = $impl->toString(); | ||
| if ($parentClassName === 'OCP\\Capabilities\\ICapability' || $parentClassName === 'OCP\\Capabilities\\IPublicCapability' || !str_starts_with($parentClassName, 'OCP\\')) { | ||
| continue; | ||
| } | ||
|
|
||
| $parentClass = new ReflectionClass($parentClassName); | ||
| $parentMethods = $parentClass->getMethods(); | ||
|
|
||
| foreach ($childClass->getMethods() as $childMethod) { | ||
| foreach ($parentMethods as $parentMethod) { | ||
| if (strtolower($childMethod->name->name) !== strtolower($parentMethod->name)) { | ||
| continue; | ||
| } | ||
|
|
||
| $childMethod->setAttribute('comments', []); | ||
| $changed = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if ($changed) { | ||
| file_put_contents($info->getRealPath(), $prettyPrinter->printFormatPreserving($newStmts, $oldStmts, $oldTokens)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| */ | ||
|
|
||
| use PhpParser\Node\Name; | ||
| use PhpParser\Node\Stmt\Class_; | ||
| use Psalm\CodeLocation; | ||
| use Psalm\Issue\InvalidDocblock; | ||
| use Psalm\IssueBuffer; | ||
| use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent; | ||
|
|
||
| include __DIR__ . '/../../3rdparty/autoload.php'; | ||
|
|
||
| class OcInheritDoc implements Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface { | ||
| public static function afterClassLikeVisit(AfterClassLikeVisitEvent $event): void { | ||
| $childClass = $event->getStmt(); | ||
| $statementsSource = $event->getStatementsSource(); | ||
|
|
||
| if (!$childClass instanceof Class_) { | ||
| return; | ||
| } | ||
|
|
||
| $childClassName = $childClass->getAttribute('namespacedName')?->toString() ?? ''; | ||
| if (!str_starts_with($childClassName, 'OC\\')) { | ||
| return; | ||
| } | ||
|
|
||
| /** @var Name $impl */ | ||
| foreach ($childClass->implements as $impl) { | ||
| $parentClassName = $impl->getAttribute('resolvedName') ?? ''; | ||
| if ($parentClassName === 'OCP\\Capabilities\\ICapability' || $parentClassName === 'OCP\\Capabilities\\IPublicCapability' || !str_starts_with($parentClassName, 'OCP\\')) { | ||
| continue; | ||
| } | ||
|
|
||
| $parentClass = new ReflectionClass($parentClassName); | ||
| $parentMethods = $parentClass->getMethods(); | ||
|
|
||
| foreach ($childClass->getMethods() as $childMethod) { | ||
| foreach ($parentMethods as $parentMethod) { | ||
| if (strtolower($childMethod->name->name) !== strtolower($parentMethod->name)) { | ||
| continue; | ||
| } | ||
|
|
||
| $doc = $childMethod->getDocComment(); | ||
| if ($doc !== null) { | ||
| IssueBuffer::maybeAdd( | ||
| new InvalidDocblock( | ||
| 'Docblock for OCP implementation must be empty to inherit the interface.', | ||
| new CodeLocation($statementsSource, $childMethod) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The script will be removed from the PR anyway. When it is fixed once it is enforced by psalm so the script will never be needed again as everyone has to set the correct doc blocks in their PRs already.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry 🙈
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries, I never said what is going to happen with this PR.
I plan to fix the issues that were found in separate PRs and once that is done we can merge the PR.