Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for aligning constants, only touch lines with only assign…

…ment
  • Loading branch information...
commit 8ce69026e31f265d06cb58e50d3a9848562bfa5f 1 parent d945bb3
Willem Stuursma authored
Showing with 30 additions and 16 deletions.
  1. +30 −16 Transformer/AlignAssignments.php
46 Transformer/AlignAssignments.php
View
@@ -7,8 +7,9 @@ class Code_Transformer_AlignAssignments extends Code_Transformer_Abstract_File
const IDENTIFIER = "[A-z0-9]+[A-z0-9_]*";
const MODE_VARIABLES = 1;
- const MODE_PROPERTIES = 2;
- const MODE_ARRAY = 4;
+ const MODE_OBJECT = 2;
+ const MODE_ARRAY = 3;
+ const MODE_CONSTANTS = 4;
protected $mode;
@@ -18,24 +19,34 @@ public function processFile (array $lines)
{
$this->lines = $lines;
- for ($i = 0; $i < sizeof($this->lines); $i++) {
-
+ for ($i = 0; $i < sizeof($this->lines); $i++)
+ {
$line = $this->lines[$i];
- if (preg_match("!(\\s*)(\\$".self::IDENTIFIER.")\\s*=\\s*([^;]+;)!i", $line, $matches)) {
+ if (preg_match("!^(\\s*)(\\$".self::IDENTIFIER.")\\s*=\\s*([^;]+;)!i", $line, $matches))
+ {
$this->switchToMode(self::MODE_VARIABLES);
$this->addLine($i, $matches);
continue;
}
- if (preg_match("!(\\s*)(\\d+|\"[^\"]+\"|'[^']+')\\s*=>\\s*([^,]+,?)!i", $line, $matches)) {
+ if (preg_match("!^(\\s*)(\\d+|\"[^\"]+\"|'[^']+')\\s*=>\\s*([^,]+,?)!i", $line, $matches))
+ {
$this->switchToMode(self::MODE_ARRAY);
$this->addLine($i, $matches);
continue;
}
- if (preg_match("!(\\s*)(\\$".self::IDENTIFIER."->".self::IDENTIFIER.")\\s*=\\s*([^;]+;)!i", $line, $matches)) {
- $this->switchToMode(self::MODE_PROPERTIES);
+ if (preg_match("!^(\\s*)(\\$".self::IDENTIFIER."->".self::IDENTIFIER.")\\s*=\\s*([^;]+;)!i", $line, $matches))
+ {
+ $this->switchToMode(self::MODE_OBJECT);
+ $this->addLine($i, $matches);
+ continue;
+ }
+
+ if (preg_match("!^(\\s*)const\\s+(".self::IDENTIFIER.")\\s*=\\s*([^;]+;)!i", $line, $matches))
+ {
+ $this->switchToMode(self::MODE_CONSTANTS);
$this->addLine($i, $matches);
continue;
}
@@ -46,8 +57,8 @@ public function processFile (array $lines)
return $this->lines;
}
- protected function align() {
-
+ protected function align ()
+ {
// Get the longest variable name
$longest = 0;
foreach ($this->matched_lines as $i => $match) {
@@ -56,12 +67,15 @@ protected function align() {
switch ($this->mode) {
case self::MODE_VARIABLES:
- case self::MODE_PROPERTIES:
+ case self::MODE_OBJECT:
$template = "%s%-{$longest}s = %s";
break;
case self::MODE_ARRAY:
$template = "%s%-{$longest}s => %s";
break;
+ case self::MODE_CONSTANTS:
+ $template = "%sconst %-{$longest}s = %s";
+ break;
}
// Use the whitespace from the first line.
@@ -72,11 +86,10 @@ protected function align() {
// We are done aliging, reset everything.
$this->matched_lines = array();
- $this->mode = null;
+ $this->mode = null;
}
- protected function switchToMode($mode) {
-
+ protected function switchToMode ($mode) {
if ($mode === $this->mode) {
return;
}
@@ -85,13 +98,14 @@ protected function switchToMode($mode) {
$this->mode = $mode;
}
- if ($this->mode !== $mode) {
+ if ($this->mode !== $mode)
+ {
$this->align();
$this->mode = $mode;
}
}
- protected function addLine($line_num, array $matches) {
+ protected function addLine ($line_num, array $matches) {
$this->matched_lines[$line_num] = $matches;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.