Oscar Merida

twitter: @omerida

Articles

Old School Recipes for Output

By Oscar Merida

The Spacetraders client uses a strongly typed object to parse API responses. However, as it’s grown in functionality, the user experience—especially the output–has become increasingly cumbersome. We added a basic help command, but the output of our commands still leaves much to be desired. Scrolling through endless lines of raw dumps of complex objects isn’t just tedious-it’s inefficient. Let’s address the UX and make our tool a pleasure to use. We’ll show how PHP’s string manipulation capabilities can render clean, readable, and even color-coded output. From sprintf() to venerable ANSI escape codes, let’s dive into some old-school functions to polish our output. by Oscar Merida

Published in Lounging Around with PHP, November 2024

Slow is Smooth Sailing with Captain Hook

By Oscar Merida

We’ve been working on an API client and making decent progress while we discover new tools that we can use to improve our development work. If you’ve been following along, you may be asking why it seems we’ve made so little progress in implementing all the functionality available for the API. This month, we look at the progress to date and explain why we’re working at a slower pace. Finally, we will check out (no pun intended) how to use Git hooks to enforce project standards and norms. by Oscar Merida

Published in The Symfony of PHP, October 2024

PHP Foundry

By Oscar Merida

Are you managing a growing list of features in your project? As your codebase evolves, the complexity and depth can outstrip our ability to manage and document it. While working on an API client for Spacetraders, I encountered it firsthand and found it overwhelming, even with a handful of commands. Despite following conventions, details are scattered across multiple files, and I can’t keep track of them all in my head. In this article, we’ll explore how attributes introduced in PHP 8, coupled with the Reflection API, can help us tame complexity. by Oscar Merida

Published in PHP Is Listening, September 2024

The Art and Mechanics Of Readable Code

By Oscar Merida

We covered how to add static analysis with PHPStan in the previous issue to ensure our code is parsable by the interpreter and to make full use of argument and return type hints throughout our application. Our code is also how we communicate with colleagues and contributors who have to interface, extend, and maintain it. We should put some effort into making it easy for them to understand how it works, trace its execution, and find potential problems. Another valuable tool to reduce friction and boost adoption is to use a formatter or beautifier to enforce some coding standards. We’ll look at PHPCodeSniffer and then explore how we can run it frequently for immediate feedback. by Oscar Merida

Published in HaPHPy Developers, August 2024

PHPStan’s Watchful Eye

By Oscar Merida

Keeping the type hints in argument and return types makes your code easier to understand, describes intents to colleagues, and prevents bugs from sloppy type coercions. If you’re the lead, the responsibility can’t fall on your shoulders alone to make sure your team is being diligent about using them properly. Nor should you be expected to check that code will work properly during code reviews. In this month’s article, I’ll show you why you need to add PHPStan to your project and how to get the most out of it by gradually increasing its scrutiny and documenting your types accurately. We’ll also see how you can extend PHPStan to check for custom rules made to analyze your code and enforce coding practices. by Oscar Merida

Published in Search For Good Code, July 2024

Advanced Valinor and Reorganizing Code

By Oscar Merida

This month, we delve further into working with Valinor and the benefits of using strongly typed classes for API responses instead of primitive data structures like arrays. I’ll share practical tips for mapping complex JSON payloads by pointing out the pitfalls to avoid and how to find and understand Valinor’s error messages when it throws exceptions. We’ll also look at why I reorganized the client code as the number of endpoints blossomed rapidly. by Oscar Merida

Published in AI Llamas, June 2024

Typed Objects with Valinor

By Oscar Merida

Robust PHP applications handle API responses efficiently and effectively, but it isn’t easy to do so as your application sprawls and integrates with various external APIs. API response classes with a helpful dose of value objects can significantly improve code maintainability and reliability for you and your team. In this article, let’s look at practical examples with our Spacetraders client. by Oscar Merida

Published in PHP Reflections, May 2024

Dependency Injection with PHP-DI

By Oscar Merida

Any codebase can quickly devolve into a tangled mess of function calls and copy-pasted snippets, which is difficult to adapt without some upfront discipline and tools that support some consistent organization. If we want our programs to be reusable, concise, and testable without hassle, then we should use dependency injection as a guiding principle to write loosely coupled components. Each piece can focus on doing one thing well while ignoring implementation details that aren’t concerned with it. We can also swap dependencies to control how code behaves in different environments. by Oscar Merida

Published in Deep Diving PHP Security, April 2024

Prepare for Launch

By Oscar Merida

Welcome to the PHP Foundry. My focus for this column will be to explore the tools and libraries available to augment regular, daily tasks as we work on bug fixes and new features. I want to focus on practical advice for tools that streamline development. We’ll explore how to structure a project, use tools to automate mundane tasks, assess code quality and adherence to standards, and more. To focus our efforts, we’ll work on building a PHP client for SpaceTraders.io. by Oscar Merida

Published in World Community, March 2024

Five Card Stud

