ZF-11472: "zf create action" can cause malformed PHP when other action methods already exist

Description

When controller contains some non-empty methods, e.g.:


public function editAction()
{
    if ($obj === null) {
        ...
    } else {
        ...
    }
}

then command:

{{zf create action lorem}}

eats closing braces. editAction() method will be converted to


public function editAction()
{
    if ($obj === null) {
        ...
    } else {
        ...
    }

(without ending brace).

This is annoying if you have large number of action methods.

Comments

Add code tags

Thanks.

Reproduced successfully against trunk 1. {{zf create controller Foo}} 2. {{zf create action testOne Foo}} 3. Edit FooController, change testOneAction to:


public function testOneAction()
{
    if ( true )
    {
        echo "True!";
    }
    else
    {
        echo "False!";
    }
}
  1. {{zf create action testTwo Foo}}

The controller will now look like this:


public function testOneAction()
{
    if ( true )
    {
        echo "True!";
    }
    else
    {
        echo "False!";

}

public function testTwoAction()
{
    // action body
}

Reproducing test case:


Index: tests/Zend/CodeGenerator/Php/FileTest.php
===================================================================
--- tests/Zend/CodeGenerator/Php/FileTest.php   (revision 24511)
+++ tests/Zend/CodeGenerator/Php/FileTest.php   (working copy)
@@ -378,4 +378,47 @@

         $this->assertEquals($target, $codeGenFile->generate());
     }
+
+    /**
+     * @group ZF-11472
+     */
+    public function testClassMethodDoesNotGetTruncated()
+    {
+        $codeGenFile = Zend_CodeGenerator_Php_File::fromReflectedFileName(dirname(__FILE__) . "/_files/Zf11472TestController.php");
+        $codeGenFileClasses = $codeGenFile->getClasses();
+        $classCodeGen = array_shift($codeGenFileClasses);
+        $classCodeGen->setMethod(array('name' => 'testAction', 'body' => '        // action body here'));
+
+        $expected = <<assertEquals($expected,$classCodeGen->generate());
+    }
 }
\ No newline at end of file
Index: tests/Zend/CodeGenerator/Php/_files/Zf11472TestController.php
===================================================================
--- tests/Zend/CodeGenerator/Php/_files/Zf11472TestController.php       (revision 0)
+++ tests/Zend/CodeGenerator/Php/_files/Zf11472TestController.php       (revision 0)
@@ -0,0 +1,23 @@
+<?php
+
+class IndexController extends Zend_Controller_Action
+{
+
+    public function init()
+    {
+        /* Initialize action controller here */
+    }
+
+    public function indexAction()
+    {
+        if ( true )
+        {
+            echo "True";
+        }
+        else
+        {
+            echo "False";
+        }
+    }
+}
+

Property changes on: tests/Zend/CodeGenerator/Php/_files/Zf11472TestController.php
___________________________________________________________________
Added: svn:keywords
   + Id

Result:


++ phpunit --verbose --group ZF-11472 AllTests
PHPUnit 3.5.14 by Sebastian Bergmann.

F

Time: 8 seconds, Memory: 424.75Mb

There was 1 failure:

1) Zend_CodeGenerator_Php_FileTest::testClassMethodDoesNotGetTruncated
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
             echo "False";
-        }
+
     }

     public function testAction()
     {
         // action body here
     }


 }

/usr/local/apache2/htdocs/lib/zfdev/trunk/tests/Zend/CodeGenerator/Php/FileTest.php:422

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

Working on a fix now.

This is actually an issue with {{Zend_Reflection_Method}} which was reported in ZF-9018

Duplicate of ZF-9501