Issues

ZF-10398: Adding actions to controller generate sintax errors in controller

Description

I have added many actions to a controller . Whenever I have added another action to the controller the code in the actions moves to the right

AND

in the actions where the functions ends with 2 breckets like
public funtion addAction { if (3>4){ } } one brecket is deleted and generates a sintax error .

This happens every time i add an action ! I have tested on several versions.

Example 1 - before adding action

class TestController extends Zend_Controller_Action {

public function init()
{
    require_once (APPLICATION_PATH.'\models\masina.class.php');
    require_once (APPLICATION_PATH.'\models\angajat.class.php');
    require_once (APPLICATION_PATH.'\models\Forms.php');
    require_once (APPLICATION_PATH.'\models\Test1.php');
    require_once (APPLICATION_PATH.'\models\statie.class.php');
    require_once (APPLICATION_PATH.'\models\ruta.class.php');
    require_once (APPLICATION_PATH.'\models\traseu.class.php');
}

public function indexAction()
{
    $x = new Application_Model_Test1();
                                    $trasee = $x -> getTraseeActive();
}

public function listruteAction()
{
    $x = new Application_Model_Test1();
                                                            $trasee = $x -> getTraseeActive();

                                                            foreach ($trasee as $t )
                                                                    {

                                                                     $rute [$t['id_ruta'] ] = "ok" ; 

                                                                    }

                                                                    echo "RUTE:<BR>";
                                                                    foreach ($rute as $r => $x )
                                                                       {
                                                                       echo "<a href='listtrasee?ruta=$r' >  $r  </a> <br> ";
        }
}

public function listtraseeAction()
{
    $ruta  =  $this->_request->getQuery("ruta") ;
                                                            echo "Ruta selectata este :  "  . $ruta  . "<BR>" ;

                                                            $x = new Application_Model_Test1();
                                                            $trasee = $x -> getTraseeActive();

                                                            echo "Trasee pentru aceasta ruta : <BR> <br> " ;
                                                            foreach ($trasee as $t )
                                                                {

                                                                if ($t['id_ruta']==$ruta )
                                                                 {              
                                                                    echo "&nbsp ID traseu: <a href='listmasini?id_traseu={$t['id_traseu']}' >" . $t['id_traseu'] ." </a> <BR>";
                                                                    echo "&nbsp Nume complet:" . $t['nume_traseu'] ."<BR>";
                                                                    echo "&nbsp Valabilitate:" . $t['valabilitate'] ."<BR>";
                                                                    echo "-------------------------------------<br>";
                                                                 }

                                                                }

                                                            // action body
}

}


Example 2 - after executing zf create action newaction test(missing action , even more text moving to the right)

