ZF-9501: "zf create action" causes malformed PHP when non-empty action methods already exist

Description

I have controller which has this code in the start of the class:

public function preDispatch()
{
    $acl = Zend_Registry::get('acl');

            if(!$acl->isAllowed(Zend_Registry::get('role'), 'admin', $this->getRequest()->action)) {
                if(!Zend_Auth::getInstance()->hasIdentity()) {
                    $this->_forward('noauth', 'error');
                } else {
                    $this->_forward('noacl', 'error');
                }
            }
}

when I run zf create action for this file, the resulting PHP file comes out broken - this method has one closing brace } missing.

Comments

Also just noticed - it does not preserve the formatting of the document completely. While all the lines are ok, indentation is often messed up.

This also happens to me. Together with the tab messup, it really becomes a pain to use zend tool.

All the controller gets wrecked every single time a new action is added wich really starts to get into you once the controller starts to get big.

Just upgraded to 1.10.6 to make sure, and it still happens.

It removes any closing bracket that's right before the method's one. i.e.:

function yadayadaAction(){

/* lots of code */

if (something){ ... } }

The if's closing bracket gets removed.

I can confirm this on 1.10.3.

Youssef Eldakar Bibliotheca Alexandrina

This problem is related to Zend_Reflection_Method::getBody().

Greetings Ramon

Is related to with issue ZF-9018.

I've noticed that whenever I have a try{}catch() the code breaks up and the comments get an additional empty line, heres code:


class IndexController extends Zend_Controller_Action {
    public function init() {
    }

    /**
     * a test action
     */
    public function funkyAction() {
        $this->view->title = "COOOL";
    }

    public function indexAction() {
        try {
            $this->view->title = "Testing Zend_Tool bug";
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        }
    }

    /**
     * a test action
     */
    public function unfunkyAction() {
        $this->view->title = "UNCOOOL";
    }
}

class IndexController extends Zend_Controller_Action
{
    public function init()
    {
    }

    /**
     * a test action
     *
     */
    public function funkyAction()
    {
        $this->view->title = "COOOL";
    }

    public function indexAction()
    {
        try {
            $this->view->title = "Testing Zend_Tool bug";
        } catch (Exception $exc) {
            echo $exc->getTraceAsString();
        
    }

    /**
     * a test action
     *
     */
    public function unfunkyAction()
    {
        $this->view->title = "UNCOOOL";
    }

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

Note missing } in the try and the extra comment line: /** * a test action * */

This was done with the ZF 1.11.2

Same problem here. ZF version: 1.11.0 If I add an action with a command like this:

zf create action

there are 2 kind of problems:

  1. The controller source is bad indented
  2. The last closing bracket inside EVERY action method is removed, leaving you with corrupted PHP code

Note: the error #2 is for every structure control enclosed with curly braces, so this problem is valid if you have a while, an if, a try/catch, and so on... The closing bracket is stripped away.

I'm trying to teach ZF inside our company but If the basic Tools like Zend_Tool don't function properly I can't tell people to switch to them. This bug is almost an year old: hope to see it fixed soon. I'll vote for it.

Not that this will stop the indentation problem, but it only removes a curly bracket if it is the last thing before the closing bracket of the function. This means you can stop Zend Tool from breaking the code by adding a commented line before the closing bracket:

public function myAction() { if($whatever) { } // Comment added here stops the curly bracket getting removed from the line above. }

Still not ideal, but it does at least mean you can add actions using zf.

I'm having the same problem, but only in the following situation.


    public function removeAction()
    {
        if (NULL !== ($itemId = $this->_getParam('id', null))) {
            $cartMapper = new Application_Model_CartMapper();
            if ($cartMapper->removeItem($itemId)) {
                $this->_redirect('/cart');
            }
        }
    }



    public function removeAction()
    {
        if (NULL !== ($itemId = $this->_getParam('id', null))) {
            $cartMapper = new Application_Model_CartMapper();
            if ($cartMapper->removeItem($itemId)) {
                $this->_redirect('/cart');
            }
        
    }

This issue stems from problems with {{Zend_Reflection_Method::getBody}}, as outlined in ZF-9018. An interim fix has been suggested there.

Fixed in ZF-9501