Posts List

How to Check PHP Version Compatibility

Phan Static Analyzer When trying to determine what needs to be updated in a project to make it PHP 7.* compatible, it is nice to use a commandline program to assist. I straight away tried to use phan static analyzer since I had used it on other projects to check the quality of code but it was impossibly slow, when using it for a codebase plus vendor libs, even when target_php_version was the only analyzer turned on when running phan .

Computing Eloquent Model Deltas

Computing the differences or deltas between Eloquent models is a common task in many data oriented apps. The easiest way that I have found out how to do so is below: // This is assumed to be in \App\YourModel /** * @param \App\YourModel $model * * @return array */ public function delta(\App\YourModel $model) { // array_diff() is an easy way to produce deltas $baseline = $model->attributesToArray(); $possiblyChanged = $this->attributesToArray(); /* * $baseline = ['test' => 500, 'not_changed' => 5] * $possiblyChanged = ['test' => 700, 'not_changed' => 5] */ /* * $delta == ['test' => 700] */ $delta = array_diff($possiblyChanged, $baseline); // Here I unset possible changes that I don't care about unset($delta['id']); unset($delta['created_at']); unset($delta['updated_at']); return $delta; }

Eloquent Model JSON Serialization and Deserialization

Awhile back I needed to save an Eloquent model to the DB so that I might compute a delta later on. Json serves this purpose nicely since the json data type is now natively supported in many languages. One could do this manually but, fortunately, Laravel makes this easy on us. We can utilize model mutators to dynamically convert the input and output of a specific database column. For example, say we want to save a passed in model as json and that the column that we want to save to is called json:

Laravel seeding using a yield generator

I was needing to improve the performance of a pivot table seeder recently and decided to use a generator to do it. Interestingly, one of the easiest to implement iterators is the yield syntax introduced in PHP 5.5. It allows for good abstraction between the data that is to be iterated and the source of said data. That can lead to significant performance increases as well since it might, depending on the use case, reduce database calls or the amount of data being stored in memory among other things.

How to create Eloquent models from Laravel Query Builder results

Sometimes Eloquent doesn’t provide all of the functionality and access to data that you need out of the box. Even Taylor Otwell, the framework’s creator, often uses query builder for his DB operations. Getting wet Refer to the the query builder docs on how to use it: Query Builder Running a query such as: \DB::table('users')->take(2)->get(); will return an array of stdClass objects. What if you want to change those into User model objects?

Up and Running with PHP 7 — Installation for Testing

With the release of PHP 7 just around the corner, a quick start guide seems to be in order. This guide was written with Linux in mind but it should work ok with Mac OS X. If you are on Windows, you should switch to Linux (or maybe Mac) :). Let’s get a testing environment set up. 1. Vagrant box installation First, let’s install VirtualBox Next, you need to install Vagrant version > 1.

The Single Responsibility Principle

The first of the SOLID principles is unfortunately violated quite often even though it is easy to understand why you want to maintain a separation of concerns. It is similar to the SQL injection vulnerabilities that show up on StackOverflow and other places even though the internet is flooded with warnings about how to avoid being pwnd by the easily escapable beast. In the example repository, I give the example of a basketball player that is both a coach and a player.

Setting expectations for a closure using Mockery

If you are trying to set an expectation for \Closure using mockery, you may have seen this error: Mockery\Exception\NoMatchingExpectationException: No matching handler found for Mockery_0_NAMESPACE_CLASSNAME::METHODNAME(object(Closure)). Either the method was unexpected or its arguments matched no expected argument list for this method To start off with, anonymous functions in PHP are instances of the internal Closure class. You probably tried something like passing a Closure to the expectation handler that was identical to the one being passed in the code.

How to Setup a Private Composer Satis Package Server

Assumptions This how-to assumes that you are using Ubuntu 14.04 Contents User Setup Installations Configure Apache SFTP Jail Setup Satis Firewall Set Hostname SSH Keys for Private Repos User Setup You will need to enter passwords for both of these users. adduser admin_user_name_of_your_choice adduser composer visudo Add the following lines to visudo: admin_user_name_of_your_choice ALL=(ALL:ALL) ALL SSH Create an authorized keys file then move it to the ~/.