class TestController extends Zend_Controller_Action {

public function init()
{
    require_once (APPLICATION_PATH.'\models\masina.class.php');
            require_once (APPLICATION_PATH.'\models\angajat.class.php');
            require_once (APPLICATION_PATH.'\models\Forms.php');
            require_once (APPLICATION_PATH.'\models\Test1.php');
            require_once (APPLICATION_PATH.'\models\statie.class.php');
            require_once (APPLICATION_PATH.'\models\ruta.class.php');
            require_once (APPLICATION_PATH.'\models\traseu.class.php');
}

public function indexAction()
{
    $x = new Application_Model_Test1();
                                            $trasee = $x -> getTraseeActive();
}

public function listruteAction()
{
    $x = new Application_Model_Test1();
                                                                    $trasee = $x -> getTraseeActive();

                                                                    foreach ($trasee as $t )
                                                                            {

                                                                             $rute [$t['id_ruta'] ] = "ok" ; 

                                                                            }

                                                                            echo "RUTE:<BR>";
                                                                            foreach ($rute as $r => $x )
                                                                               {
                                                                               echo "<a href='listtrasee?ruta=$r' >  $r  </a> <br> ";
}

public function listtraseeAction()
{
    $ruta  =  $this->_request->getQuery("ruta") ;
                                                                    echo "Ruta selectata este :  "  . $ruta  . "<BR>" ;

                                                                    $x = new Application_Model_Test1();
                                                                    $trasee = $x -> getTraseeActive();

                                                                    echo "Trasee pentru aceasta ruta : <BR> <br> " ;
                                                                    foreach ($trasee as $t )
                                                                        {

                                                                        if ($t['id_ruta']==$ruta )
                                                                         {              
                                                                            echo "&nbsp ID traseu: <a href='listmasini?id_traseu={$t['id_traseu']}' >" . $t['id_traseu'] ." </a> <BR>";
                                                                            echo "&nbsp Nume complet:" . $t['nume_traseu'] ."<BR>";
                                                                            echo "&nbsp Valabilitate:" . $t['valabilitate'] ."<BR>";
                                                                            echo "-------------------------------------<br>";
                                                                         }

                                                                        }

                                                                    // action body
}

}

Comments

I was able to replicate at least part of this problem.

If I start with a simple controller: <?php

class IndexController extends Zend_Controller_Action { public function indexAction() { if (2>3) {} } }

And run the following command:

$ zf.sh create action two Creating an action named two inside controller at /var/www/tmp/application/controllers/IndexController.php Updating project profile '/var/www/tmp/.zfproject.xml' Creating a view script for the two action method at /var/www/tmp/application/views/scripts/index/two.phtml Updating project profile '/var/www/tmp/.zfproject.xml'

My controller now looks like this:

<?php

class IndexController extends Zend_Controller_Action {

public function indexAction()
{
    if (2>3) {
}

public function twoAction()
{
    // action body
}

}

Note the parse error in indexAction.

Found cause is Zend_Reflection_Method::getBody(). It does not properly strip the method's opening or closing tags if code exists on the same line as the opening brace, or the code ends in a curly brace.

Attached patch including three new unit tests, two of which fail: - testGetBodyReturnsCorrectContentWithCodeOnOpeningBraceLine - testGetBodyReturnsCorrectContentWithClosingBrace

I will work on a fix.

Attached patch that resolves issues with modified code causing parse errors in the following scenarios: - having method with code that exists on same line as method's opening brace - having method with code that ends in curly brace - having method with code on same line as method's closing brace

Caragata Cosmin-Mihail, can you apply this patch and see if it resolves the issue?

If not, please provide example controller (preferrably smaller than first example) and the command you ran so I can replicate problem?

I have updated Zend/Reflection/Method.php :: getBody to this like you said : public function getBody() { $lines = array_slice( file($this->getDeclaringClass()->getFileName(), FILE_IGNORE_NEW_LINES), $this->getStartLine(), ($this->getEndLine() - $this->getStartLine()), true );

    $firstLine = array_shift($lines);

    if (trim($firstLine) !== '{') {
        // if line has method's opening curly brace and more code, remove first curly brace (leaving spaces intact)
        if (substr(ltrim($firstLine), 0, 1) == '{') {
            //$firstLine = preg_replace('/{/', '', $firstLine, 1);
            $firstLine = substr(ltrim($firstLine), 1);
        }
         array_unshift($lines, $firstLine);
     }

     $lastLine = array_pop($lines);

     if (trim($lastLine) !== '}') {
        // if line has code and the method's closing curly brace, remove trailing spaces and last curly brace
        if (substr(rtrim($lastLine), -1, 1) == '}') {
            $lastLine = substr(rtrim($lastLine), 0, -1);
        }
         array_push($lines, $lastLine);
     }

    // just in case we had code on the bracket lines
    return rtrim(ltrim(implode("\n", $lines), '{'), '}');
    return implode("\n", $lines);

}

}

*Created a new project *Created a controller named test1 *Added 2 actions: *zf create action action1 test1 *zf create action action2 test1 (I use windows xp btw) *Edited the first action like this

<?php class Test1Controller extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { // action body } public function action1Action() { if (1>2) {} } public function action2Action() { // action body } } ?>

*Added 1 more action action3 *zf craete action action 3 test1

<?php class Test1Controller extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { // action body } public function action1Action() { if (1>2) { } public function action2Action() { // action body } public function action3Action() { // action body } } ?>

*notice the missing brecket in action1 - problem persists even with the update

*I have re-edited action1 to public function action1Action() { if (1>2) { } // }

  • notice the "//" characters added
  • after running: zf crate action action4 test1 <?php class Test1Controller extends Zend_Controller_Action { public function init() { /* Initialize action controller here */ } public function indexAction() { // action body } public function action1Action() { if (1>2) { } // } public function action2Action() { // action body } public function action3Action() { // action body } public function action4Action() { // action body } }?>

*notice that the brecket is not missing anymore *in fact , if any characters are in between the 2 breckets the brecket won't be deleted - this happens without the update too *ALTOUGH the ident problem still persists

*My Conclusion: *With or without the update the "missing brecket" problem persists *With or without the update the ident problem persists *Can aviod the missing brecket if ANY characters inserted between the breckets but the ident problem persists(added neutral code like"//" or ";" but any code is ok) *The position of the 2 breckets doesn't matter \ *I have tested this too: public function action1Action() { if (1>2) {} }

*BTW I'm using Xp sp3 , Xampp Lite server , zend 1.10.8

Hope this helps you find the solution .Thank you for the reply.

Duplicate of ZF-9502