Contributing to Laravel

A couple of days ago, I started to build a small web app for fun, and it was a Dropbox clone. It’s called Yet Another DropBox, and as you guessed, it’s an online file hosting and sharing service. At the same time, I was also planning to try out Laravel 5.5, which is not been officially released yet. So I thought I could try out the cutting edge of what Laravel got to offer.

What is DropBox?

Dropbox is a file hosting service that offers cloud storage, file synchronization, personal cloud, and client software.

What about Laravel?

Laravel is a free, open-source PHP web framework created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern. And by the way, Laravel is third on GitHub in the web application frameworks category next to Meteor and Rails.

Then what?

After setting up the necessary things for the web app, I changed the namespace of my app to YetAnother using the app:name artisan command, which is one of Laravel's awesome helper commands. The app:name command lets you set the application namespace from the default App namespace to the one you specify.

How can working on your own project make you a laravel contributor?

Yesterday I was generating a model for representing “file”, and I saw -a an option to the make:model command, and the description said

-a, –all Generate a migration, factory, and resource controller for the model”

I thought it was awesome because before v5.5 I used to pass -cmr to the make:model command to generate a resource controller and migration for a given model. I tried it out, and it generated the model with migration and a resource controller, but it also created a file FileFactory.php in database/factories. I opened it up, and it was a model factory for the File model that we used to write in ModelFactory.php on 5.4. Then I saw a file called a UserFactory.php next to it, and it was the model factory for the User model, which used to be shipped in ModelFactory.php before 5.4.

Then I realized it was a new feature on Laravel 5.5. We store all our model factories in one file, which can be messy and get huge if you have a lot of models. Splitting it into multiple files was a cool thing, and getting rid of ModelFactory.php was a smart move … I appreciated Taylor as always and continued.

So what?

However, I noticed that the application namespace is not updated on UserFactory, It was still saying App\User. So I executed the command again as

php artisan app:name YetAnother

it was still not changing the namespace. I know for sure this command is used to change the namespace in ModelFactory Laravel 5.4, but now it’s not working on laravel 5.5-dev , so I start poking around why it doesn’t work.

First, I search for AppName and I found out that the app:name command resides on Illuminate\Foundation\Console\AppNameCommand class, and I started skimming through the code. Since every command implements the handle method, I jumped to the handle method and saw $this->setDatabaseFactoryNamespaces(); it looks convenient that the database factory is handled there, so I jump to the setDatabaseFactoryNamespaces method.

On line 224, I found the method, and I was shocked. it read as

$this->replaceIn( $this->laravel->databasePath().'/factories/ModelFactory.php', $this->currentRoot, $this->argument('name') );

I thought, why is it trying to change the namespace in ModelFactory. I asked Isn’t ModelFactory gone for good? Then, I tried the command a couple of times with a debugger, and finally, I realized it was a bug.

Since my installation was a week old I said it might be fixed and I should update to get the latest commit. Therefore I update my composer dependencies and nothing changed. So I logged in to GitHub and checked it on the latest Laravel branch, and I found out the bug was there, too.

How could this happen?

I started digging when the regression happened, and I found out it was 5 months ago when Taylor himself renamed the ModelFactory.php file to UserFactory.php with a commit message saying “rename ModelFactory to UserFactory to encourage separate files”. But the app:name command was not updated accordingly of the change. That poor little artisan command was still looking for ModelFactory.php a file.

Finally, I gladly fixed the bug. I searched all the files inside the database/factories and update the namespace on all of them iteratively.

After that, I sent a pull request to the laravel/framework repo, which got merged after a couple of hours.

Got some advice?

I want to give some pointers for those of you who want to contribute to big open-source projects

  1. First, read the contribution guidelines before doing anything

  2. Try to find some useful improvements or bugs

  3. Don’t be afraid to send a pull request; they won’t bite

  4. Don’t get offended if they don’t accept your pull request

  5. Most importantly, be nice

Thanks for reading

PS. Give some feedback

Originally published at sam.1gna.com on August 27, 2017.