Skip to content

Commit

Permalink
Alignment of type declarations in functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ucirello committed Jan 16, 2015
1 parent d43de59 commit f24b9cc
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 2 deletions.
132 changes: 132 additions & 0 deletions Additionals/AlignTypehint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
final class AlignTypehint extends AdditionalPass {
const ALIGNABLE_TYPEHINT = "\x2 TYPEHINT%d \x3";

public function candidate($source, $foundTokens) {
if (isset($foundTokens[T_FUNCTION])) {
return true;
}
return false;
}

public function format($source) {
$this->tkns = token_get_all($source);
$this->code = '';
$contextCounter = 0;
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
switch ($id) {
case T_FUNCTION:
$this->appendCode($text);
$this->printUntil(ST_PARENTHESES_OPEN);
do {
list($id, $text) = $this->printAndStopAt([T_VARIABLE, ST_PARENTHESES_OPEN, ST_PARENTHESES_CLOSE]);
if (ST_PARENTHESES_OPEN == $id) {
$this->appendCode($text);
$this->printBlock(ST_PARENTHESES_OPEN, ST_PARENTHESES_CLOSE);
continue;
}
if (ST_PARENTHESES_CLOSE == $id) {
$this->appendCode($text);
break;
}
$this->appendCode(sprintf(self::ALIGNABLE_TYPEHINT, $contextCounter) . $text);
} while (true);
$contextCounter++;
break;

default:
$this->appendCode($text);
break;
}
}

for ($j = 0; $j <= $contextCounter; ++$j) {
$placeholder = sprintf(self::ALIGNABLE_TYPEHINT, $j);
if (false === strpos($this->code, $placeholder)) {
continue;
}
if (1 === substr_count($this->code, $placeholder)) {
$this->code = str_replace($placeholder, '', $this->code);
continue;
}

$lines = explode($this->newLine, $this->code);
$linesWithComment = [];
$blockCount = 0;

foreach ($lines as $idx => $line) {
if (false !== strpos($line, $placeholder)) {
$linesWithComment[$blockCount][] = $idx;
} else {
++$blockCount;
$linesWithComment[$blockCount] = [];
}
}

$i = 0;
foreach ($linesWithComment as $group) {
++$i;
$farthest = 0;
foreach ($group as $idx) {
$farthest = max($farthest, strpos($lines[$idx], $placeholder));
}
foreach ($group as $idx) {
$line = $lines[$idx];
$current = strpos($line, $placeholder);
$delta = abs($farthest - $current);
if ($delta > 0) {
$line = str_replace($placeholder, str_repeat(' ', $delta) . $placeholder, $line);
$lines[$idx] = $line;
}
}
}

$this->code = str_replace($placeholder, '', implode($this->newLine, $lines));
}

return $this->code;
}

/**
* @codeCoverageIgnore
*/
public function getDescription() {
return 'Vertically align "//" comments.';
}

/**
* @codeCoverageIgnore
*/
public function getExample() {
return <<<'EOT'
<?php
//From:
function a(
TypeA $a,
TypeBB $bb,
TypeCCC $ccc = array(),
TypeDDDD $dddd,
TypeEEEEE $eeeee
){
noop();
}
//To:
function a(
TypeA $a,
TypeBB $bb,
TypeCCC $ccc = array(),
TypeDDDD $dddd,
TypeEEEEE $eeeee
){
noop();
}
?>
EOT;
}
}
Binary file modified fmt.phar
Binary file not shown.
2 changes: 1 addition & 1 deletion fmt.phar.sha1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
990bcbb98e835760bb5f6fd4493c5e8de427e66e
8b22a9f5fef27c99dd9e4a730b08f5510ba33f18
131 changes: 131 additions & 0 deletions fmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4458,6 +4458,137 @@ public function getExample() {
$bb = 22;
$ccc = 333;
?>
EOT;
}
};
final class AlignTypehint extends AdditionalPass {
const ALIGNABLE_TYPEHINT = "\x2 TYPEHINT%d \x3";

public function candidate($source, $foundTokens) {
if (isset($foundTokens[T_FUNCTION])) {
return true;
}
return false;
}

public function format($source) {
$this->tkns = token_get_all($source);
$this->code = '';
$contextCounter = 0;
while (list($index, $token) = each($this->tkns)) {
list($id, $text) = $this->getToken($token);
$this->ptr = $index;
switch ($id) {
case T_FUNCTION:
$this->appendCode($text);
$this->printUntil(ST_PARENTHESES_OPEN);
do {
list($id, $text) = $this->printAndStopAt([T_VARIABLE, ST_PARENTHESES_OPEN, ST_PARENTHESES_CLOSE]);
if (ST_PARENTHESES_OPEN == $id) {
$this->appendCode($text);
$this->printBlock(ST_PARENTHESES_OPEN, ST_PARENTHESES_CLOSE);
continue;
}
if (ST_PARENTHESES_CLOSE == $id) {
$this->appendCode($text);
break;
}
$this->appendCode(sprintf(self::ALIGNABLE_TYPEHINT, $contextCounter) . $text);
} while (true);
$contextCounter++;
break;

default:
$this->appendCode($text);
break;
}
}

for ($j = 0; $j <= $contextCounter; ++$j) {
$placeholder = sprintf(self::ALIGNABLE_TYPEHINT, $j);
if (false === strpos($this->code, $placeholder)) {
continue;
}
if (1 === substr_count($this->code, $placeholder)) {
$this->code = str_replace($placeholder, '', $this->code);
continue;
}

$lines = explode($this->newLine, $this->code);
$linesWithComment = [];
$blockCount = 0;

foreach ($lines as $idx => $line) {
if (false !== strpos($line, $placeholder)) {
$linesWithComment[$blockCount][] = $idx;
} else {
++$blockCount;
$linesWithComment[$blockCount] = [];
}
}

$i = 0;
foreach ($linesWithComment as $group) {
++$i;
$farthest = 0;
foreach ($group as $idx) {
$farthest = max($farthest, strpos($lines[$idx], $placeholder));
}
foreach ($group as $idx) {
$line = $lines[$idx];
$current = strpos($line, $placeholder);
$delta = abs($farthest - $current);
if ($delta > 0) {
$line = str_replace($placeholder, str_repeat(' ', $delta) . $placeholder, $line);
$lines[$idx] = $line;
}
}
}

$this->code = str_replace($placeholder, '', implode($this->newLine, $lines));
}

return $this->code;
}

/**
* @codeCoverageIgnore
*/
public function getDescription() {
return 'Vertically align "//" comments.';
}

/**
* @codeCoverageIgnore
*/
public function getExample() {
return <<<'EOT'
<?php
//From:
function a(
TypeA $a,
TypeBB $bb,
TypeCCC $ccc = array(),
TypeDDDD $dddd,
TypeEEEEE $eeeee
){
noop();
}
//To:
function a(
TypeA $a,
TypeBB $bb,
TypeCCC $ccc = array(),
TypeDDDD $dddd,
TypeEEEEE $eeeee
){
noop();
}
?>
EOT;
}
Expand Down
1 change: 1 addition & 0 deletions fmt.src.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
require 'Additionals/AlignDoubleArrow.php';
require 'Additionals/AlignDoubleSlashComments.php';
require 'Additionals/AlignEquals.php';
require 'Additionals/AlignTypehint.php';
require 'Additionals/AutoPreincrement.php';
require 'Additionals/CakePHPStyle.php';
require 'Additionals/EncapsulateNamespaces.php';
Expand Down
Loading

0 comments on commit f24b9cc

Please sign in to comment.