RSSAmplifier

Blog

Andreas Möller

RSS feed ↗36 posts

Latest posts

Adopting a reasonable PHP version support policy

Adopting a reasonable PHP version support policy The fourth Thursday in November of every year is a special day for people working on and with PHP. On that day, the release managers of PHP release a new major or minor PHP version. On that day, two years after its initial release, another PHP version reaches the end of active support. And on that day, three years after its initial release, another…

Understanding the lifecycle of a PHP version

Understanding the lifecycle of a PHP version You build, maintain, run, and distribute PHP applications and packages - but are you aware of the lifecycle of a PHP version? Initial release The initial release marks the beginning of the lifecycle of a PHP version. Active support Active support for a PHP version begins with the initial release and ends two years after the initial release. During…

Avoiding empty() in PHP

Avoiding empty() in PHP The language construct empty() appears rather versatile. It's like a Swiss army knife with a thousand blades, ready to hurt you if you grab it by the wrong end. Or a jack of all trades, master of none. Most of all, empty() is a poor communicator. I spot empty() in poorly-aged closed-source projects. I discover empty() in brand-new closed-source projects from last week. And…

Introducing PHP-CS-Fixer into legacy projects

Introducing PHP-CS-Fixer into legacy projects You are working on a legacy PHP project and want to use friendsofphp/php-cs-fixer to enforce a consistent coding standard. But you are unsure how to do that without causing problems. What could be a strategy for introducing PHP-CS-Fixer into your legacy PHP that reduces risk and invites other developers to collaborate? Requirements If you want the…

Collecting line, branch, and path coverage with PHPUnit

Collecting line, branch, and path coverage with PHPUnit phpunit/phpunit allows the collection of code coverage through phpunit/php-code-coverage , which currently supports two code coverage drivers: PCOV and Xdebug . With PCOV, you can collect line coverage; with Xdebug, you can collect line, branch, and path coverage. The PHPUnit documentation explains line, branch, and path coverage as follows:…

Avoiding one-liners in PHP

Avoiding one-liners in PHP PHP developers frequently share how they transform multiple lines of PHP code into one-liners. Through the eye of a maintainer, how do these one-liners compare to their multi-line counterparts? After all, writing PHP code is not a problem, but maintaining it is even more so. Maintenance of PHP projects With a safety net of automated tests, the maintenance of a PHP…

Sharing configurations for PHP-CS-Fixer across projects

Sharing configurations for PHP-CS-Fixer across projects You are working on PHP applications and packages and use friendsofphp/php-cs-fixer to enforce coding standards. What are your options for sharing your configurations for friendsofphp/php-cs-fixer to enforce a consistent coding standard across these projects in your organization? Configuring PHP-CS-Fixer You can configure…

Organizing test code in PHP

Organizing test code in PHP You are working on a PHP application or a package. You have decided that you want to use: infection/infection for running mutation tests phpbench/phpbench for running performance tests phpunit/phpunit for running unit, integration, functional, and end-to-end tests Perhaps you are considering other design and testing frameworks as well, for example:…

Documenting the system under test in PHPUnit

Documenting the system under test in PHPUnit You are working in a code base you inherited from someone else or have not touched in years. Customers have repeatedly asked for a feature or demanded that you fix a bug. You already know which class or classes you need to change, but - you still have difficulty understanding how to use them and what they do. In your attempts to understand the code…

Extending PHPUnit with its new event system

Extending PHPUnit with its new event system You can extend phpunit/phpunit by creating and using abstract test classes and traits - or by using the event system of phpunit/phpunit . As one of the contributors to the new event system of phpunit/phpunit , I will give you an overview of the iterations of the event systems of phpunit/phpunit , show how you can migrate from any of the previous event…

Indenting YAML files

Indenting YAML files YAML files are omnipresent: whether you use continuous integration systems, external code quality tools, or container virtualization, the chances are that you are dealing with YAML files. At first glance, the YAML specification is straightforward: dashes create sequences colons separate key-value pairs to create mappings indentation spaces can create structure separation…

Documenting namespaces for test code in composer.json

Documenting namespaces for test code in composer.json When you build an application or library and use composer for autoloading, you typically configure one or more autoloaders for production code in the autoload section of composer.json . { "autoload" : { "psr-4" : { "App\\" : "src/" } } } phpunit/phpunit , the popular testing framework, will search directories configured in phpunit.xml or passed…

Enhancing types in PHP

Enhancing types in PHP Matthias Noback has published an article questioning whether DateTimeImmutable can be considered a primitive type . Matthias concludes that a DateTimeImmutable is not a primitive type. Primitive type We can probably agree that the following types array bool float int object string are all primitive types. Simple type We can probably also agree that a DateTimeImmutable is…

Asserting the output of Symfony console commands

Asserting the output of Symfony console commands If you write console commands using symfony/console and have not used SymfonyStyle yet, you should give it a try. SymfonyStyle provides additional methods for rendering output in the terminal. These methods allow rendering titles, section headings, success and error messages, tables, bullet lists, and more - all of them nicely formatted and…

Naming constructors in PHP

Naming constructors in PHP The chances are that you are already aware of the concept of named constructors . If not, take a look at Matthias Verraes ' excellent article Named Constructors in PHP . When it comes to consistently naming constructors, I currently apply the following rules for different types of objects. services exceptions entities value objects Services When a service requires…

Creating releases with GitHub Actions

Creating releases with GitHub Actions In October 2021, GitHub introduced a feature that enables maintainers to generate release notes for a release with the click of a button. By default, the automatically generated release notes contain a list of pull requests maintainers have merged since the last release a list of first-time contributors to that release a link to the diff, comparing the changes…

Wrapping it up

Wrapping it up Writing is a social activity: although often performed in solitude, all writers write to be read by an audience. If you have difficulties finding readers, you have failed - whether you write for print or web, fiction or non-fiction, or computer programs. Line Length Typographers of print and web agree that the hard limit for line length should be somewhere between 50 and 75…

Creating Doctrine entities populated with fake data

Creating Doctrine entities populated with fake data Creating database fixtures in projects using doctrine/orm can be cumbersome - but it doesn't have to be. When you create and populate entities with fake data by hand, your tests can quickly become bloated and difficult to understand. In need of a cure, you might extract helper methods or classes. The chances are that you use a third-party package…

Merging pull requests with GitHub Actions

Merging pull requests with GitHub Actions In May 2019, GitHub acquired Dependabot , and recently GitHub announced that Dependabot is moving into GitHub . When you follow the instructions, you will find that the migration is straightforward. However, there is one thing missing: after migrating from version 1 to version 2, Dependabot will not automatically merge pull requests anymore. That the…

Rolling up database migrations with Doctrine

Rolling up database migrations with Doctrine Introduction As a user of doctrine/orm and doctrine/migrations , it is likely that you - like me - have encountered one of the following scenarios: You have started using doctrine/migrations late in a project. Unfortunately, you can not set up a database from scratch using database migrations. You use Doctrine entities or other services in the database…

Avoiding imports and aliases in PHP

Avoiding imports and aliases in PHP PHP 5.3 , released on June 30, 2009, introduced namespaces as well as imports and aliases . Developers quickly adopted these features. Where previously a class was named Foo_Bar_Baz_Qux , namespaces allow calling it Foo\Bar\Baz\Qux . Imports With imports, I can import the fully-qualified class name and reference the class by its terminating class name: <?php…

Quickly switching between PCOV and Xdebug

Quickly switching between PCOV and Xdebug There are at least two PHP extensions I need during the development of a PHP application or package: PCOV and Xdebug. PCOV is relatively new . Built by by Joe Watkins , it provides fast code coverage for use with phpunit/phpunit . Xdebug has been around for a while . Built by Derick Rethans , it is a development tool that can not only be used for…

Using Makefiles in projects where I can not use them

Using Makefiles in projects where I can not use them In Makefile for lazy developers , I have shared how I use Makefile s to save time running frequent tasks. As a maintainer of a project, I can use any tool I like to get the job done, and of course, I use Makefiles in all of these projects. As a collaborator of a project, I do not have that luxury. I depend on the owners of a project. Some…

Switching PHP versions when using Homebrew

Switching PHP versions when using Homebrew Homebrew is an excellent option for installing one or more PHP versions on your Mac. But how can you switch PHP versions when you have installed more than one version of PHP with Homebrew? Which do you prefer - slowly or quickly ? Scenario You have installed PHP 7.4, 8.0, 8.1, and 8.2 with Homebrew. You have no idea which PHP version is currently linked,…

From @localheinz to @ergebnis

From @localheinz to @ergebnis With 2019 in review and 2020 coming closer, I am sorting out a few things related to open-source projects I am maintaining. Move I have decided to move the open-source PHP projects I am currently maintaining on my personal account @localheinz to the organization @ergebnis on GitHub . The following projects have been moved so far: ergebnis/classy ergebnis/clock…

The grass could be a lot greener on both sides of the fence

The grass could be a lot greener on both sides of the fence It is the end of 2019, and the invitation to write an article for 24 Days in December is an excellent opportunity to reflect on what has happened this year - and maybe even look back a bit further. Past In 1990 I wrote my first line of code on an Atari Portfolio, and at the time, I would have never guessed that one day I would be building…

Project notes

Project notes In almost every project I work on, I take notes in Markdown files, keep track of small lists of things that need to be done, or create small scripts - and most of these are not important enough to find their way into a journal, an issue tracker, or version control. I have observed developers who keep these files untracked within or in a separate directory outside of the project. I…

Test to the left, production to the right

Test to the left, production to the right Not only do I speak and read languages where texts are written and read from left-to-right and top-to-bottom, but I also work with programming languages where programs are written in the same order. I'm also a proponent of Test-Driven Development (TDD), a practice that suggests following three simple rules : You are not allowed to write any production code…

Running tests for PHPUnit in PhpStorm

Running tests for PHPUnit in PhpStorm Have you been working on phpunit/phpunit ? Are you a user of PhpStorm ? I have and I am, and until yesterday I have been struggling with setting up phpunit/phpunit as test framework for PhpStorm so I can run tests for phpunit/phpunit from within PhpStorm for phpunit/phpunit itself. Use Composer autoloader In any project that requires phpunit/phpunit as a…

Accessing a system-wide terminal via hotkey on macOS

Accessing a system-wide terminal via hotkey on macOS When pairing with other developers, I oftentimes notice them spending more time than necessary on things that are neither interesting nor should take a lot of time. One of these things is opening a terminal window so commands can be entered. Very often I have observed developers move around windows on the screen in search for a previously opened…

Cost and value of DocBlocks

Cost and value of DocBlocks Over the years I have added, updated, and removed a lot of DocBlocks. I have also suggested to add, update, or remove DocBlocks many times, as well as explained why I believe a DocBlock makes sense in one case, and doesn't in another. In order to have a document I can refer to if I need to explain my reasoning again, I'm writing this article - maybe it's of use for you…

Code coverage is a meaningless metric

Code coverage is a meaningless metric Oftentimes people ask: How much code coverage is sufficient? While I believe that this is a legitimate question to ask, especially when just getting started with testing, there is a better, a counter-question: Do you want to do Test-Driven Development or not? This question has three possible answers: Yes No What is Test-Driven Development? Test-Driven…

Makefile for lazy developers

Makefile for lazy developers Whatever the size of the software project, I believe in, subscribe to, and promote Continuous Integration . Personally, I rely on GitHub Actions as an automated build system. Even this blog here is built with, and eventually deployed into production using GitHub Actions. Regardless of whether an automated build system can be set up and used for a project or not, I…

Normalizing composer.json

Normalizing composer.json If you are using composer , you have probably modified composer.json at least once to keep things nice and tidy. In fact, when I started using composer in late summer 2012, I of course edited composer.json manually, even when I wanted to add or update dependencies. I soon learned that one should run composer require foo/bar:~x.y.z instead, as this prevents pulling in…

Pretty-printing JSON with custom indentation

Pretty-printing JSON with custom indentation PHP 5.4 added the JSON_PRETTY_PRINT , JSON_UNESCAPED_SLASHES , and JSON_UNESCAPED_UNICODE constants for use with the json_encode() function. For example, you can use the JSON_PRETTY_PRINT constant to encode a value to a JSON string with four spaces of indentation. <?php declare (strict_types= 1 ); $data = [ 'name' => 'Andreas Möller' , 'emoji' => '🤓' ,…

Essential PhpStorm plugins

Essential PhpStorm plugins If you are like me and have been programming for a bit, you probably have worked with several editors and IDEs. Over the years, I have tried a bunch of them - with varying degrees of successes. Some of them were just the right tool to get started, others were recommended by friends and developers along the way. Most of them were quickly outgrown by the projects I worked…