- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 688
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
AnnotationToAttributeRector removes empty lines #5447
Comments
Hi, this is expected behavior. Coding style must be handled by your coding standard tool. See |
Sorry, I don't understand what you mean. Why does Rector remove those lines? |
I've read that and I'm using ECS. But ECS can't add those removed lines, isn't it? And in this case, it's related to |
If it's not in ECS yet, you need to provide your own rule. |
Sorry, I'm afraid you don't understand what I mean. With |
I understand that. It's a side effect of AST changes. The reprint to the original content is not always 100 %. It's limitation of technology itself. This is in the linked content. This will happen accidentaly for many changes in AST nodes, unrelated to any specific rule. You can read more about it here: nikic/PHP-Parser#344 |
Ok, thanks for clarifying! It didn't expect the body to be touched in the first place, since the body seems to be unrelated to this rector . That's why I thought it might be another problem. |
I believe a lot of developers use empty lines inside methods to separate some logic. I decided to move from Symfony public function listAction(PaginationRequest $request): Response
{
$before = $request->query->getInt('before');
$limit = $request->query->getInt('limit');
-removed-
$conversations = $this->entityManager->getRepository(Conversation::class);
-removed-
// some heavy-logic block
//.... Reverting changes line by line is pretty annoying. So, here is how I managed it (hope this could help someone). # Step 1. First of all run `rector`
vendor/bin/rector process src/Controller
# Step 2. Generate patch using `git diff` without context lines, only changes (`-U0`).
# pipe it to perl to replace in place empty lines (-0 to operate multiline)
git diff -U0 | perl -0 -pwe "s/@@.+\n\-\n//g" > no-removed-blank-lines.patch
# Step 3. Reset changes made by rector
git reset --hard HEAD
# Step 4. Finally apply patch and it is done
git apply -v --unidiff-zero no-removed-blank-lines.patch So what happens in Step 3, you just get rid of blank lines (like below) in your patch. In the end you just apply necessary changes. @@ -60 +52,0 @@ class ConversationsController
- P.S. other changes like whitespace in method signature could be fixed using your code-style tools 😉 |
@svitiashchuk Thank you for sharing this exact solution. I'll definitely use it soon in refactoring that I make with Rector 👍 |
It would be really nice if AnnotationToAttributeRector would not touch function body. |
@algimantas most of the line removal from |
@samsonasik could you please modify the |
Using |
@svitiashchuk I tried the solution proposed here #5447 (comment) but the patch (git apply -v --unidiff-zero no-removed-blank-lines.patch) only applies on the first route of each class. Sample of class before change (using annotation): /**
* Brand Import controller.
*
* @Route("/secured/transport/brand/import")
*/
class BrandController extends ImportController
{
/**
* @Route("/get/fields", name="brand_import_get_field_definitions", methods={"POST"})
*/
public function getFieldDefinitionsAction(Request $request): JsonResponse
{
return $this->getFieldDefintions($request);
}
/**
* @Route("/get/import/file", name="brand_import_get_file", methods={"POST"})
*/
public function getImportFileAction(Request $request): Response
{
return $this->getImportFile($request, 'brand');
}
... Sample of class before change (using attributes - after /**
* Brand Import controller.
*
- * @Route("/secured/transport/brand/import")
*/
+#[Route(path: '/secured/transport/brand/import')]
class BrandController extends ImportController
{
- /**
- * @Route("/get/fields", name="brand_import_get_field_definitions", methods={"POST"})
- */
+ #[Route(path: '/get/fields', name: 'brand_import_get_field_definitions', methods: ['POST'])]
public function getFieldDefinitionsAction(Request $request): JsonResponse
{
return $this->getFieldDefintions($request);
}
-
- /**
- * @Route("/get/import/file", name="brand_import_get_file", methods={"POST"})
- */
+ #[Route(path: '/get/import/file', name: 'brand_import_get_file', methods: ['POST'])]
public function getImportFileAction(Request $request): Response
{
return $this->getImportFile($request, 'brand');
}
What is generated in file
diff --git a/src/PTC/AdminBundle/Controller/Import/BrandController.php b/src/PTC/AdminBundle/Controller/Import/BrandController.php
index 433a3879..9a8ed902 100644
--- a/src/PTC/AdminBundle/Controller/Import/BrandController.php
+++ b/src/PTC/AdminBundle/Controller/Import/BrandController.php
@@ -13 +12,0 @@ use Symfony\Component\Routing\Annotation\Route;
- * @Route("/secured/transport/brand/import")
@@ -14,0 +14 @@ use Symfony\Component\Routing\Annotation\Route;
+#[Route(path: '/secured/transport/brand/import')]
@@ -17,3 +17 @@ class BrandController extends ImportController
- /**
- * @Route("/get/fields", name="brand_import_get_field_definitions", methods={"POST"})
- */
+ #[Route(path: '/get/fields', name: 'brand_import_get_field_definitions', methods: ['POST'])]
- /**
- * @Route("/get/import/file", name="brand_import_get_file", methods={"POST"})
- */
+ #[Route(path: '/get/import/file', name: 'brand_import_get_file', methods: ['POST']) |
@raziel057 Hi, could you share minimal reproducible repository? I'll see if there is some other way to handle this. |
@svitiashchuk It's cause by the perl transformation as the different
|
If I remove the perl part, I have the following result: diff --git a/src/PTC/AdminBundle/Controller/Import/BrandController.php b/src/PTC/AdminBundle/Controller/Import/BrandController.php
index 433a3879..9a8ed902 100644
--- a/src/PTC/AdminBundle/Controller/Import/BrandController.php
+++ b/src/PTC/AdminBundle/Controller/Import/BrandController.php
@@ -13 +12,0 @@ use Symfony\Component\Routing\Annotation\Route;
- * @Route("/secured/transport/brand/import")
@@ -14,0 +14 @@ use Symfony\Component\Routing\Annotation\Route;
+#[Route(path: '/secured/transport/brand/import')]
@@ -17,3 +17 @@ class BrandController extends ImportController
- /**
- * @Route("/get/fields", name="brand_import_get_field_definitions", methods={"POST"})
- */
+ #[Route(path: '/get/fields', name: 'brand_import_get_field_definitions', methods: ['POST'])]
@@ -24,4 +22 @@ class BrandController extends ImportController
-
- /**
- * @Route("/get/import/file", name="brand_import_get_file", methods={"POST"})
- */
+ #[Route(path: '/get/import/file', name: 'brand_import_get_file', methods: ['POST'])]
@@ -32,4 +27 @@ class BrandController extends ImportController |
As a minimal reproducer, you can just get the file in the following zip and try to run the perl command:
|
Repository would be better, as we'll need to make it work online first. Otherwise we can get to "works on my machine" situation. |
rectorphp/rector-src@e3ccf17 Fix scoped e2e to load rector/rector:dev-main (#5447)
Bug Report
Minimal PHP Code Causing Issue
See https://getrector.org/demo/edb98391-fcaf-4f4b-941c-db7afb2a6127
Responsible rules
ArgumentAdderRector
AnnotationToAttributeRector
Expected Behavior
Rector removes an empty line:
This shouldn't be happening.
The text was updated successfully, but these errors were encountered: