NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Script to automate writing of Set/Get functions OOP PHPDoc


Joined: 01 Feb 2007
Posts: 8
Reply with quote
Integration script (see comments on the code)

Code:

<?php
/**
* CreateSetGet
* @author Diego Alberto Bernal info@lasumo.com
* @desc
*
* First of all, sorry for my bad english.
* I use to make all class attributes private, and I use interfases for assign its values (adjusting to OOP paradigm)
*
* If I declare x number of attributes I had to write x getAttributeName, setAttributeName functions.
*
* To solve this and save some time, I wrote this Script (to use on PhpED) that reads lines with any of these compositions (with or without // and ; ):
*
* ambit type $variable;
* ambit type $variable; // comentary comentary comentary
* ambit $variable;
* ambit $variable; // comentary comentary comentary
*
* So, for example, if I write on phpED the following
*
* * * * * * * * * * * * * * * * * * * * * * *
* private integer $myVar; // Variable that handles nothing
* * * * * * * * * * * * * * * * * * * * * * *
*
* .. select the text and apply the script, it will transform it to:
*
* * * * * * * * * * * * * * * * * * * * * * *
* /**
* * @var integer Variable that handles nothing
* *\/
* private integer $myVar;
* /**
* * @param integer Set: Variable that handles nothing
* *\/
* public function setMyVar($myVar) { $this->myVar = $myVar; }
* /**
* * @return integer Get: Variable that handles nothing
* *\/
* public function getMyVar() { return $this->myVar; }
*
* * * * * * * * * * * * * * * * * * * * * * *
*
* Best Regards from Bogotá, Colombia
* Diego Alberto Bernal
* Web Developer
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

* ahora en español....
*
* Transforma lineas con alguna de las siguientes formas (con o sin //) :
*
* ambito tipo $variable; // comentarios comentarios comentarios (ojo que esto no es compatible con php)
* ambito tipo $variable; (ojo que esto no es compatible con php)
* ambito $variable; // comentarios comentarios comentarios
* ambito $variable;
* $variable; // comentarios comentarios comentarios
* $variable;
*
*  y la convirte en una declaración completa comentada de la siguiente forma:
* (si no se especifica tipo este valdrá "mixed")
* (si no se especifica ambito este valdrá "")
* (si no se especifican comentarios estos valdrán "")
*
* /**
* * @var tipo comentarios comentarios comentarios
* *\/
* ambito $variable;
* /**
* * $param tipo Establece: comentarios comentarios comentarios
* *\/
* public function setVariable($variable) { $this->variable = $variable; }
* /**
* * @return tipo Devuelve: comentarios comentarios comentarios
* *\/
* public function getVariable($variable) { return $this->variable; }
*
* @todo Admitir lineas con la asignaciónes
*
*/

class CreateSetGet {
   private $text;
   private $finalText;
   
   private $leftMargin;
   private $preSetDescription;
   private $preGetDescription;
   
   private $ambit;
   private $type;
   private $variable;
   private $comments;
   
   public function getFinalText() {
      return $this->finalText;   
   }
   
   public function __construct($theText = NULL) {
      if($theText != NULL) { $this->text = $theText; } else { $this->text = explode("\n", rtrim(file_get_contents('php://stdin'))); }
      if(!is_array($this->text)) { $temp = $this->text; $this->text = array($this->text); }
      $this->preSetDescription = "Establece: ";
      $this->preGetDescription = "Obtiene: ";
      $this->finalText = "";
      foreach($this->text as $line) {
         if($this->parseLine($line)) {
            $this->finalText .= $this->constructBlock() . "\n";
         } else {
            $this->finalText .= '// La linea está mal formada || '.$line."\n";
         }
      }
   }
   private function constructBlock() {
      $returnText = "";
      $returnText .= $this->leftMargin . '/**'."\n";
      $returnText .= $this->leftMargin . '* @var '.$this->type.' '.$this->comments."\n";
      $returnText .= $this->leftMargin . '*/'."\n";
      $returnText .= $this->leftMargin . $this->ambit .' $'.$this->variable.';'."\n";
      $returnText .= $this->leftMargin . '/**'."\n";
      $returnText .= $this->leftMargin . '* @param '.$this->type.' '.$this->preSetDescription.$this->comments."\n";
      $returnText .= $this->leftMargin . '*/'."\n";
      $returnText .= $this->leftMargin . 'public function set'.ucfirst($this->variable).'($'.$this->variable.') { $this->'.$this->variable.' = $'.$this->variable.'; }'."\n";
      $returnText .= $this->leftMargin . '/**'."\n";
      $returnText .= $this->leftMargin . '* @return '.$this->type.' '.$this->preGetDescription.$this->comments."\n";
      $returnText .= $this->leftMargin . '*/'."\n";
      $returnText .= $this->leftMargin . 'public function get'.ucfirst($this->variable).'() { return $this->'.$this->variable.'; }';
      return $returnText;
   }
   
   private function parseLine($line) {
      $this->setDefaults();
      $lineError = false;
      $pos = strpos($line,'$');
      if($pos === false) { // Si no encontró el simbolo pesos en la linea haga
         $lineError = true;
      } else { // si no (encontró el simbolo pesos en la linea) haga...
      
         // Proceso el primer segmento
         if($pos == 0) { // Si el simbolo pesos es lo primero de la linea haga..
            $secondSegment = substr($line,1);
         } else { // si no (simbolo pesos no es lo primero de la linea) haga...
            $this->leftMargin = "";
            $firstSegment = substr($line,0,$pos);
            $secondSegment = substr($line,$pos+1);
            for($i=0;$i<strlen($firstSegment);$i++) {  // Creo la margen si la hay
               if(($firstSegment[$i] == " ") || ($firstSegment[$i] == "\t")) {
                  $this->leftMargin .= $firstSegment[$i];
               } else {
                  break;   
               }
            }
            $firstSegment = trim($firstSegment); // Quito margen si la hay
            if(strlen($firstSegment) > 0) { // Si el primer segmento no es solo margen haga...
               $firstSegment = explode(" ",$firstSegment);
               $sizeofFirstSegment = sizeof($firstSegment);
               if($sizeofFirstSegment > 2) {
                  $lineError = true;   
               } else {
                  if($sizeofFirstSegment == 1) {
                     $this->ambit = $firstSegment[0];
                  } elseif($sizeofFirstSegment == 2) {
                     $this->ambit = $firstSegment[0];
                     $this->type = $firstSegment[1];                               
                  } else {
                     $lineError = true;   
                  }                               
               }
            }                   
         }
         
         // Proceso el segundo segmento
         $secondSegment = str_replace('/'," ",$secondSegment);
         $secondSegment = str_replace(';'," ",$secondSegment);
         $secondSegment = trim($secondSegment);
         $secondSegment = explode(" ",$secondSegment);
         if(sizeof($secondSegment) == 0) {
            $lineError;   
         } elseif(sizeof($secondSegment) == 1) {
            $this->variable = $secondSegment[0];
         } else {
            $this->variable = $secondSegment[0];
            $this->comments = "";
            for($i=1;$i<sizeof($secondSegment);$i++) {
               $this->comments .= $secondSegment[$i] . " ";
            }
            $this->comments = trim($this->comments);                   
         }        
      }
      if($lineError) {
         return false;   
      } else {
         return true;   
      }
   }   
   
   private function setDefaults() {
      $this->ambit = "public";
      $this->type = "mixed";
      $this->variable = "noSeEspecificoElNombreDeLaVariable";
      $this->comments = "";
      
      $this->leftMargin = "";
   }
   
}


$createSetGet = new CreateSetGet();
echo utf8_decode($createSetGet->getFinalText());
          
?>
View user's profileFind all posts by lasumoSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 7835
Reply with quote
Thanks!

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 06 Apr 2007
Posts: 48
Location: Gdansk, POLAND
Reply with quote
Hmmm, how to "install" that script in phpEd?
View user's profileFind all posts by NatanielSend private messageVisit poster's website
Site Admin

Joined: 13 Jul 2003
Posts: 7835
Reply with quote
howto-use-a-php-script-for-my-editing-t753.html

_________________
The PHP IDE team
View user's profileFind all posts by dmitriSend private messageVisit poster's website


Joined: 06 Apr 2007
Posts: 48
Location: Gdansk, POLAND
Reply with quote
dmitri wrote:
1. Create a customization entry (a menu item)


I don't get it Surprised How?
View user's profileFind all posts by NatanielSend private messageVisit poster's website


Joined: 08 Feb 2007
Posts: 67
Reply with quote
I had a few minutes of confusion too when I first started integrating scripts Wink

Step 1: Tools > Settings > Tools > Integration > Add Menu

Then click "Edit" on the menu item you just created and continue from Step 2 in the link above

[edit]Oh! Also don't forget to use @PHP5@ instead of @PHP@ in step 3, as this is a PHP5 script[/edit]
View user's profileFind all posts by nothsaSend private message


Joined: 28 Nov 2009
Posts: 2
Reply with quote
Can you give me an example or a video to demo how and when I'll use it?
Thanks sir
View user's profileFind all posts by undefinedSend private message
Script to automate writing of Set/Get functions OOP PHPDoc
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic