Hi. I’m Matthieu Napoli.

These and thoughts and experiments about web development and programming.

Decoupling packages

Decoupling packages is a hard thing. There are not a lot of options, and this blog post is about how some options are better than others.

Let’s say for example that you are writing a “package”, or library, to respond to HTTP requests (that kind of package could be considered the basis for a web framework). How do you handle routing?

If you write your Router package as an independent package (which is good: small and specialized packages are more reusable and maintainable), you might not want to couple the HTTP package to the Router package: you want to leave users free to choose the router of their choice.

So, what are your options to make the HTTP package and the Router package decoupled from each other?

Read on →

A retrospective on open sourcing projects at My C-Sense

Today is my last day at My C-Sense, after almost 2 years working on creating and modernizing existing PHP applications. This has been an incredibly rich experience in an excellent work environment, I will surely miss this job and my colleagues.

Now is probably a good time to look back on our open source experience as a company, and as a developer. It all started when we moved all of our code to GitHub: we used a lot of open source software and libraries, and we thought we might try to contribute some day with some small internal projects too.

So that’s how MyCLabs was born: let’s keep the door open for open source because “why not”.

Read on →


The Repository interface

Here is the definition of a repository for Eric Evans in his Domain Driven Design book:

A repository represents all objects of a certain type as a conceptual set (usually emulated). It acts like a collection, except with more elaborate querying capability. […] For each type of object that needs global access, create an object that can provide the illusion of an in-memory collection of all objects of that type.

While I love Doctrine, I really dislike their repository interface because it doesn’t look and act like a collection. And that’s too bad because Doctrine even provides a very good abstraction for collections through the doctrine/collection project. It even supports filtering with criterias over collections and repositories.

I know that Doctrine is not targeted at Domain Driven Design only, but I think having a better repository interface would still benefit the project and the users.

Here is a basic repository interface I tend to use instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
interface Repository
{
    function add($entity);

    function remove($entity);

    function count();

    /**
     * @throws NotFoundException
     * @return object
     */
    function get($id);

    /**
     * @return array
     */
    function find(Criteria $criteria);

    function toArray();
}
Read on →

php

Working with mails in dev environment

If your application sends emails, you probably don’t want emails to be sent when you are developing on your machine.

If you use nice libraries like SwiftMailer, it is easy to use a mock instead of sending real emails. But if you don’t, you can act on PHP’s configuration: instead of installing and using a real SMTP server on your machine, you can fake one using a simple script.

Read on →


Running composer install when you switch branch

When working with Composer and git branches, you will end up either:

  • reinstalling dependencies each time you switch branch
  • or meeting weird bugs because you didn’t

because composer.json may have changed between branches.

To have composer automatically re-install dependencies when you switch to a branch, simply create a .git/hooks/post-checkout file in your project repository:

1
2
3
4
5
#!/bin/sh

cd $GIT_DIR/..

composer install

This is a git post-checkout hook (as the name suggest)


I herd you like tests

So you chose to test your application using unit/functional tests.

How do you ensure your tests do indeed test what you expect?

Fear not! Here comes TestsTester!

Read on →


Friend services?

Using the Domain Driven Design methodology, I sometimes end up on such a situation: a behavior in a model class is complex and involving other classes. It’s time to put that behavior in a service (or a factory if the situation applies to a constructor).

The problem is that in my service, I am not in the context of the domain class. If I move Foo::something() into FooService::something($foo), then I can’t access private properties of Foo, thus limiting me to the public API of Foo.

Now I end up adding accessors, thus breaking encapsulation and complexifying everything where all I wanted was improving the code.

VB.Net has a concept of “Friend” visibility, i.e. if A is friend with B, then A can access private properties of B (or something like that it’s been a long time :p). PHP doesn’t have such a concept natively, but here is a tryout to apply it with workarounds.

Read on →