Skip to content

Commit

Permalink
Merge pull request #1 from sjparkinson/hotfix/parameterIsDefaultValue…
Browse files Browse the repository at this point in the history
…Constant

Pass ...Expr_ConstFetch a ...Node_Name if isDefaultValueConstant is true
  • Loading branch information
Ocramius committed Sep 5, 2014
2 parents 906da54 + ef37f0c commit cd3c74c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
"CodeGenerationUtils\\": "src"
}
},
"autoload-dev": {
"psr-0": {
"CodeGenerationUtilsTests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
Expand Down
4 changes: 3 additions & 1 deletion src/CodeGenerationUtils/ReflectionBuilder/ClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ protected function buildParameter(ReflectionParameter $reflectionParameter)
&& $reflectionParameter->isDefaultValueConstant()
) {
$parameterBuilder->setDefault(
new PHPParser_Node_Expr_ConstFetch($reflectionParameter->getDefaultValueConstantName())
new PHPParser_Node_Expr_ConstFetch(
new PHPParser_Node_Name($reflectionParameter->getDefaultValueConstantName())
)
);
} else {
$parameterBuilder->setDefault($reflectionParameter->getDefaultValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace CodeGenerationUtilsTest\Visitor;

use CodeGenerationUtils\ReflectionBuilder\ClassBuilder;
use CodeGenerationUtilsTest\ReflectionBuilder\ClassWithDefaultValueIsConstantMethod;
use PHPParser_Node_Stmt_ClassMethod;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
Expand Down Expand Up @@ -68,4 +69,34 @@ function ($node) use ($currentMethod) {

$this->assertSame($currentMethod, $thisMethod->name);
}

/**
* Check the isDefaultValueConstant edge case.
*/
public function testBuildWithDefaultValueConstantParameter()
{
$classBuilder = new ClassBuilder();
$testClass = new ClassWithDefaultValueIsConstantMethod();
$ast = $classBuilder->fromReflection(new ReflectionClass($testClass));

/* @var $namespace \PHPParser_Node_Stmt_Namespace */
$namespace = $ast[0];
$class = $namespace->stmts[0];
$method = 'defaultValueIsConstant';

/* @var $methods PHPParser_Node_Stmt_ClassMethod[] */
$methods = array_filter(
$class->stmts,
function ($node) use ($method) {
return ($node instanceof PHPParser_Node_Stmt_ClassMethod && $node->name === $method);
}
);

$this->assertCount(1, $methods);

/* @var $thisMethod PHPParser_Node_Stmt_ClassMethod */
$thisMethod = reset($methods);

$this->assertSame($method, $thisMethod->name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace CodeGenerationUtilsTest\ReflectionBuilder;

/**
* Class to use for testing the ReflectionBuilder {@see \CodeGenerationUtilsTest\Visitor\ClassBuilderTest}
*
* @author Samuel Parkinson <sam@graze.com>
* @license MIT
*/
class ClassWithDefaultValueIsConstantMethod
{
/**
* Method to test the ReflectionProperty::getDefaultValueConstantName usage
* within {@see \CodeGenerationUtils\ReflectionBuilder\ClassBuilder}.
*
* @param string $test
* @return void
*/
public function defaultValueIsConstant($test = PHP_VERSION)
{
return;
}
}

0 comments on commit cd3c74c

Please sign in to comment.