ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ChangeLicenseHeader.php
Go to the documentation of this file.
1 <?php
2 
19 namespace ILIAS\CI\Rector;
20 
21 use PhpParser\Node;
26 use Rector\NodeTypeResolver\Node\AttributeKey as AttributeKeys;
27 
28 final 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\Stmt\If_::class,
52  Node\Expr\Empty_::class,
53  Node\Stmt\Global_::class,
54  Node\Expr\Include_::class,
55  Node\Stmt\Use_::class,
56  Node\Stmt\Namespace_::class,
57  Node\Name::class,
58  Node\Stmt\Class_::class,
59  Node\Stmt\Expression::class,
60  Node\Stmt\Declare_::class,
61  ];
62 
63  public function __construct()
64  {
65  $this->standard_comment = new Comment($this->license_header_default);
66  }
67 
71  public function getNodeTypes(): array
72  {
73  return [
74  Node\Stmt\Class_::class,
75  Node\Stmt\Interface_::class,
76  Node\Stmt\Trait_::class
77  ];
78  }
79 
83  public function refactor(Node $node)
84  {
85  if (preg_match(self::IGNORE_SUBPATHS, $this->file->getFilePath()) > 0) {
86  return $node;
87  }
88  $node->setAttribute('comments', $this->filterComments($node));
89  $current = $node;
90  $previous = $node->getAttribute(AttributeKeys::PREVIOUS_NODE);
91  while (is_object($previous) && in_array($previous::class, $this->previous_search)) {
92  if ($previous instanceof \PhpParser\Node\Name) {
93  $previous = $previous->getAttribute(AttributeKeys::PARENT_NODE);
94  }
95  if ($previous instanceof Node\Expr\Empty_) {
96  $this->removeNode($previous);
97  }
98  $current = $previous;
99  $current->setAttribute(
100  AttributeKeys::COMMENTS,
101  $this->filterComments($current)
102  );
103  $previous = $current->getAttribute(AttributeKeys::PREVIOUS_NODE);
104  }
105 
106  $current->setAttribute(AttributeKeys::COMMENTS, $this->filterComments($current, [$this->standard_comment]));
107 
108  return $node;
109  }
110 
114  private function filterComments(Node $node, array $default = []): array
115  {
116  foreach ($node->getComments() as $comment) {
117  if (preg_match(self::EXISTING_LICENSE_PATTERN, $comment->getText()) > 0) {
118  continue;
119  }
120  $default[] = $comment;
121  }
122  return $default;
123  }
124 
125  public function getRuleDefinition(): RuleDefinition
126  {
127  return new RuleDefinition(
128  'Adds or replaces a license-header in each class-file',
129  [
130  new CodeSample(
131  // code before
132  '',
133  // code after
134  ''
135  ),
136  ]
137  );
138  }
139 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
filterComments(Node $node, array $default=[])
$comment
Definition: buildRTE.php:72