Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Contentful Blog:
An extremely picky developer's take on static site generators for PHP: Part 2 - Jig
Aug 03, 2018 @ 10:50:23

On the Contentful blog they've posted the second part of their series reviewing static site generators for PHP. In this new article they look at Jigsaw, a Laravel-based tool.

In the first article of the series we took a look at Sculpin, the PHP static site generator which is currently the most starred on Github. Today we’re exploring Jigsaw, a tool which promises to bring a Laravel-based approach to the world of PHP static site generators (SSGs).

The author starts by sharing some about his (somewhat limited) experience with Laravel. From there he dives right in, showing the creation of a new Jigsaw site including configuration and how to serve it up locally. It then gets into detail about the directory structure of the site, asset handling and adding new content. The post ends by answering the "is it ready" question for their own needs (at Contentful).

tagged: static site generator review series part2 jigsaw

Link: https://www.contentful.com/blog/2018/08/01/extremely-picky-developers-take-php-static-site-generators-part-2-jigsaw/

PHP Roundtable Podcast:
Episode 073 - PHP Static Analysis
Jun 21, 2018 @ 12:26:08

The PHP Roundtable podcast, hosted by PHP community member Sammy Powers, has posted their latest episode: #73 - PHP Static Analysis. In this episode Sammy is joined by Ond?ej Mirtes, Craig Duncan, Vladimir Reznichenko and Damien Seguy.

Static analysis is a fancy word to describe a tool that looks at our code and gives us helpful hints on how to improve it. We'll be discussing what static analyzers do, which tools the PHP community has access to, and how we can incorporate the tools into our daily development flow.

You can catch this latest episode in a few different ways: either using the in-page audio or video player, by downloading the audio file directly or by watching the recording over on Youtube. If you enjoy the show, be sure to subscribe to their feed and follow them on Twitter for updates when new shows are released.

tagged: phproundtable ep73 podcast sammypowers static analysis

Link: https://www.phproundtable.com/episode/using-static-analyzers-to-improve-our-php-codebases

Exakat Blog:
5 usages of static keyword in PHP
Jun 20, 2018 @ 10:28:21

On the Exakat blog there's a new post sharing five uses of the "static" keyword in PHP applications. This includes the less common static closures and static class names.

Static is a PHP keyword with many usages. It is almost universally used, though there are many variations of it. Let’s review all the five of them :
  • static method
  • static property
  • static closure
  • static variable
  • static as a classname

The post goes through each of the items in the list giving a brief explanation of how it's used and a code example showing it in action. The post finishes with some tips on evaluating your own code for the use of "static" and tips for each to make your code easier to wrangle and maintain in the future.

tagged: static keyword example tutorial method property closure variable classname

Link: https://www.exakat.io/5-usages-of-static-keyword-in-php/

Matthias Noback:
Road to dependency injection
Jun 12, 2018 @ 09:31:59

In a new post to his site Matthias Noback walks you down a road to dependency injection, sharing his process of migrating a codebase from static calls to a more modern, robust dependency injection pattern.

I've worked with several code bases that were littered with calls to Zend_Registry::get(), sfContext::getInstance(), etc. to fetch a dependency when needed. I'm a little afraid to mention façades here, but they also belong in this list. The point of this article is not to bash a certain framework (they are all lovely), but to show how to get rid of these "centralized dependency managers" when you need to.

He talks about this common use case of "statics everywhere" and some of the problems with coupling your code to a static way of doing things. He walks through the steps towards modernization including injecting the container itself into the controller and shifting logic out into services. He also makes suggestions on what to do when constructor injection isn't possible and how to deal with "temporal coupling".

tagged: dependencyinjection refactor codebase static inject container tutorial

Link: https://matthiasnoback.nl/2018/06/road-to-dependency-injection/

Matt Stauffer:
Adding an Auto-Generated Sitemap to Your Jigsaw-based Static Site
May 29, 2018 @ 11:07:42

If you're a user of the Jigsaw static site generation tool from Tighten, Matt Stauffer has a new post to his site that could come in handy. He shows how to automatically create a sitemap.

