Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[hotfix/ZF-11513] Support for constants and properties with the same …

…name in a class
  • Loading branch information...
commit 43bbee43b5e9fcddbab1d16f12206b19eaf2121a 1 parent f901d80
@adamlundrigan authored
View
102 library/Zend/CodeGenerator/Php/PhpClass.php
@@ -80,6 +80,11 @@ class PhpClass extends AbstractPhp
protected $_properties = null;
/**
+ * @var array Array of constants
+ */
+ protected $_constants = null;
+
+ /**
* @var array Array of methods
*/
protected $_methods = null;
@@ -328,6 +333,21 @@ public function setProperties(array $properties)
}
/**
+ * setConstants()
+ *
+ * @param array $constants
+ * @return \Zend\CodeGenerator\Php\Class
+ */
+ public function setConstants(Array $constants)
+ {
+ foreach ($constants as $const) {
+ $this->setConstant($const);
+ }
+
+ return $this;
+ }
+
+ /**
* setProperty()
*
* @param array|\Zend\CodeGenerator\Php\PhpProperty $property
@@ -342,6 +362,9 @@ public function setProperty($property)
}
$propertyName = $property->getName();
+ if ($property->isConst()) {
+ return $this->setConstant($property);
+ }
if (isset($this->_properties[$propertyName])) {
throw new Exception\InvalidArgumentException('A property by name ' . $propertyName . ' already exists in this class.');
}
@@ -351,6 +374,34 @@ public function setProperty($property)
}
/**
+ * setConstant()
+ *
+ * @param array|\Zend\CodeGenerator\Php\PhpProperty $const
+ * @return Zend\CodeGenerator\Php\PhpClass
+ */
+ public function setConstant($const)
+ {
+ if (is_array($const)) {
+ $const = new PhpProperty($const);
+ $constName = $const->getName();
+ } elseif ($const instanceof PhpProperty) {
+ $constName = $const->getName();
+ } else {
+ throw new Exception\InvalidArgumentException('setConstant() expects either an array of property options or an instance of Zend_CodeGenerator_Php_Property');
+ }
+
+ if (!$const->isConst()) {
+ throw new Exception\InvalidArgumentException('setProperty() expects argument to define a constant');
+ }
+ if (isset($this->_constants[$constName])) {
+ throw new Exception\InvalidArgumentException('A constant by name ' . $constName . ' already exists in this class.');
+ }
+
+ $this->_constants[$constName] = $const;
+ return $this;
+ }
+
+ /**
* getProperties()
*
* @return array
@@ -361,6 +412,16 @@ public function getProperties()
}
/**
+ * getConstants()
+ *
+ * @return array
+ */
+ public function getConstants()
+ {
+ return $this->_constants;
+ }
+
+ /**
* getProperty()
*
* @param string $propertyName
@@ -377,6 +438,22 @@ public function getProperty($propertyName)
}
/**
+ * getConstant()
+ *
+ * @param string $constName
+ * @return \Zend\CodeGenerator\Php\PhpProperty
+ */
+ public function getConstant($constName)
+ {
+ foreach ($this->_constants as $const) {
+ if ($const->getName() == $constName) {
+ return $const;
+ }
+ }
+ return false;
+ }
+
+ /**
* hasProperty()
*
* @param string $propertyName
@@ -402,6 +479,17 @@ public function setMethods(array $methods)
}
/**
+ * hasConstant()
+ *
+ * @param string $constName
+ * @return bool
+ */
+ public function hasConstant($constName)
+ {
+ return isset($this->_constants[$constName]);
+ }
+
+ /**
* setMethod()
*
* @param array|\Zend\CodeGenerator\Php\PhpMethod $method
@@ -478,6 +566,12 @@ public function isSourceDirty()
}
}
+ foreach ($this->_constants as $constant) {
+ if ($constant->isSourceDirty()) {
+ return true;
+ }
+ }
+
foreach ($this->_methods as $method) {
if ($method->isSourceDirty()) {
return true;
@@ -530,6 +624,13 @@ public function generate()
$output .= self::LINE_FEED . '{' . self::LINE_FEED . self::LINE_FEED;
+ $constants = $this->getConstants();
+ if (!empty($constants)) {
+ foreach ($constants as $const) {
+ $output .= $const->generate() . self::LINE_FEED . self::LINE_FEED;
+ }
+ }
+
$properties = $this->getProperties();
if (!empty($properties)) {
foreach ($properties as $property) {
@@ -556,6 +657,7 @@ public function generate()
protected function _init()
{
$this->_properties = new PhpMember\MemberContainer(PhpMember\MemberContainer::TYPE_PROPERTY);
+ $this->_constants = new PhpMember\MemberContainer(PhpMember\MemberContainer::TYPE_PROPERTY);
$this->_methods = new PhpMember\MemberContainer(PhpMember\MemberContainer::TYPE_METHOD);
}
View
30 tests/Zend/CodeGenerator/Php/PhpClassTest.php
@@ -368,4 +368,34 @@ public function testPassingANamespacedClassnameShouldGenerateAClassnameWithoutIt
$received = $codeGenClass->generate();
$this->assertContains('class FunClass', $received, $received);
}
+
+ /**
+ * @group ZF-11513
+ */
+ public function testAllowsClassConstantToHaveSameNameAsClassProperty()
+ {
+ $const = new Php\PhpProperty();
+ $const->setName('name')->setDefaultValue('constant')->setConst(true);
+
+ $property = new Php\PhpProperty();
+ $property->setName('name')->setDefaultValue('property');
+
+ $codeGenClass = new Php\PhpClass();
+ $codeGenClass->setName('My_Class')->setProperties(array($const, $property));
+
+ $expected = <<<CODE
+class My_Class
+{
+
+ const name = 'constant';
+
+ public \$name = 'property';
+
+
+}
+
+CODE;
+ $this->assertEquals( $expected, $codeGenClass->generate() );
+ }
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.