By Oscar Merida

Last month, we solved how to model and shuffle a deck of cards. In this article, we’ll look at how to identify the poker hands we deal out. by Oscar Merida

Published in The PHP Gambit: Winning Strategies in Code, February 2024

Playing Cards

By Oscar Merida

For my 28th installment, we turn back to games after our tour through sorting algorithms. We’ll look at a direct way to represent playing cards and contrast it with an object-oriented approach to leverage PHP’s built-in features. by Oscar Merida

Published in Bad Bug Bounties, January 2024

Quicksort

By Oscar Merida

Quicksort is a popular sorting algorithm first published in 1961. It works quicker than most other algorithms with randomized data. It uses a “divide-and-conquer” approach to sort an array recursively. Let’s look at how to use it in PHP and learn why this could be a very short article. by Oscar Merida

Published in Generating Efficient PHP, December 2023

Shellsort

By Oscar Merida

Much like Comb Sort improved on the Bubble Sort, Shellsort also improves the execution of the Insertion Sort. Again, the improvements come from comparing items that are far apart in the array in early iterations until we are comparing adjacent items again. by Oscar Merida

Published in Command Line Picasso, November 2023

Insertion Sort

By Oscar Merida

There’s more than one way to sort things? We’ve looked at two basic sorting algorithms and will continue that exploration this month. Instead of comparing pairs of values in our unsorted arrays, we’ll take a different approach and use an insertion sort. by Oscar Merida

Published in Software Archeology, October 2023

Comb Sort

By Oscar Merida

The Bubble Sort is an easy-to-understand algorithm to order array elements from smallest to largest. One drawback of the bubble sort is that we compare one element to an adjacent element. We’ll see how the Comb sort addresses the limitation and compare its performance with the Bubble sort.
by Oscar Merida

Published in The Spectrum of PHP, September 2023

Bubble Sorting

By Oscar Merida

We’ve escaped the maze(s)! On to new subjects, lets explore sorting and the different ways we have to put things in order. We’ll start with the first one I remember writing in Turbo Pascal.
by Oscar Merida

Published in Packing Up PHP, August 2023

PHP Puzzles: Maze Directions

By Oscar Merida

We know how to plot a course through a maze. Next, we’re tasked with
translating the maze solution into a list of directions to escape the
labyrinth. We’ll study the problem, come up with a solution, and throw
in some unit testing for good measure. by Oscar Merida

Published in Be Barrier Free, July 2023

PHP Puzzles: Maze Rats, Part Three

By Oscar Merida

In the prior two installments, we’ve explored maze-generating
algorithms. Once you’re in the labyrinth, you’ll want a way to find the
exit. In this final installment, we figure out how to get from the
maze’s entrance to the exit. by Oscar Merida

Published in Evolving PHP, June 2023

PHP Puzzles: Maze Rats, Part Two

By Oscar Merida

Last month, we solved how to represent a maze by storing the
configuration of walls for each grid square within. Now, we’ve been
tasked with generating a solvable maze at random. by Oscar
Merida

Published in HTTP Burritos, May 2023

PHP Puzzles: Maze Rats

By Oscar Merida

Back in the January 2021 installment for PHP Puzzles, Sherri Wheeler
looked at drawing a path through a grid. In this first part, we delve
into the work of maze generation. by Oscar Merida

Published in Getting TEKnical, April 2023

PHP Puzzles: Grade Deviations

By Oscar Merida

We’re continuing with grades and statistics by calculating statistics for Grade Point Averages. In the educational system in the United States, it’s a way to boil down the grades across the multiple courses taken by a student. It involves translating a letter grade back to a numeric value and then calculating a weighted average. At the end of all this, we’re left with a singular number we can use for a rough comparison between students. by Oscar Merida

Published in Box of PHP, March 2023

PHP Puzzles: Stats 101 Grade Book

By Oscar Merida

Let’s go back to basics and look at some different ways to measure the average value in a list of numbers. More specifically, they’re ways of measuring central tendency. These handy techniques to identify “the middle” of a set prove useful when analyzing sales, traffic, or other frequency counts. by Oscar Merida

Published in Knowledge Crunching, February 2023

PHP Puzzles: Any Two Birthdays

By Oscar Merida

Working with dates and times is fraught with peril, so it helps to practice manipulating such data. We’ll have to be wary of how to calculate differences between dates, potentially deal with leap days, and other irregularities. by Oscar Merida

Published in PHP is Standing Tall, January 2023

PHP Puzzles: Sticker Swapping

By Oscar Merida

The FIFA World Cup wraps up this month. One thing many fans worldwide do in the months leading up to the tournament is to fill out sticker albums with each team’s roster. Completing an album, short of spending a ton of excess money, involves trading with other collectors. A common technical interview question is to find the common characters in a string or the common elements in multiple arrays. Let’s take a look at how to approach such a problem. by Oscar Merida

Published in Owning The Web, December 2022

PHP Puzzles: World Cup Draws

