hautelook/AliceBundle — pull request #6 by KingCrunch (2013-08-25 20:33:02+00:00)
Title: Undefined variable $loader

I ran into an issue, because depending on the environment I load different fixtures, even sometimes non at all. If `getFixtures()` returns an empty array the `Loader` fails with

PHP Warning: Uncaught exception 'Symfony\Component\Debug\Exception\ContextErrorException'
with message 'Notice: Undefined variable: loader
in .../vendor/hautelook/alice-bundle/Hautelook/AliceBundle/Alice/Loader.php line 96'

While looking at the code it absolutely makes sense, because it looks like

public function load(array $files)
{
$objects = array();
foreach ($files as $file) {
/** @var $loader \Nelmio\Alice\Loader\Base */
$loader = $this->getLoader('yaml');
$loader->setProviders($this->providers);

$set = $loader->load($file);
$this->persister->persist($set);

$objects = array_merge($objects, $set);
}

foreach ($loader->getReferences() as $name => $obj) {
$this->persister->detach($obj);
$this->references->set($name, $obj);
}
}

If `$files` is empty, `$loader` is never set. But this makes me wonder about another thing here: The next foreach uses `$loader` again. That is the same `$loader`-variable, that is set in the previous `freach` over and over again, which means, that it only use the latest occurence. Wouldn't it make sense to move the two lines

/** @var $loader \Nelmio\Alice\Loader\Base */
$loader = $this->getLoader('yaml');
$loader->setProviders($this->providers);

out of the first `foreach`?