The new functional language. Fully interoperability with PHP 🚀

https://phel-lang.org/
https://phel-lang.org/

The Phel Language

Phel is a Functional Programming (FP) language that compiles to PHP. It is a dialect of Lisp inspired by Clojure and Janet.

Features

  • Good error reporting
  • Different Datastructures (Arrays, Tables, and Tuples)
  • Macros
  • Recursive functions
  • Powerful but simple Syntax
  • REPL

Why Phel?

Phel is a creation of Jens Haase and, as he mentions on the website, “it is the result of many failed attempts to do functional programming in PHP”. He was looking for something with these characteristics:

  • A LISP-inspired
  • Functional Programming language
  • That runs on cheap hosting providers
  • And is easy to write and debug

From my point…


How to escape the mocking hell

Image for post
Image for post

What is actually happening when we create a mock? Which types of mocks are there? Is mocking good or bad? Well, as always, everything depends on the context. And here we will consider some of the main situations about when to mock and when not to mock, but especially why.

What happens when you mock something?

First of all, we should define what a mock is:

In a unit test, mock objects can simulate the behavior of complex, real objects and are therefore useful when it is impractical or impossible to incorporate a real object into a unit test.

Mocking makes sense in a unit testing


Another way of sharing quick suggestions with your team

Image for post
Image for post

Imagine this situation

You are reviewing a Pull Request (PR) and you see some minor improvements or suggestions that you would like to share with the author. You might write some comments, and usually, that would be sufficient.

But imagine that in order to transmit your “whole idea” you would need to change some files because just communicating the full picture will end up in a huge comment which might be not as clear as it could be.

What possibilities are there apart from just comments in a PR?

Well, there are multiple options.


The spread operator to the rescue.

Image for post
Image for post

Flattening a one-level array

I have seen people using the array_merge function in a loop like:

$lists = [
[1, 2],
[3, 4],
[5, 6],
];
$merged = [];foreach($lists as $list) {
$merged = array_merge($merged, $list);
}
// $merged === [1, 2, 3, 4, 5, 6];

This is a very bad practice because it’s a (memory) performance killer!

Instead, you should use the spread operator (in PHP since 5.6!):

$lists = [
[1, 2],
[3, 4],
[5, 6],
];
$merged = array_merge(...$lists);
// === [1, 2, 3, 4, 5, 6];

Unpacking an assoc-array

What if you had an assoc-array instead like this one?

$lists = […


An alternative to the missing feature in PHP: Generics

Image for post
Image for post

The perfect combination

We will use this snipped for our examples

Having a class, `Customer`:

/** 
* @psalm-immutable
*/
final class Customer
{
// Using PHP 8 constructor property promotion
public function __construct(
public string $name,
) {}
}
// We create a list of 6 customers
$customers = array_map(
fn(int $i): Customer => new Customer("name-{$i}"),
range(1, 6)
);

Whenever we want to manipulate a…


How to focus and have self-discipline

Image for post
Image for post
The Pilsum Lighthouse, on Germany’s North Sea coast.

No goal should be an achievement on their own, but the process itself that helps us to go in the direction of these goals.

Goals, in business and in life in general, should be conceived as directions, where their real intention is to help us accomplish more in the way we planned or want.

If we get rewarded only for results and not for processes, we will become pretty miserable.

Society doesn’t reward the journey, but the results. And that’s exactly part of the problem when you are too focused on how society acts on you. Of course, it’s important…


How to write proper unit tests to already written code.

Image for post
Image for post

These tests are also known as Characterization tests.

A characterization test describes the actual behavior of an existing piece of software, and therefore protects existing behavior of legacy code against unintended changes via automated testing. This term was coined by Michael Feathers.

They enable and provide a safety net for extending and refactoring code that does not have adequate unit tests. A test can be written that asserts that the output of the legacy code matches the observed result for the given inputs.

How to start?

These are my learnings one year after reading Working Effectively with Legacy Code and applying it to…


declare(strict_types=1);

Image for post
Image for post

In December 2015, PHP 7 introduced scalar type declarations and with it the strict types flag.

To enable the strict mode, a single declare directive must be placed at the top of the file. This means that the strictness of typing for scalars is configured on a per-file basis. This directive not only affects the type declarations of parameters, but also a function’s return type.

The good thing about declaring a PHP file as strict is that it actually applies to ONLY the current file. It ensures that this file has strict types, but it doesn’t apply to any other…


When, how, and why.

Image for post
Image for post

TL;DR: If you see something, in the scope of your current task, that can be easily improved, improve it. And if you have any questions about it, ask.

What is refactoring?

Refactoring means improving your code. It can go from making a variable name more readable, extract some lines of code into a private method, or separate the responsibilities of a class into subclasses, for example.

Refactoring is the action of showing that you care about what you do as a professional. It can be a controversial topic; it is indeed one of the major controversial topics since a long time ago. …


Final, or not final, that’s the question

Image for post
Image for post

TL;DR: Benefits of final classes: clear contracts, isolated side effects, testability, low complexity and cognitive load, code fluidity, and confidence in yourself.

Motivation

Reduce the scope visibility to the minimum

When you see a class prefix with final you will prevent a particular class to be extended by any other, which not only makes it more readable but also makes you be sure that the scope of the logic where you are is limited to that particular class.

Encourage “composition over inheritance” mentality

The Open-Close Principle states: open for extension but close for modification.

If for any reason, a good one you should be completely aware of, you decide to create an inheritance…

Jose Maria Valera Reales

Aka: Chema. I love what I do: software, music, and sport, but here I write mostly about software.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store