There are a few tasks that are still pretty tough with static sites—for example, search, and submitting forms (which we're trying to fix with FieldGoal). But there are other tasks that are tough-but-possible, and key among them are RSS and sitemaps. Let's start with sitemaps.

There was a feature recently added to help with this allowing you to add extra "hooks" to fire when the site builds. Matt makes use of this (and this package) to create a GenerateSitemap class that goes through each item in the site and adds a link to the sitemap.xml file. He shares the code for this class and the updates to make to the configuration and bootstrapping to correctly load the class.

tagged: generated sitemap jigsaw static site package tutorial

Link: https://mattstauffer.com/blog/adding-an-auto-generated-sitemap-to-your-jigsaw-based-static-site/

Tomas Vortuba:
How I Got into Static Trap and Made Fool of Myself
Apr 30, 2018 @ 10:38:38

Tomas Vortuba has a new post to his site sharing some of his experiences with static function analysis (and fixing) and the trap he got himself into when evaluating the current state of his code. This is a continuation of his series covering the use of the SymfonyCodingStandard package.

PHP story with code examples, copy-cat killers, just a little bit of static, consistency, sniffs and way to prevent all that ever happening ever again.

In his post he uses an example "fixer" for testing DocBlock comments to see if they provide much useful information over what the PHP itself provides. He dives "under the surface" and looks at the steps it takes to accomplish this. He then gets to the "trap" part, showing the code behind the fixer and how depending on 3rd party packages and their parsing lead to code mistakenly being "fixed". He finishes up the post with a look at a few solutions that could help make these "traps" easier to fix including dependency injection and preventing the issue specifically with static functions.

tagged: static function analysis fixer codingstandard tutorial

Link: https://www.tomasvotruba.cz/blog/2018/04/26/how-i-got-into-static-trap-and-made-fool-of-myself/

Laravel News:
Going Deeper with Factories Through Factory States
Apr 11, 2018 @ 10:11:58

The Laravel News site has posted a tutorial that takes you deeper with factories when using the framework looking at the factory states.

I suspect that if you are familiar with Laravel that you might use factories and possibly even factory states in your application development. The documentation shows you the mechanics of using factories to seed and create test data, but there are a couple of guiding thoughts to using factories with your models effectively that I’d like to consider.

He starts with two options he sees for using factory states: making them with static values or using them to make simple models instead. He goes through these two options, introducing some of the basic concepts of each, how it would work and the code to make it happen. He also covers a few other approaches including the use of a trait to include the factory functionality directly in a class. He finishes the post with a few links to more information in the Laravel manual and other outside resources/tools.

tagged: laravel factory state static value model tutorial

Link: https://laravel-news.com/going-deeper-with-factories-through-factory-states

Mark Baker:
Discharging Static #2
Apr 05, 2018 @ 10:22:09

Mark Baker continues his series looking at the use of static properties and methods in applications and how to test them. In this second part of the series he focuses more on some of the unintentional side-effects that could happen when you're trying to refactor them out.

In the first article in this series, I wrote about the problems with testing static methods in classes, and showed a few approaches that allow us to write mocks for statics. Testing classes where we have static properties is a lot harder to manage, because any changes made in any test to those properties are global, across the entire test suite, not simply the test that we are running. Static properties are part of the global state, and we can’t simply tearDown() those changes in the way that we do with instances — at least we cannot easily do so if the property has a visibility of private or protected.

He goes through an example of a refactor from a static property (essentially in the global scope) to a private property. He points out the issue of setting a static value in what seem to be separate child classes, the fact that it actually changes the base value, not the individual ones, leading to potentially unintended consequences.

His main recommendation is to avoid the use of static properties all together. Where that's no possible (like in a legacy project) he offers two potential solutions: either replace them with constants if they're never changed or changing them to instance properties.

tagged: static property series part2 refactor consequences testing

Link: https://markbakeruk.net/2018/04/03/discharging-static-2/

Marc Baker:
Discharging Static #1
Mar 14, 2018 @ 14:49:47

On his blog Marc Baker about static calls and the trouble they bring in your applications. It's a continuation of ideas that Kore Nordmann shared previously on his site.

It’s been seven years since Kore Nordmann first published “static considered harmful” on his blog, explaining the problems with using static method calls in classes, and the difficulties that they cause when trying to test that class. Seven years on, and those difficulties are still the same, and there is still new code being written using static calls despite that knowledge; but it’s often a more severe problem in legacy code with little or no unit tests.a

So why exactly are static calls so bad? If you’ve read Kore’s article, then you probably have a good idea already; but what that article doesn’t cover is approaches that we can use to make the code testable.

He covers the main issue static methods have when it comes to testing: they introduce coupling by hard-coding a dependency into your code. He talks about the static testing functionality that various PHP testing tools provided: PHPUnit, Phake and Mockery. He then focuses on a newer tool that he's discovered to help make the testing simpler: AspectMock. He gives an example of it in use and some examples of tests using anonymous classes to make it easier to create tests on the fly.

tagged: static testing phpunit example harmful tutorial aspectmock

Link: https://markbakeruk.net/2018/03/13/discharging-static-1/

Tim MacDonald:
Backup multiple sites and frameworks with 'Laravel Backup'
Nov 01, 2017 @ 10:56:19

In a new post to his site Tim MacDonald covers a Laravel backup package provided from Spatie and how to use it to backup your sites (and not just Laravel ones).

Using Spatie's Laravel Backup package you can backup several sites on a server from a single app install, including Laravel apps and WordPress sites.

Laravel Backup is a fantastic package brought to us by to ever awesome team at Spatie, and is only one of the many great packages they have in the wild. This package makes it super simple to backup your Laravel applications, including all your files and production database, to a 3rd party storage facility, such as Amazon S3.

He then describes his own setup, running a smaller server that hosts several of his sites that use Laravel, WordPress and static content. He needed a backup solution that would allow for him to manage backups for all of them. He then walks through the setup of the package and how to configure it for each site type. The post finishes with the command to execute and run the backup for the site and how to schedule it as a part of the recurring jobs handling.

tagged: laravel backup tutorial introduction setup configure wordpress static

Link: https://timacdonald.me/backup-multiple-sites-frameworks-laravel-backup/