for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Knp\RadBundle\DataFixtures;
use Doctrine\Common\DataFixtures\ReferenceRepository;
class ReferenceManipulator
{
public function __construct(ReferenceRepository $referenceRepository)
$this->referenceRepository = $referenceRepository;
referenceRepository
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
class MyClass { } $x = new MyClass(); $x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:
class MyClass { public $foo; } $x = new MyClass(); $x->foo = true;
}
public function createReferenceName($className, array $attributes = array())
$className = join('', array_slice(explode('\\', $className), -1));
$referenceId = reset($attributes);
$referenceName = $referenceId ? sprintf('%s:%s', $className, $referenceId) : $className;
return $this->generateUniqueReferenceName($referenceName);
private function generateUniqueReferenceName($referenceName)
if ($this->referenceRepository->hasReference($referenceName)) {
$referenceName = $this->incrementReferenceName($referenceName);
return $referenceName;
private function incrementReferenceName($referenceName)
if (0 === preg_match('#^(.*)(\d+)$#', $referenceName)) {
if (1 === preg_match('#^(\w+):(\w+)#', $referenceName)) {
return sprintf('%s-1', $referenceName);
} else {
return sprintf('%s:1', $referenceName);
return preg_replace_callback(
'#^(.*)(\d+)$#',
function ($matches) { return $matches[1].intval($matches[2]+1); },
$referenceName
);
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: