Skip to content

Commit

Permalink
minor #9487 unify constructor initialization style throughout symfony…
Browse files Browse the repository at this point in the history
… (Tobion)

This PR was merged into the master branch.

Discussion
----------

unify constructor initialization style throughout symfony

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | n/a

In almost all classes symfony uses property initialization when the value is static. Constructor initialization is only used for things that actually have logic, like passed parameters or dynamic values. IMHO it makes the code much more readable because property definition, phpdoc and default value is in one place. Also one can easily see what the constructor implements for logic like overridden default value of a parent class. Otherwise the real deal is just hidden behind 10 property initializations. One more advantage is that it requires less code. As you can see, the code was almost cut in half (210 additions and 395 deletions).
I unified it accordingly across symfony. Sometimes it was [not even consistent within one class](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Config/Definition/BaseNode.php#L32). At the same time I recognized some errors like missing parent constructor call, or undefined properties or private properties that are not even used.

I then realized that a few Kernel tests were not passing because they were deeply implementation specific like modifying booted flag with a custom `KernelForTest->setIsBooted();`. I improved and refactored the kernel tests in the __second commit__.

__Third commit__ unifies short ternary operator, e.g. `$foo ?: new Foo()`. __Forth commit__ unifies missing parentheses, e.g. `new Foo()`.

Commits
-------

077a089 unify missing parentheses
2888594 unify short ternary operator
2a9daff [HttpKernel] better written kernel tests
111ac18 unify constructor initialization style throughout symfony
  • Loading branch information
fabpot committed Nov 22, 2013
2 parents efff757 + 077a089 commit b74a887
Show file tree
Hide file tree
Showing 99 changed files with 395 additions and 658 deletions.
5 changes: 2 additions & 3 deletions src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php
Expand Up @@ -24,12 +24,11 @@ class DoctrineOrmTypeGuesser implements FormTypeGuesserInterface
{
protected $registry;

private $cache;
private $cache = array();

public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
$this->cache = array();
}

/**
Expand Down Expand Up @@ -90,7 +89,7 @@ public function guessRequired($class, $property)
return null;
}

/* @var ClassMetadataInfo $classMetadata */
/** @var ClassMetadataInfo $classMetadata */
$classMetadata = $classMetadatas[0];

// Check whether the field exists and is nullable or not
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Bridge/Twig/NodeVisitor/Scope.php
Expand Up @@ -29,21 +29,19 @@ class Scope
/**
* @var array
*/
private $data;
private $data = array();

/**
* @var boolean
*/
private $left;
private $left = false;

/**
* @param Scope $parent
*/
public function __construct(Scope $parent = null)
{
$this->parent = $parent;
$this->left = false;
$this->data = array();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Expand Up @@ -41,7 +41,7 @@ public function __construct(ContainerInterface $container, $resource, array $opt
$this->container = $container;

$this->resource = $resource;
$this->context = null === $context ? new RequestContext() : $context;
$this->context = $context ?: new RequestContext();
$this->setOptions($options);
}

Expand Down
Expand Up @@ -25,7 +25,7 @@
class TemplateNameParser implements TemplateNameParserInterface
{
protected $kernel;
protected $cache;
protected $cache = array();

/**
* Constructor.
Expand All @@ -35,7 +35,6 @@ class TemplateNameParser implements TemplateNameParserInterface
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
$this->cache = array();
}

/**
Expand Down
10 changes: 4 additions & 6 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Expand Up @@ -24,7 +24,10 @@
class Translator extends BaseTranslator
{
protected $container;
protected $options;
protected $options = array(
'cache_dir' => null,
'debug' => false,
);
protected $loaderIds;

/**
Expand All @@ -47,11 +50,6 @@ public function __construct(ContainerInterface $container, MessageSelector $sele
$this->container = $container;
$this->loaderIds = $loaderIds;

$this->options = array(
'cache_dir' => null,
'debug' => false,
);

// check option names
if ($diff = array_diff(array_keys($options), array_keys($this->options))) {
throw new \InvalidArgumentException(sprintf('The Translator does not support the following options: \'%s\'.', implode('\', \'', $diff)));
Expand Down
Expand Up @@ -14,7 +14,6 @@
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\UserProviderFactoryInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
Expand All @@ -23,7 +22,6 @@
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\Security\Core\Authorization\ExpressionLanguage;

/**
Expand Down
Expand Up @@ -26,7 +26,7 @@
class LogoutUrlHelper extends Helper
{
private $container;
private $listeners;
private $listeners = array();
private $router;

/**
Expand All @@ -39,7 +39,6 @@ public function __construct(ContainerInterface $container, UrlGeneratorInterface
{
$this->container = $container;
$this->router = $router;
$this->listeners = array();
}

/**
Expand Down
21 changes: 8 additions & 13 deletions src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -32,19 +32,19 @@ abstract class Client
{
protected $history;
protected $cookieJar;
protected $server;
protected $server = array();
protected $internalRequest;
protected $request;
protected $internalResponse;
protected $response;
protected $crawler;
protected $insulated;
protected $insulated = false;
protected $redirect;
protected $followRedirects;
protected $followRedirects = true;

private $maxRedirects;
private $redirectCount;
private $isMainRequest;
private $maxRedirects = -1;
private $redirectCount = 0;
private $isMainRequest = true;

/**
* Constructor.
Expand All @@ -58,13 +58,8 @@ abstract class Client
public function __construct(array $server = array(), History $history = null, CookieJar $cookieJar = null)
{
$this->setServerParameters($server);
$this->history = null === $history ? new History() : $history;
$this->cookieJar = null === $cookieJar ? new CookieJar() : $cookieJar;
$this->insulated = false;
$this->followRedirects = true;
$this->maxRedirects = -1;
$this->redirectCount = 0;
$this->isMainRequest = true;
$this->history = $history ?: new History();
$this->cookieJar = $cookieJar ?: new CookieJar();
}

/**
Expand Down
8 changes: 0 additions & 8 deletions src/Symfony/Component/BrowserKit/History.php
Expand Up @@ -21,14 +21,6 @@ class History
protected $stack = array();
protected $position = -1;

/**
* Constructor.
*/
public function __construct()
{
$this->clear();
}

/**
* Clears the history.
*/
Expand Down
36 changes: 8 additions & 28 deletions src/Symfony/Component/Config/Definition/ArrayNode.php
Expand Up @@ -22,34 +22,14 @@
*/
class ArrayNode extends BaseNode implements PrototypeNodeInterface
{
protected $xmlRemappings;
protected $children;
protected $allowFalse;
protected $allowNewKeys;
protected $addIfNotSet;
protected $performDeepMerging;
protected $ignoreExtraKeys;
protected $normalizeKeys;

/**
* Constructor.
*
* @param string $name The Node's name
* @param NodeInterface $parent The node parent
*/
public function __construct($name, NodeInterface $parent = null)
{
parent::__construct($name, $parent);

$this->children = array();
$this->xmlRemappings = array();
$this->removeKeyAttribute = true;
$this->allowFalse = false;
$this->addIfNotSet = false;
$this->allowNewKeys = true;
$this->performDeepMerging = true;
$this->normalizeKeys = true;
}
protected $xmlRemappings = array();
protected $children = array();
protected $allowFalse = false;
protected $allowNewKeys = true;
protected $addIfNotSet = false;
protected $performDeepMerging = true;
protected $ignoreExtraKeys = false;
protected $normalizeKeys = true;

public function setNormalizeKeys($normalizeKeys)
{
Expand Down
15 changes: 5 additions & 10 deletions src/Symfony/Component/Config/Definition/BaseNode.php
Expand Up @@ -24,11 +24,11 @@ abstract class BaseNode implements NodeInterface
{
protected $name;
protected $parent;
protected $normalizationClosures;
protected $finalValidationClosures;
protected $allowOverwrite;
protected $required;
protected $equivalentValues;
protected $normalizationClosures = array();
protected $finalValidationClosures = array();
protected $allowOverwrite = true;
protected $required = false;
protected $equivalentValues = array();
protected $attributes = array();

/**
Expand All @@ -47,11 +47,6 @@ public function __construct($name, NodeInterface $parent = null)

$this->name = $name;
$this->parent = $parent;
$this->normalizationClosures = array();
$this->finalValidationClosures = array();
$this->allowOverwrite = true;
$this->required = false;
$this->equivalentValues = array();
}

public function setAttribute($key, $value)
Expand Down
Expand Up @@ -22,18 +22,18 @@
*/
class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinitionInterface
{
protected $performDeepMerging;
protected $ignoreExtraKeys;
protected $children;
protected $performDeepMerging = true;
protected $ignoreExtraKeys = false;
protected $children = array();
protected $prototype;
protected $atLeastOne;
protected $allowNewKeys;
protected $atLeastOne = false;
protected $allowNewKeys = true;
protected $key;
protected $removeKeyItem;
protected $addDefaults;
protected $addDefaultChildren;
protected $addDefaults = false;
protected $addDefaultChildren = false;
protected $nodeBuilder;
protected $normalizeKeys;
protected $normalizeKeys = true;

/**
* {@inheritDoc}
Expand All @@ -42,16 +42,8 @@ public function __construct($name, NodeParentInterface $parent = null)
{
parent::__construct($name, $parent);

$this->children = array();
$this->addDefaults = false;
$this->addDefaultChildren = false;
$this->allowNewKeys = true;
$this->atLeastOne = false;
$this->allowEmptyValue = true;
$this->performDeepMerging = true;
$this->nullEquivalent = array();
$this->trueEquivalent = array();
$this->normalizeKeys = true;
}

/**
Expand Down
Expand Up @@ -19,8 +19,8 @@
class MergeBuilder
{
protected $node;
public $allowFalse;
public $allowOverwrite;
public $allowFalse = false;
public $allowOverwrite = true;

/**
* Constructor
Expand All @@ -30,8 +30,6 @@ class MergeBuilder
public function __construct(NodeDefinition $node)
{
$this->node = $node;
$this->allowFalse = false;
$this->allowOverwrite = true;
}

/**
Expand Down
23 changes: 10 additions & 13 deletions src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php
Expand Up @@ -25,33 +25,30 @@ abstract class NodeDefinition implements NodeParentInterface
protected $normalization;
protected $validation;
protected $defaultValue;
protected $default;
protected $required;
protected $default = false;
protected $required = false;
protected $merge;
protected $allowEmptyValue;
protected $allowEmptyValue = true;
protected $nullEquivalent;
protected $trueEquivalent;
protected $falseEquivalent;
protected $trueEquivalent = true;
protected $falseEquivalent = false;

/**
* @var NodeParentInterface|NodeInterface
* @var NodeParentInterface|null
*/
protected $parent;
protected $attributes = array();

/**
* Constructor
*
* @param string $name The name of the node
* @param NodeParentInterface $parent The parent
* @param string $name The name of the node
* @param NodeParentInterface|null $parent The parent
*/
public function __construct($name, NodeParentInterface $parent = null)
{
$this->parent = $parent;
$this->name = $name;
$this->default = false;
$this->required = false;
$this->trueEquivalent = true;
$this->falseEquivalent = false;
}

/**
Expand Down Expand Up @@ -110,7 +107,7 @@ public function attribute($key, $value)
/**
* Returns the parent node.
*
* @return NodeParentInterface The builder of the parent node
* @return NodeParentInterface|null The builder of the parent node
*/
public function end()
{
Expand Down
Expand Up @@ -19,8 +19,8 @@
class NormalizationBuilder
{
protected $node;
public $before;
public $remappings;
public $before = array();
public $remappings = array();

/**
* Constructor
Expand All @@ -30,9 +30,6 @@ class NormalizationBuilder
public function __construct(NodeDefinition $node)
{
$this->node = $node;
$this->keys = false;
$this->remappings = array();
$this->before = array();
}

/**
Expand Down

0 comments on commit b74a887

Please sign in to comment.