Yesterday, a member of the core team asked me for help. He didn't remembered the naming convention for bundle extensions. The convention is simple enough as this is basically the name of the bundle with Extension as a suffix instead of Bundle. But that didn't work for him. Frankly, we were stuck. I had no idea why it wouldn't work for him as it definitely works for me. But a few minutes later, he found the problem. He had a typo in the namespace declaration:

// file is Foo/Bar/Baz.php
namespace Foo/Bab;

// notice that the namespace is Foo/Bab
// but the path is Foo/Bar!

class Baz
{
    // ...
}

No way I would have been able to find the problem as I had no access to his code. This is really frustrating as he does not work for the person you are trying to help, but you know that this should definitely work. At some point, you start yelling at the framework, and eventually you give up because the damn thing is too "complex". I had the exact same problem more than once in the past and I bet that most of you had it too. And if you don't give up, you can spend hours before spotting the problem... and start yelling at you!

Even if the problem has nothing to do with the framework, is it possible to enhance the developer experience here? Can Symfony2 help the developer avoiding this error in the first place? Every time someone reports a problem like this one, we should think how we can change the framework for the better.

After thinking a bit about this specific issue last night, it turns out that we can easily improve the situation and save hours of frustration all around the world.

Whenever the standard Symfony2 autoloader finds a file where the class to autoload should be defined, we can easily check that the class is effectively declared in the file we have just included. If that's not the case, there is something wrong:

if (!class_exists($class, false) && !interface_exists($class, false)) {
    throw new \Exception(sprintf(
        'The autoloader expected class "%s" to be defined in file "%s".
         You probably have a typo in the namespace or the class name.',
    $class, $file));
}

The actual patch is a bit more complex as it should not slow down your application in production, but you get the idea.

This enhancement will be available in Symfony SE beta2.

Published in #Living on the edge