ILIAS  release_8 Revision v8.24
ChangeLicenseHeader.php
Go to the documentation of this file.
1<?php
2
20
21use PhpParser\Node;
22use Rector\Core\Rector\AbstractRector;
23use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
24use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
25use PhpParser\Comment;
26use Rector\NodeTypeResolver\Node\AttributeKey as AttributeKeys;
27
28final class ChangeLicenseHeader extends AbstractRector
29{
30 public const EXISTING_LICENSE_PATTERN = '(copyright|Copyright|GPL-3\.0|GPLv3|LICENSE)';
31 public const IGNORE_SUBPATHS = '(lib|vendor|data|Customizing)';
32 private string $license_header_default = "/**
33 * This file is part of ILIAS, a powerful learning management system
34 * published by ILIAS open source e-Learning e.V.
35 *
36 * ILIAS is licensed with the GPL-3.0,
37 * see https://www.gnu.org/licenses/gpl-3.0.en.html
38 * You should have received a copy of said license along with the
39 * source code, too.
40 *
41 * If this is not the case or you just want to try ILIAS, you'll find
42 * us at:
43 * https://www.ilias.de
44 * https://github.com/ILIAS-eLearning
45 *
46 *********************************************************************/
47";
48
49 private Comment $standard_comment;
50 private array $previous_search = [
51 Node\Expr\Include_::class,
52 Node\Stmt\Use_::class,
53 Node\Stmt\Namespace_::class,
54 Node\Name::class,
55 Node\Stmt\Class_::class,
56 Node\Stmt\Expression::class,
57 Node\Stmt\Declare_::class
58 ];
59
60 public function __construct()
61 {
62 $this->standard_comment = new Comment($this->license_header_default);
63 }
64
68 public function getNodeTypes(): array
69 {
70 return [
71 Node\Stmt\Class_::class,
72 Node\Stmt\Interface_::class,
73 Node\Stmt\Trait_::class
74 ];
75 }
76
80 public function refactor(Node $node): ?Node
81 {
82 if (preg_match(self::IGNORE_SUBPATHS, $this->file->getSmartFileInfo()->getPathname()) > 0) {
83 return $node;
84 }
85 $node->setAttribute('comments', $this->filterComments($node));
86 $current = $node;
87 $previous = $node->getAttribute(AttributeKeys::PREVIOUS_NODE);
88 while (is_object($previous) && in_array(get_class($previous), $this->previous_search)) {
89 if ($previous instanceof \PhpParser\Node\Name) {
90 $previous = $previous->getAttribute(AttributeKeys::PARENT_NODE);
91 }
92 $current = $previous;
93 $current->setAttribute(
94 AttributeKeys::COMMENTS,
95 $this->filterComments($current)
96 );
97 $previous = $current->getAttribute(AttributeKeys::PREVIOUS_NODE);
98 }
99
100 $current->setAttribute(AttributeKeys::COMMENTS, $this->filterComments($current, [$this->standard_comment]));
101
102 return $node;
103 }
104
109 private function filterComments(Node $node, array $default = []): array
110 {
111 foreach ($node->getComments() as $comment) {
112 if (preg_match(self::EXISTING_LICENSE_PATTERN, $comment->getText()) > 0) {
113 continue;
114 }
115 $default[] = $comment;
116 }
117 return $default;
118 }
119
120 public function getRuleDefinition(): RuleDefinition
121 {
122 return new RuleDefinition(
123 'Adds or replaces a license-header in each class-file',
124 [
125 new CodeSample(
126 // code before
127 '',
128 // code after
129 ''
130 ),
131 ]
132 );
133 }
134}
$comment
Definition: buildRTE.php:72
filterComments(Node $node, array $default=[])
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...