diff --git a/scripts/Rector/ChangeLicenseHeader.php b/scripts/Rector/ChangeLicenseHeader.php deleted file mode 100755 index 52be019ec1aa..000000000000 --- a/scripts/Rector/ChangeLicenseHeader.php +++ /dev/null @@ -1,139 +0,0 @@ -standard_comment = new Comment($this->license_header_default); - } - - /** - * @return class-string[] - */ - public function getNodeTypes(): array - { - return [ - Node\Stmt\Class_::class, - Node\Stmt\Interface_::class, - Node\Stmt\Trait_::class - ]; - } - - /** - * @param Node\Stmt\Global_ $node - */ - public function refactor(Node $node) - { - if (preg_match(self::IGNORE_SUBPATHS, $this->file->getFilePath()) > 0) { - return $node; - } - $node->setAttribute('comments', $this->filterComments($node)); - $current = $node; - $previous = $node->getAttribute(AttributeKeys::PREVIOUS_NODE); - while (is_object($previous) && in_array($previous::class, $this->previous_search)) { - if ($previous instanceof \PhpParser\Node\Name) { - $previous = $previous->getAttribute(AttributeKeys::PARENT_NODE); - } - if ($previous instanceof Node\Expr\Empty_) { - $this->removeNode($previous); - } - $current = $previous; - $current->setAttribute( - AttributeKeys::COMMENTS, - $this->filterComments($current) - ); - $previous = $current->getAttribute(AttributeKeys::PREVIOUS_NODE); - } - - $current->setAttribute(AttributeKeys::COMMENTS, $this->filterComments($current, [$this->standard_comment])); - - return $node; - } - - /** - * @return Comment[] - */ - private function filterComments(Node $node, array $default = []): array - { - foreach ($node->getComments() as $comment) { - if (preg_match(self::EXISTING_LICENSE_PATTERN, $comment->getText()) > 0) { - continue; - } - $default[] = $comment; - } - return $default; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition( - 'Adds or replaces a license-header in each class-file', - [ - new CodeSample( - // code before - '', - // code after - '' - ), - ] - ); - } -} diff --git a/scripts/Rector/DIC/DICDependencyManipulator.php b/scripts/Rector/DIC/DICDependencyManipulator.php deleted file mode 100755 index f1ecf6be6a04..000000000000 --- a/scripts/Rector/DIC/DICDependencyManipulator.php +++ /dev/null @@ -1,282 +0,0 @@ -getDICVariable()]); - } - - public function addStmtToMethodIfNotThereYetAtFirstPosition( - ClassMethod $classMethod, - Stmt\Class_ $class, - Stmt $stmt - ): void { - $class_method_string = $class->name->name . '::' . $classMethod->name->name; - $stmt_string = $this->nodeComparator->printWithoutComments($stmt); - if (isset($this->duplicate_checker[$class_method_string][$stmt_string]) - && $this->duplicate_checker[$class_method_string][$stmt_string] === true) { - return; - } - $stmts = $this->stmtsManipulator->filterOutExistingStmts( - $classMethod, - [$stmt] - ); - // all stmts are already there → skip - if ($stmts === []) { - return; - } - $first = null; - foreach ($classMethod->getStmts() as $inner_statement) { - if ($inner_statement->getAttributes() === []) { - continue; - } - $first = $inner_statement; - break; - } - if ($first !== null) { - $this->nodesToAddCollector->addNodeBeforeNode($stmt, $first); - } else { - $classMethod->stmts[] = $stmt; - } - $this->duplicate_checker[$class_method_string][$stmt_string] = true; - } - - private function createConstructor( - \PhpParser\Node\Stmt\Class_ $class - ): ClassMethod { - if (isset($this->added_constructors[$class->name->name])) { - return $this->added_constructors[$class->name->name]; - } - $classMethod = $this->nodeFactory->createPublicMethod( - \Rector\Core\ValueObject\MethodName::CONSTRUCT - ); - // implement parent constructor call - if ($this->hasClassParentClassMethod( - $class, - \Rector\Core\ValueObject\MethodName::CONSTRUCT - )) { - $classMethod->stmts[] = $this->createParentClassMethodCall( - \Rector\Core\ValueObject\MethodName::CONSTRUCT - ); - } - $first_class_method = array_filter($class->stmts, function (\PhpParser\Node $node): bool { - return $node instanceof ClassMethod; - }); - $first_class_method = array_shift($first_class_method); - if ($first_class_method !== null) { - $this->nodesToAddCollector->addNodeBeforeNode($classMethod, $first_class_method); - } else { - array_unshift($class->stmts, $classMethod); - } - $this->outputStyle->newline(); - $this->outputStyle->warning( - 'created constructor for ' . $class->name->name . '. Please check the parent-call for missing parameters!' - ); - $this->outputStyle->newline(); - - $this->added_constructors[$class->name->name] = $classMethod; - - return $classMethod; - } - - public function addStmtToConstructorIfNotThereYetAtFirstPosition( - \PhpParser\Node\Stmt\Class_ $class, - Stmt $stmt - ): void { - $classMethod = $class->getMethod( - \Rector\Core\ValueObject\MethodName::CONSTRUCT - ); - if (!$classMethod instanceof \PhpParser\Node\Stmt\ClassMethod) { - $classMethod = $this->createConstructor($class); - } - $this->addStmtToMethodIfNotThereYetAtFirstPosition( - $classMethod, - $class, - $stmt - ); - } - - public function ensureGlobalDICinConstructor(Stmt\Class_ $class): void - { - $stmt = $this->getGlobalDIC(); - $this->addStmtToConstructorIfNotThereYetAtFirstPosition( - $class, - $stmt - ); - $this->duplicate_checker[$class->name->name][$this->nodeComparator->printWithoutComments($stmt)] = true; - } - - public function ensureGlobalDICinMethod(ClassMethod $classMethod, Stmt\Class_ $class): Variable - { - $this->addStmtToMethodIfNotThereYetAtFirstPosition( - $classMethod, - $class, - $this->getGlobalDIC() - ); - return $this->getDICVariable(); - } - - public function addStmtToMethodIfNotThereAfterGlobalDIC( - ClassMethod $classMethod, - Stmt\Class_ $class, - Stmt $stmt - ): void { - $class_method_string = $class->name->name . '::' . $classMethod->name->name; - $statement_string = $this->nodeComparator->printWithoutComments($stmt); - if (isset($this->duplicate_checker[$class_method_string][$statement_string]) - && $this->duplicate_checker[$class_method_string][$statement_string] === true) { - return; - } - $stmts = $this->stmtsManipulator->filterOutExistingStmts( - $classMethod, - [$stmt] - ); - // all stmts are already there → skip - if ($stmts === []) { - return; - } - - $node = $this->betterNodeFinder->findFirst($classMethod->stmts, function (\PhpParser\Node $node): bool { - if (!$node instanceof Stmt\Global_) { - return false; - } - foreach ($node->vars as $var) { - if (!(property_exists($var, 'name') && $var->name !== null)) { - continue; - } - if ($var->name !== self::DIC) { - continue; - } - return true; - } - return false; - }); - $dic_statement_string = $this->nodeComparator->printWithoutComments($this->getGlobalDIC()); - if (!$node instanceof \PhpParser\Node - && !isset($this->duplicate_checker[$class_method_string][$dic_statement_string]) // we already added global $DIC in this run - && !$this->duplicate_checker[$class_method_string][$dic_statement_string] - ) { - throw new ShouldNotHappenException( - 'no dic found: ' . $class_method_string . ' (' . $statement_string . ') ' - ); - } - - // get first existing statement - $first_existing = array_filter($classMethod->stmts, function (\PhpParser\Node $node): bool { - if ($node->getAttributes() === []) { - return false; - } - return !$node instanceof Stmt\Global_; - }); - $first_existing = array_shift($first_existing); - if ($first_existing !== null) { - $this->nodesToAddCollector->addNodeBeforeNode($stmt, $first_existing); - } else { - // we use a fallback to add the element in first place. - // the nodesToAddCollector does not work here, becaue there are only - // "new" nodes without position - $classMethod->stmts[] = $stmt; - } - $this->duplicate_checker[$class_method_string][$statement_string] = true; - } - - public function addStmtToConstructorIfNotThereAfterGlobalDIC( - \PhpParser\Node\Stmt\Class_ $class, - Stmt $stmt - ): void { - $classMethod = $class->getMethod( - \Rector\Core\ValueObject\MethodName::CONSTRUCT - ); - if (!$classMethod instanceof \PhpParser\Node\Stmt\ClassMethod) { - $classMethod = $this->createConstructor($class); - } - $this->addStmtToMethodIfNotThereAfterGlobalDIC( - $classMethod, - $class, - $stmt - ); - } - - private function hasClassParentClassMethod( - \PhpParser\Node\Stmt\Class_ $class, - string $methodName - ): bool { - $scope = $class->getAttribute( - \Rector\NodeTypeResolver\Node\AttributeKey::SCOPE - ); - if (!$scope instanceof \PHPStan\Analyser\Scope) { - return \false; - } - $classReflection = $scope->getClassReflection(); - if (!$classReflection instanceof \PHPStan\Reflection\ClassReflection) { - return \false; - } - foreach ($classReflection->getParents() as $parentClassReflection) { - if ($parentClassReflection->hasMethod($methodName)) { - return \true; - } - } - return \false; - } - - private function createParentClassMethodCall( - string $methodName - ): \PhpParser\Node\Stmt\Expression { - $staticCall = new \PhpParser\Node\Expr\StaticCall( - new \PhpParser\Node\Name( - \Rector\Core\Enum\ObjectReference::PARENT()->getValue() - ), - $methodName - ); - - // append arguments - - return new \PhpParser\Node\Stmt\Expression($staticCall); - } -} diff --git a/scripts/Rector/DIC/DICMember.php b/scripts/Rector/DIC/DICMember.php deleted file mode 100755 index 08b656daf85e..000000000000 --- a/scripts/Rector/DIC/DICMember.php +++ /dev/null @@ -1,76 +0,0 @@ -alternative_classes = $alternative_classes; - } - - public function getName(): string - { - return $this->name; - } - - public function getMainClass(): string - { - return $this->main_class; - } - - /** - * @return mixed[] - */ - public function getAlternativeClasses(): array - { - return $this->alternative_classes; - } - - /** - * @return mixed[] - */ - public function getDicServiceMethod(): array - { - return $this->dic_service_method; - } - - public function getPropertyName(): string - { - return $this->property_name; - } -} diff --git a/scripts/Rector/DIC/DICMemberMap.php b/scripts/Rector/DIC/DICMemberMap.php deleted file mode 100755 index 2ce948135401..000000000000 --- a/scripts/Rector/DIC/DICMemberMap.php +++ /dev/null @@ -1,47 +0,0 @@ -setAlternativeClasses([\ilTemplate::class, \ilGlobalTemplate::class, \ilGlobalPageTemplate::class]); - $this->map[self::TPL] = $dicMember; - } - - public function getByName(string $name): DICMember - { - if (!isset($this->map[$name])) { - throw new \InvalidArgumentException("The dependency '$name' is currently not configured"); - } - return $this->map[$name]; - } -} diff --git a/scripts/Rector/DIC/DICMemberResolver.php b/scripts/Rector/DIC/DICMemberResolver.php deleted file mode 100755 index 3dba16a591fb..000000000000 --- a/scripts/Rector/DIC/DICMemberResolver.php +++ /dev/null @@ -1,188 +0,0 @@ -DICMemberMap = new DICMemberMap(); - } - - /** - * @return Expr|MethodCall - */ - private function getStaticDICCall( - DICMember $DICMember, - Class_ $class, - ClassMethod $classMethod - ): \PhpParser\Node\Expr\Variable { - // $DIC; - $dic_variable = $this->dicDependencyManipulator->ensureGlobalDICinMethod($classMethod, $class); - // new variable like $main_tpl; - $variable = new Variable($DICMember->getPropertyName()); - // MethodCall to get DIC Dependency - $expression = new Expression( - new Assign( - $variable, - $this->appendDICMethods( - $DICMember, - $dic_variable - ) - ) - ); - $this->dicDependencyManipulator->addStmtToMethodIfNotThereAfterGlobalDIC( - $classMethod, - $class, - $expression - ); - - return $variable; - } - - public function ensureDICDependency( - string $name, - Class_ $class, - ClassMethod $classMethod - ): Expr { - $DICMember = $this->getDICMemberByName($name); - - // return simple $GLOBALS access in static methods or - // return simple $GLOBALS access in static methods if we are in - // constructor itself, since currently we have problems to assign the - // member then... - $classMethodName = $classMethod->name->name ?? null; - if ($classMethod->isStatic()) { - return $this->getStaticDICCall($DICMember, $class, $classMethod); - } - if ($classMethodName === \Rector\Core\ValueObject\MethodName::CONSTRUCT) { - return $this->getStaticDICCall($DICMember, $class, $classMethod); - } - - // Test primary class - $mainClass = $DICMember->getMainClass(); - $dicPropertyFetch = $this->typeProvidingExprFromClassResolver->resolveTypeProvidingExprFromClass( - $class, - $classMethod, - $this->getObjectType($mainClass) - ); - if ($dicPropertyFetch instanceof PropertyFetch) { - return $dicPropertyFetch; - } - - // try alternatives - $alternatives = $DICMember->getAlternativeClasses(); - foreach ($alternatives as $alternative) { - $dicPropertyFetch = $this->typeProvidingExprFromClassResolver->resolveTypeProvidingExprFromClass( - $class, - $classMethod, - $this->getObjectType($alternative) - ); - if ($dicPropertyFetch instanceof PropertyFetch) { - return $dicPropertyFetch; - } - } - - // Add property - $this->propertyToAddCollector->addPropertyWithoutConstructorToClass( - $DICMember->getPropertyName(), - $this->getObjectType($mainClass), - $class - ); - - $dicPropertyFetch = new PropertyFetch( - new Variable(self::THIS), - $DICMember->getPropertyName() - ); - // Method call to get DIC dependency - $methodCall = $this->appendDICMethods( - $DICMember, - new Variable(self::DIC) - ); - // global $DIC - $this->dicDependencyManipulator->ensureGlobalDICinConstructor( - $class - ); - // $this->xy = $DIC->xy() - $expression = new Expression( - new Assign( - $dicPropertyFetch, - $methodCall - ) - ); - $this->dicDependencyManipulator->addStmtToConstructorIfNotThereAfterGlobalDIC( - $class, - $expression - ); - - return $dicPropertyFetch; - } - - private function appendDICMethods(DICMember $dicMember, Expr $expr): \PhpParser\Node\Expr - { - foreach ($dicMember->getDicServiceMethod() as $call) { - $expr = new MethodCall( - $expr, - $call - ); - } - return $expr; - } - - private function getDICMemberByName(string $name): DICMember - { - return $this->DICMemberMap->getByName($name); - } - - private function getObjectType(string $name): ObjectType - { - return new ObjectType($name); - } -} diff --git a/scripts/Rector/README.md b/scripts/Rector/README.md deleted file mode 100755 index 121cfe7aa64b..000000000000 --- a/scripts/Rector/README.md +++ /dev/null @@ -1,28 +0,0 @@ -Rector - Instant Upgrades and Automated Refactoring -=================================================== - -We can use Rector to do some automated refactoring. This is a good way to upgrade your codebase to a new version of PHP. - -## Usage - -There is basic configuration which includes two costim rules, removing all requires/includes and addid the -License-header if needed. - -```bash -./vendor/composer/vendor/bin/rector process --config scripts/Rector/basic_rector.php YOUR_DIRECTORY -``` - -You can try to update your Code to support PHP 8.0 to 8.2 with the follwoing rule-set: - -```bash -./vendor/composer/vendor/bin/rector process --config scripts/Rector/ilias_9.php YOUR_DIRECTORY -``` - -Please check the changes and revert the ones you don't want to have. - -The following rule-set contains some general improvements for your code 8such as early returns, removing unused -variables, etc.): - -```bash -./vendor/composer/vendor/bin/rector process --config scripts/Rector/code_quality.php YOUR_DIRECTORY -``` diff --git a/scripts/Rector/RemoveRequiresAndIncludesRector.php b/scripts/Rector/RemoveRequiresAndIncludesRector.php deleted file mode 100755 index 13c885b1f32b..000000000000 --- a/scripts/Rector/RemoveRequiresAndIncludesRector.php +++ /dev/null @@ -1,63 +0,0 @@ -> - */ - public function getNodeTypes(): array - { - return [\PhpParser\Node\Expr\Include_::class]; - } - - /** - * @param Node\Expr\Include_ $node - */ - public function refactor(Node $node): \PhpParser\Node\Expr\Include_ - { - if (!$this->isObjectType($node, new ObjectType(Node\Expr\Assign::class))) { - $this->nodeRemover->removeNode($node); - } - - return $node; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition( - 'Remove requires and includes', - [ - new CodeSample( - // code before - 'require_once "./..."', - // code after - '' - ), - ] - ); - } -} diff --git a/scripts/Rector/basic_rector.php b/scripts/Rector/basic_rector.php deleted file mode 100755 index ab221e76e622..000000000000 --- a/scripts/Rector/basic_rector.php +++ /dev/null @@ -1,15 +0,0 @@ -disableParallel(); - // We start with a single and sinle (own) rule. remove requires and include. - $rectorConfig->rule(RemoveRequiresAndIncludesRector::class); - // The second rule will add (or replace) e license-header for every class-file - $rectorConfig->rule(ChangeLicenseHeader::class); -}; diff --git a/scripts/Rector/code_quality.php b/scripts/Rector/code_quality.php deleted file mode 100755 index a95949ae553c..000000000000 --- a/scripts/Rector/code_quality.php +++ /dev/null @@ -1,18 +0,0 @@ -disableParallel(); - $rectorConfig->phpVersion(PhpVersion::PHP_80); - $rectorConfig->sets([ - SetList::DEAD_CODE, - SetList::TYPE_DECLARATION, - SetList::CODE_QUALITY, - SetList::EARLY_RETURN, - ]); -}; diff --git a/scripts/Rector/ilUtils/Example.php b/scripts/Rector/ilUtils/Example.php deleted file mode 100755 index 5eb9886b58bb..000000000000 --- a/scripts/Rector/ilUtils/Example.php +++ /dev/null @@ -1,36 +0,0 @@ -> - */ - public function getNodeTypes(): array - { - return [\PhpParser\Node\Expr\StaticCall::class]; - } - - private function isApplicable(Node $node): bool - { - /** @var $node \PhpParser\Node\Expr\StaticCall::class */ - if (!$node->class instanceof \PhpParser\Node\Name) { - return false; - } - $staticCallClassName = $node->class->toString(); - if ($staticCallClassName !== \ilUtil::class) { - // not calling ilUtil - return false; - } - if (!$node->name instanceof \PhpParser\Node\Identifier) { - // node has no name - return false; - } - // not interested in method since not in list - return in_array($node->name->name, $this->old_method_names); - } - - /** - * @param Node $node the Static Call to ilUtil:sendXY - */ - public function refactor(Node $node): ?\PhpParser\Node\Expr\MethodCall - { - if (!$this->isApplicable($node)) { - return null; // leave the node as it is - } - $class_where_call_happens = $this->betterNodeFinder->findParentType( - $node, - \PhpParser\Node\Stmt\Class_::class - ); - if (!$class_where_call_happens instanceof \PhpParser\Node\Stmt\Class_) { - // not on class, abort - return null; // leave the node as it is - } - $method_where_call_happend = $this->betterNodeFinder->findParentType( - $node, - \PhpParser\Node\Stmt\ClassMethod::class - ); - if (!$method_where_call_happend instanceof \PhpParser\Node\Stmt\ClassMethod) { - // not in a method, abort - return null; // leave the node as it is - } - - if ($method_where_call_happend->isStatic()) { -// return null; - } - - // prepend a new argument with the type of the message, aka sendInfo goes to setOnScreenMessage('info', ... - $message_type = strtolower(str_replace('send', '', $node->name->name)); - $arg = $this->nodeFactory->createArg($message_type); - $arguments = $node->args; - array_unshift($arguments, $arg); - - // ensure a dic property for ilGlobalTemplate is in the class. or we get another Expr to fetch ilGlobalTemplate - try { - $dicPropertyFetch = $this->dicMemberResolver->ensureDICDependency( - DICMemberMap::TPL, - $class_where_call_happens, - $method_where_call_happend - ); - } catch (ShouldNotHappenException $e) { - throw new ShouldNotHappenException( - "Could not process " . $this->file->getFilePath() . ': ' . $e->getMessage() - ); - } - - // return new method call - $methodCall = new \PhpParser\Node\Expr\MethodCall( - $dicPropertyFetch, - $this->new_method_name, - $arguments - ); - return $methodCall; - } - - public function getRuleDefinition(): \Symplify\RuleDocGenerator\ValueObject\RuleDefinition - { - return new RuleDefinition('lorem', [ - new CodeSample( - "\ilUtil::sendQuestion('my_text', true);", - "\$this->main_tpl->setOnScreenMessage('question', 'my_text', true)" - ) - ]); - } -} diff --git a/scripts/Rector/ilUtils/ilutil_rector.php b/scripts/Rector/ilUtils/ilutil_rector.php deleted file mode 100755 index 06f68382fcf3..000000000000 --- a/scripts/Rector/ilUtils/ilutil_rector.php +++ /dev/null @@ -1,44 +0,0 @@ -disableParallel(); - $rectorConfig->parameters()->set(Option::SKIP, [ - // there a several classes which make Rector break (multiple classes - // in one file, wrong declarations in inheritance, ...) - "components/ILIAS/LTIConsumer", - "components/ILIAS/LTIProvider", - "components/ILIAS/SOAPAuth/include" - ]); - $rectorConfig->parameters()->set(Option::DEBUG, false); - - $rectorConfig->phpVersion(PhpVersion::PHP_80); - - $rectorConfig->services()->set(DICMemberResolver::class)->autowire(); - $rectorConfig->services()->set(DICDependencyManipulator::class)->autowire(); - $rectorConfig->services()->set(ReplaceUtilSendMessageRector::class); -}; diff --git a/scripts/Rector/ilias_9.php b/scripts/Rector/ilias_9.php deleted file mode 100755 index 6302310e521e..000000000000 --- a/scripts/Rector/ilias_9.php +++ /dev/null @@ -1,22 +0,0 @@ -disableParallel(); - $rectorConfig->phpVersion(PhpVersion::PHP_80); - $rectorConfig->sets([ - SetList::PHP_80, - SetList::PHP_81, - SetList::PHP_82, - LevelSetList::UP_TO_PHP_82, - DowngradeLevelSetList::DOWN_TO_PHP_80, - ]); -};