sabbelasichon / typo3-rector

Rector for TYPO3
MIT License
231 stars 63 forks source link

New use statement not always part of existing use statements #1893

Closed DanielSiepmann closed 3 years ago

DanielSiepmann commented 3 years ago

Under some conditions a new introduced use statement is added somewhere else in the file, even if there are existing use statements. I would expect it to be part of the existing block of statements.

But I also think this is not important, only happens under some conditions, and should be the task of some cgl tool anyway.

We got the following result:

diff --git a/local_packages/e2_core/Classes/ViewHelpers/Uri/EditRecordViewHelper.php b/local_packages/e2_core/Classes/ViewHelpers/Uri/EditRecordViewHelper.php
index 8a1664c88..e37b40b66 100644
--- a/local_packages/e2_core/Classes/ViewHelpers/Uri/EditRecordViewHelper.php
+++ b/local_packages/e2_core/Classes/ViewHelpers/Uri/EditRecordViewHelper.php
@@ -1,88 +1,89 @@
 <?php
 namespace E2\E2Core\ViewHelpers\Uri;

+use TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException;
 /*
  * This file is part of the TYPO3 CMS project.
  *
  * It is free software; you can redistribute it and/or modify it under
  * the terms of the GNU General Public License, either version 2
  * of the License, or any later version.
  *
  * For the full copyright and license information, please read the
  * LICENSE.txt file that was distributed with this source code.
  *
  * The TYPO3 project - inspiring people to share!
  */

 use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
 use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
 use TYPO3\CMS\Backend\Routing\UriBuilder;
 use TYPO3\CMS\Core\Utility\GeneralUtility;

 // COPIED FROM 10.4, remove once we update and replace namespace in templates

 /**
  * Use this ViewHelper to provide edit links (only the uri) to records. The ViewHelper will
  * pass the uid and table to FormEngine.
  *
  * The uid must be given as a positive integer.
  * For new records, use the :ref:`<be:uri.newRecord> <typo3-backend-uri-newrecord>`.
  *
  * Examples
  * ========
  *
  * URI to the record-edit action passed to FormEngine::
  *
  *    <be:uri.editRecord uid="42" table="a_table" returnUrl="foo/bar" />
  *
  * ``/typo3/index.php?route=/record/edit&edit[a_table][42]=edit&returnUrl=foo/bar``
  *
  * URI to the edit record action: edit only the fields title and subtitle of
  * page uid=42 and return to foo/bar::
  *
  *    <be:uri.editRecord uid="42" table="pages" fields="title,subtitle" returnUrl="foo/bar" />
  *
  * ``<a href="/typo3/index.php?route=/record/edit&edit[pages][42]=edit&returnUrl=foo/bar&columnsOnly=title,subtitle">``
  */
 class EditRecordViewHelper extends AbstractViewHelper
 {

For the following File:

<?php
namespace E2\E2Core\ViewHelpers\Uri;

/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

use TYPO3\CMS\Backend\Routing\UriBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;

// COPIED FROM 10.4, remove once we update and replace namespace in templates

/**
 * Use this ViewHelper to provide edit links (only the uri) to records. The ViewHelper will
 * pass the uid and table to FormEngine.
 *
 * The uid must be given as a positive integer.
 * For new records, use the :ref:`<be:uri.newRecord> <typo3-backend-uri-newrecord>`.
 *
 * Examples
 * ========
 *
 * URI to the record-edit action passed to FormEngine::
 *
 *    <be:uri.editRecord uid="42" table="a_table" returnUrl="foo/bar" />
 *
 * ``/typo3/index.php?route=/record/edit&edit[a_table][42]=edit&returnUrl=foo/bar``
 *
 * URI to the edit record action: edit only the fields title and subtitle of
 * page uid=42 and return to foo/bar::
 *
 *    <be:uri.editRecord uid="42" table="pages" fields="title,subtitle" returnUrl="foo/bar" />
 *
 * ``<a href="/typo3/index.php?route=/record/edit&edit[pages][42]=edit&returnUrl=foo/bar&columnsOnly=title,subtitle">``
 */
class EditRecordViewHelper extends AbstractViewHelper
{
    use CompileWithRenderStatic;

    public function initializeArguments()
    {
        $this->registerArgument('uid', 'int', 'uid of record to be edited, 0 for creation', true);
        $this->registerArgument('table', 'string', 'target database table', true);
        $this->registerArgument('fields', 'string', 'Edit only these fields (comma separated list)', false);
        $this->registerArgument('returnUrl', 'string', '', false, '');
    }

    /**
     * @param array $arguments
     * @param \Closure $renderChildrenClosure
     * @param RenderingContextInterface $renderingContext
     *
     * @return string
     * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
     */
    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext): string
    {
        if ($arguments['uid'] < 1) {
            throw new \InvalidArgumentException('Uid must be a positive integer, ' . $arguments['uid'] . ' given.', 1526128259);
        }
        if (empty($arguments['returnUrl'])) {
            $arguments['returnUrl'] = GeneralUtility::getIndpEnv('REQUEST_URI');
        }

        $params = [
            'edit' => [$arguments['table'] => [$arguments['uid'] => 'edit']],
            'returnUrl' => $arguments['returnUrl']
        ];
        if ($arguments['fields'] ?? false) {
            $params['columnsOnly'] = $arguments['fields'];
        }
        $uriBuilder = GeneralUtility::makeInstance(UriBuilder::class);
        return (string)$uriBuilder->buildUriFromRoute('record_edit', $params);
    }
}
sabbelasichon commented 3 years ago

@DanielSiepmann This is also something related to Rector itself. Please open up an issue there or try to solve this with a CS-Fixer like https://github.com/FriendsOfPHP/PHP-CS-Fixer or https://github.com/symplify/easy-coding-standard. That´s the way i do it.