By Oscar Merida

A random draw is straightforward, but we usually have business rules to implement and consider in any solution. With the World Cup kicking off this month, we get a chance to look at one scenario. Can you draw the teams into proper groups before the tournament starts? by Oscar Merida

Published in The Value of the AST, November 2022

PHP Puzzles: Converting Float Strings

By Oscar Merida

Converting data is never as straightforward as we’d initially expect. Users can enter data incorrectly or in the wrong format. On the other hand, computers may not have trouble working with some data. For this article, we look again at floating-point values. by Oscar Merida

Published in The State of PHP, October 2022

PHP Puzzles: Fractional Math

By Oscar Merida

We’ve seen how working with floating-point values can be problematic. What if we had a class that worked with fractions directly by tracking the numerator and denominator as integers? Let’s see if it’s possible. by Oscar Merida

Published in Making Code, September 2022

PHP Puzzles: Decimals to Fractions

By Oscar Merida

In this installment, we look again at working with floating-point values. We approach how to take a decimal number and represent it as a fraction. by Oscar Merida

Published in PHP Blueprint, August 2022

PHP Puzzles: The Birthday Paradox

By Oscar Merida

We look at a solution to a problem that is not intuitive at first glance—the birthday paradox. Instead of calculating probabilities directly, we’ll use a simple simulation to solve the problem. by Oscar Merida

Published in Database Freedom, July 2022

PHP Puzzles: Clues for Hues

By Oscar Merida

In the last two months, we saw how to control randomness. This month, we let users guess the random sequence created by the program.

Published in Another Bright Idea, June 2022

PHP Puzzles: Controlled Randomness

By Oscar Merida

PHP Puzzles: Controlled Randomness

Published in One Last Slice, May 2022

PHP Puzzles: Making Some Change

By Oscar Merida

This article looks at a practical math problem most developers are likely to run into at some point. We’re given some amount of money and asked to figure out how to distribute it. Did you remember that computers are not very good at floating-point math?

Published in Testing The Core, April 2022

PHP Puzzles: Finding Prime Factors

By Oscar Merida

We’re building on a previous puzzle for finding integer factors. In this article, we look at how to find prime factors before turning to one more puzzle involving integer division.

Published in World Backup Day, March 2022

PHP Puzzles: Finding Integer Factors

By Oscar Merida

This article looks at solutions for finding integer factors for a given integer. Another way to put this problem is to decompose an integer into a multiple of two smaller integers. Besides hearkening back to Algebra classes of yore, we’ll discuss applications of this technique. by Oscar Merida

Published in Parallelize Your Code, February 2022

PHP Puzzles: Infamous Fizz Buzz

By Oscar Merida

Let’s start the year looking at a classic interview question—Fizz Buzz. It’s misused to screen applicants and may not be testing for what it thinks it is. Nonetheless, it doesn’t hurt to be prepared if you encounter it. We’ll look at various approaches to solve it. by Oscar Merida

Published in Domain-Driven Resolutions, January 2022

PHP Puzzles: Breakeven Point

By Oscar Merida

This month we are moving on from shifting money around time to focusing on costs at a specific moment in time. Join me as we tackle the break-even problem from last month. But first, we’ll step through it, analyzing it algorithmically to design our solution. by Oscar Merida

Published in The Zen of Mindful Programming, December 2021

PHP Puzzles: Compounding Interest

By Oscar Merida

Last month, we looked at how to output an amortization table to see how compounding interest affects some amount of money. Printing out such a table assumes we have a certain set of inputs. Moreover, it’s tedious, even simple for a computer to do. Let’s look at other ways to calculate the time value of money. by Oscar Merida

Published in The Art of Data, November 2021 —Available for Free

PHP Puzzles: Time Value of Money

By Oscar Merida

Is it better to get $100 today or $110 in one years time? The answer’s not straightforward because you could use $100 today to potentially earn more than $10 over 12 months. How do you know what to do? For that, you need to know how to calculate a rate of return or use it to compare amounts of money at different times. Hold on for a bit, we’re bending the time—space (money?) continuum.

Published in Decrypting Cryptography, October 2021

PHP Puzzles: Sending and Receiving Polybius Ciphers

By Oscar Merida

Ciphers come in many different forms and have been used since ancient times to encode and decode vital messages. Today, we use complicated mathematics to encrypt and decrypt messages. Typically, we depend on shared and secret keys to ensure the message is only readable by the intended recipients. What if you don’t have the computing power of a modern device at your disposal? This month, let’s look at using Polybius Squares to share secret messages.

Published in Testing Assumptions, May 2021

Streamlining Behat

By Oscar Merida

Behat, and feature tests overall, are crucial for giving you confidence that when you refactor code, you don’t change how an application works and for helping you understand how an application behaves in practice. Investing in integration and functional tests is worth it in the long term, and in this article, we’ll see how I also updated my Behat setup to run browser tests in Chrome.

Published in Testing Assumptions, May 2021 —Available for Free