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

Laravel News:
Botman Playground: Start Your Next Chatbot Idea In Your Browser
Feb 13, 2018 @ 09:35:58

On the Laravel News site there's a new post covering a new feature in Marcel Pociot's "Building a Chatbot" series (that makes use of the Botman package). This latest addition, a playground for testing and debugging bots allows you to get up and running without having to set up a complete environment.

Marcel Pociot recently launched a course called “Build A Chatbot,” a step-by-step video course on developing, extending, and testing Chatbots and Voicebots. Marcel is the author of Botman, a PHP chatbot framework for building chatbots.

Along with his Build a Chatbot course, Marcel launched Botman Playground, which provides a quick way to get started building and debugging chatbots without setting up a local development environment.

The playground allows you to set up a new bot, add commands and test the result with an included widget, all in-browser. The playground also allows you to set up connections to external services like Facebook, Telegram and Cisco Spark. The only catch is that you must be signed up for Marcel's Build a Chatbot course to gain access.

tagged: chatbot playground botman browser code test widget

Link: https://laravel-news.com/botman-playground

Matthias Noback:
Setting the stage: Code complexity
Jan 18, 2018 @ 12:29:07

In a post to his site, Matthias Noback talks about code complexity and how this relates to the overall "churn" (the rate of change) in a project.

Code complexity often gets measured by calculating the Cyclomatic Complexity per unit of code. The number can be calculated by taking all the branches of the code into consideration. [...] In general, we always strive for low code complexity. Unfortunately, many projects that you'll inherit ("legacy projects"), will contain code that has high code complexity, and no tests.

[...] Code complexity doesn't always have to be a big problem. If a class has high code complexity, but you never have to touch it, there's no problem at all. [...] What's really dangerous for a project is when a class with a high code complexity often needs to be modified. Every change will be dangerous. [...] Michael Feathers introduced the word "churn" for change rate of files in a project. Churn gets its own number, just like code complexity.

He then talks about combining these two numbers to provide an even more in-depth look at your code. It can give more insight into the relationship between "difficult to change", "number of changes" and the times a file has changed in the past. He mentions "it's okay" thinking (the current state is alright but not great) and shares some of his own hypotheses, observations and advice.

tagged: code complexity churn statistic evaluation combination

Link: https://matthiasnoback.nl/2018/01/churn-legacy-code/

Sergey Zhuk:
Managing ReactPHP Promises
Jan 18, 2018 @ 10:50:01

In a new post to his site Sergey Zhuk has a tutorial showing you how to manage promises in ReactPHP. Since promises are fired asynchronously they can be difficult to manage and use their output across the application.

Asynchronous application is always a composition of independently executing things. In concurrency, we are dealing with a lot of different things at once. [...] So, to make concurrency work you have to create a communication between these independent parts to coordinate them. And here come promises. They are the basic unit of concurrency in an asynchronous application. They are the blood of the asynchronous application and move the results between different tasks across the code.

He then covers a few different situations and offers advice on how to more correctly handle them:

  • I don’t know exactly what the resolver will give me
  • I want to reject a promise but without throwing an exception
  • I want to run multiple tasks and when they all finish do something else
  • I have some pending tasks and want to continue once I receive the first feedback
  • I have some pending tasks and want to continue once the first one is completed
  • I have some pending tasks and want to continue once a certain number of tasks will be completed

Code is provided for each of the situations giving you an easy, ready to use example for your application. Most require only a few lines to get the job done and can be very useful in the right circumstances.

tagged: reactphp manage promises situation code example tutorial

Link: http://sergeyzhuk.me/2018/01/16/reactphp-managing-promises/

Intracto.com Blog:
Paying Technical Debt - How To Rescue Legacy Code through Refactoring
Jan 10, 2018 @ 12:31:45

The Intracto.com blog has a post sharing some ideas and methods about how to rescue legacy code through refactoring. In it author Door Jeroen Moons shares from his own experience working with legacy applications and offers practical advice you can apply in your own legacy codebase to "tame the beast".

I have good news for you! Squirrels plant thousands of new trees every year by simply forgetting where they leave their acorns. Also: your project can be saved.

No matter how awful a muddy legacy code mess your boss has bravely volunteered for you to deal with, there is a way out of the mire. There will be twists and turns along the way, and a monster behind every other tree. But, one step at a time, you will get there.

He starts by defining technical debt and the idea of "code cancer", those shortcuts and hacks that are taken during development and slowly corrupt the quality of the code. He then covers one of the harder parts of refactoring - persuading the customer that it's an effective use of time. He also mentions replacing current code with quality code, making problems visible, working on the hard parts and code ownership. The post finishes up with mentions of testing for quality and functional assurance, creating reusable libraries and isolating and replacing things a piece at a time.

tagged: technical debt rescue legacy code refactor tutorial

Link: https://blog.intracto.com/paying-technical-debt-how-to-rescue-legacy-code-through-refactoring

Jason McCreary:
The Debugging Golden Rule
Dec 14, 2017 @ 11:41:42

Jason McCreary has a post on the Dev.to site sharing what he calls the "debugging golden rule" to keep in mind when stepping through and trying to debug your code for issues.

Developers, especially new developers, often forget this when debugging. We jump into the debugger. We add tracing statements. We review the commit log.

Such actions can be misguided. Before debugging the code we must follow a moral code. Debugging needs a Golden Rule. A rule to remind developers of a few important facts of debugging.

Boiled down to its basics the "golden rule" here is that, most of the time, it's not the tools that are the issue - it's you and your code. He shares the common thoughts we've all had when debugging ("it's the upgrade, not my code") but points out that, regardless of where the issue is, it still needs to be fixed. Even if it is something in the tool, some odd bug or weird functionality that only kicks in once in a blue moon, you still have to ultimately make it work.

tagged: debugging goldendrule code tools opinion

Link: https://dev.to/gonedark/the-debugging-golden-rule-7cb

Matthieu Napoli:
Organizing code into domain modules
Dec 11, 2017 @ 12:57:01

In a post to his site Matthieu Napoli shares some recommendations about how to organize the code in your application using a "domain modules" approach. This is an organization method that relates the code based on functionality it relates to rather than the type of object it is.

We recently discussed 2 topics seemingly unrelated with my colleagues at Wizaplace: how to organize code [and] how to organize teams.

Regarding “how to organize teams”, we were discussing Spotify’s Feature teams. In a “classic” organization, teams are usually formed by grouping people based on their job title. [...] But in a “feature team” organization, teams are organized… by features. [...] The pros of this kind of organization are numerous and I do not intend to go over them here. But what does this have to do with code organization?

He starts with the "classic" code structure - organized by each item's type (ex: Entity, Service, etc). He then suggests reorganizing it move by what it does in the application, namely which module it belongs in best. He briefly touches on dependencies, "agile design" and the differences between a normal "product" and a "product" in e-commerce. He then applies these ideas and shares a domain organized directory structure, trying to reduce the overall complexity of the structure and the overall cohesion of the codebase.

tagged: domain module organization code directory structure tutorial

Link: http://mnapoli.fr/organizing-code-into-domain-modules/

SitePoint PHP Blog:
Your First PHP Code
Nov 02, 2017 @ 12:34:02

On the SitePoint PHP blog there's a new post from author Tom Butler that starts from the very beginning and shows you how to write your first PHP code. The article is an excerpt from the SitePoint book PHP & MySQL: Novice to Ninja, 6th Edition.

PHP is a server-side language. This concept may be a little difficult to grasp, especially if you’ve only ever designed websites using client-side languages like HTML, CSS, and JavaScript.

A server-side language is similar to JavaScript in that it allows you to embed little programs (scripts) into the HTML code of a web page. When executed, these programs give you greater control over what appears in the browser window than HTML alone can provide. The key difference between JavaScript and PHP is the stage of loading the web page at which these embedded programs are executed.

At this point they assume you've already set up the server to allow for PHP execution. They then provide an example of a HTML page with a bit of PHP that generates a random number. It then gets into some of the basic language syntax and statements and how they're used in the PHP code.

tagged: first code tutorial introduction language book excerpt

Link: https://www.sitepoint.com/first-php-code/

Symfony Finland:
Running Symfony without a web server on Docker using PHP-PM
Oct 25, 2017 @ 10:30:05

On the Symfony Finland site there's a tutorial posted showing you how to run Symfony without a web server using Docker and PHP-PM.

Docker containers have been becoming an increasingly common deployment method in the past few years. I've deployed some Node.js app in containers, which are very simple to deploy as there's a single process that's easy to run. I found running PHP to be more complex.

[...] When running Nginx and PHP-FPM you are forced to run a pair of of containers, one for PHP-FPM and one for PHP-NPM. [...] But I would rather just run a single process to run the application. This is how Go, Node.js and even .NET Core apps are deployed. Can PHP do without a middle man?

While it may not be the most performant way to run a PHP and Symfony application, the author wanted to try it out and see how difficult it might be. The code for the setup is provided and, while the end result was a bit difficult to get to, it was possible.

tagged: symfony finland webserver without docker phppm tutorial code

Link: https://symfony.fi/entry/running-symfony-without-a-web-server-on-docker-using-php-pm

Jason McCreary:
Writing Clean Code (Part 2)
Oct 19, 2017 @ 11:24:52

Jason McCreary has continued his series looking at writing "clean code", providing a few helpful hints you can integrate into your daily development work. In part two he goes a bit "deeper" and talks about grouping and encapsulation.

In Part 1 of Writing Clean Code I outlined three simple practices of formatting, naming, and avoiding nested code. All in an effort to improve code readability.

In Part 2, I want to go a little deeper and cover grouping. When I say grouping, I’m really talking about the Object Oriented Programming paradigm of encapsulation. Whether we group the code into a function or a class is often not important. What is important is did we improve the readability of the code.

He starts off by describing the goal of this grouping and lists three motivations for using it as a part of your application's architecture:

  • Improving communication
  • Couple data
  • Organizing code

For each, he includes a brief summary of the topic and some code examples illustrating it in action where appropriate.

tagged: clean code example opinion communication coupling organize

Link: https://jason.pureconcepts.net/2017/10/writing-clean-code/

Asmir Mustafic:
How to add custom error codes to your Symfony API responses
Sep 22, 2017 @ 11:10:01

Asmir Mustafic has posted a guide on his site showing how to create custom error codes in the API responses from your Symfony-based application.

When writing APIs, a proper error handling is fundamental. HTTP status codes are a great start, but often when we deal with user inputs is not enough. If out model has complex validation rules, understanding the reason behind an 400 Bad Request error can be not trivial.

Fortunately when for symfony developers there are many libraries to deal with it. Symfony Validator, <a href="https://github.com/symfony/form>Symfony Form, <a href="https://github.com/FriendsOfSymfony/FOSRestBundle>FOS REST Bundle and JMS Serializer combined allows you to have nice error messages to be shown to your users.

He walks you through the code required to create the default error handling with an "author" example. This includes the creation of the entity as well as the form and controller to handle the request/response. He then refactors this away from the default to create the custom error handler with handlers for the message and code to be returned. The post ends with the configuration changes to register it with the application and what the result ends up looking like.

tagged: symfony tutorial custom error code api example

Link: http://www.goetas.com/blog/how-to-add-custom-error-codes-to-your-symfony-api-responses/