Nope. Only singular lessons that show you patterns you can use to clean things up.
I would advise you to start with tests.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Is there a series or lesson here in Laracasts that is about how to refactor an existing project and where to start?
Nope. Only singular lessons that show you patterns you can use to clean things up.
I would advise you to start with tests.
@mrcloudcat I‘ve undertook a lot of refactoring projects in my time. Unfortunately, there’s no one approach to refactoring; it depends on the codebase you’re refactoring: how old it is, how it’s structured, etc.
When it comes to refactoring, I think the key is to boil down what the end goal is, and what you have. So working out what ‘A’ and ’B’ is, and how to get from A to B. So if there’s a lot of repetitive code, look to see if you can wrap that up into a class and slowly refactor parts of the code to use that class and reduce that repetition. If there are raw SQL queries, look to create models and replace queries with the equivalent model usage.
With refactoring, small and often is key. Don’t try and tackle lots of changes in one go, as when things start going wrong and you have lots of changes, that’s a lot to pick through and then all you want to do is embark on The Big Rewrite™ and build the project again from scratch. Instead, look for a small, discrete change you can make, do that, and then let it bed in and check the project still functions as intended. If so, move on to the next change.
Also, test! If you’re changing a piece of code, write a test that passes how it functions now. Then use the test to check that piece of code still works after your refactor.
@bugsysha Thanks! Yeah, I'll start slowly doing simple tests.
@martinbean Thanks! To give a more detailed info, I have an existing Laravel project that I created about 3 years ago. It's working but my code was a disaster. No tests, bad coding, bad database structure, etc. I'm still maintaining that project. As I'm learning how to code properly, I wanted to go back and refactor that project because I'm having a hard time adding features or maybe fix some bug. It's taking too much time even for just simple issues.
I though maybe there's a procedure in refactoring an existing project in a quicker way. Or maybe where something that tells me where should I start. But I think you're right, although it'll take some time, I need to take it slow. I also thought of building the project from scratch but it'll also take time.
I also thought of building the project from scratch but it'll also take time.
You will lose those edge cases so better avoid writing from scratch.
If you want to cover as much ground as possible then write tests that will cover the way users interact with your app. For example, write "feature" tests that will get/send data to routes and assert results. That way test will cover more than separate class/unit testing.
@bugsysha got it. it'll take a while as the project is very big. I hope I can manage to clean it
You can. Just give yourself enough time for it. And remember it is an iterative process. You will "never" reach perfection.
@bugsysha That's it! You'll never reach perfection.
I'm trying to keep away myself from being too perfect. When I get back to my codes, after learning something here, I always feel like there's something to change, something that can still be shortened or cleaned up. I always feel like my code is bad.
I always feel like my code is bad.
Same here. My code is pretty clean, but I always think that I could do better.
Please or to participate in this conversation.