Skip to content

Commit

Permalink
Merge branch 'AlignPHPCode2' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
subins2000 committed Mar 1, 2017
2 parents 7983ede + ab66a34 commit 6485713
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 1 deletion.
97 changes: 96 additions & 1 deletion src/phpfmt.php
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ protected function leftMemoUsefulTokenIs( $token, $debug = false ) {
protected function leftToken( $ignoreList = [] ) {
$i = $this->leftTokenIdx( $ignoreList );

return $this->tkns[$i];
return isset( $this->tkns[$i] ) ? $this->tkns[$i] : null;
}

protected function leftTokenIdx( $ignoreList = [] ) {
Expand Down Expand Up @@ -2573,6 +2573,7 @@ abstract class BaseCodeFormatter {

'PSR2IndentWithSpace' => false,
'AlignPHPCode' => false,
'AlignPHPCode2' => false,
'AllmanStyleBraces' => false,
'NamespaceMergeWithOpenTag' => false,
'MergeNamespaceWithOpenTag' => false,
Expand Down Expand Up @@ -7043,6 +7044,100 @@ public function getExample() {
}
}

final class AlignPHPCode2 extends AdditionalPass {
const PLACEHOLDER_STRING = "\x2 CONSTANT_STRING_%d \x3";

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

return false;
}

public function format( $source ) {
$this->tkns = token_get_all( $source );
$this->code = '';
while ( list( $index, $token ) = each( $this->tkns ) ) {
list( $id, $text ) = $this->getToken( $token );
$this->ptr = $index;
switch ( $id ) {
case T_OPEN_TAG:
list( , $prevText ) = $this->getToken( $this->leftToken() );

$prevSpace = substr( strrchr( $prevText, $this->newLine ), 1 );
$skipPadLeft = false;
if ( rtrim( $prevSpace ) == $prevSpace ) {
$skipPadLeft = true;
}
$prevSpace = preg_replace( '/[^\s\t]/', ' ', $prevSpace );

$placeholders = [];
$strings = [];
$stack = $text;
while ( list( $index, $token ) = each( $this->tkns ) ) {
list( $id, $text ) = $this->getToken( $token );
$this->ptr = $index;

if ( T_CONSTANT_ENCAPSED_STRING == $id || T_ENCAPSED_AND_WHITESPACE == $id ) {
$strings[] = $text;
$text = sprintf( self::PLACEHOLDER_STRING, $this->ptr );
$placeholders[] = $text;
}
$stack .= $text;

if ( T_CLOSE_TAG == $id ) {
break;
}
}

$tmp = explode( $this->newLine, $stack );
$lastLine = sizeof( $tmp ) - 2;
foreach ( $tmp as $idx => $line ) {
$before = $prevSpace;
if ( '' === trim( $line ) ) {
continue;
}

if ( $skipPadLeft ) {
$before = '';
$skipPadLeft = false;
}

$tmp[$idx] = $before . $line;
}

$stack = implode( $this->newLine, $tmp );
$stack = str_replace( $placeholders, $strings, $stack );

$this->code = rtrim( $this->code, " \t" );
$this->appendCode( $stack );
break;

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

return $this->code;
}

public function getDescription() {
return 'Align PHP code within opening and closing php block.';
}

public function getExample() {
return <<<'EOT'
<div>
<?php
echo $a;
?>
</div>
EOT;
}
}

final class AlignTypehint extends AdditionalPass {
const ALIGNABLE_TYPEHINT = "\x2 TYPEHINT%d \x3";

Expand Down
63 changes: 63 additions & 0 deletions testing/tests/TestAlignPHPCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

class TestAlignPHPCode extends PHPUnit_Framework_TestCase {

/**
* @var string Code to test
*/
private $code = <<<CODE
<?php
require 'foo.php';
?>
<div>
<?php
echo 'foo';
?>
</div>
CODE;

public function testEnabled() {
$output = executeCommand(
array(
'--passes' => 'AlignPHPCode',
),
$this->code
);

$expected_result = <<<CODE
<?php
require 'foo.php';
?>
<div>
<?php
echo 'foo';
?>
</div>
CODE;

$this->assertContains( $expected_result, $output );
}

public function testDisabled() {
$output = executeCommand(
array(
'--exclude' => 'AlignPHPCode',
),
$this->code
);

$expected_result = <<<CODE
<?php
require 'foo.php';
?>
<div>
<?php
echo 'foo';
?>
</div>
CODE;

$this->assertContains( $expected_result, $output );
}

}
72 changes: 72 additions & 0 deletions testing/tests/TestAlignPHPCode2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

class TestAlignPHPCode2 extends PHPUnit_Framework_TestCase {

/**
* @var string Code to test
*/
private $code = <<<CODE
<?php
require 'foo.php';
?>
<div>
<?php
echo 'foo';
if ('foo' === 'bar'){
}
?>
</div>
CODE;

public function testEnabled() {
$output = executeCommand(
array(
'--passes' => 'AlignPHPCode2',
),
$this->code
);

$expected_result = <<<CODE
<?php
require 'foo.php';
?>
<div>
<?php
echo 'foo';
if ('foo' === 'bar') {
}
?>
</div>
CODE;

$this->assertContains( $expected_result, $output );
}

public function testDisabled() {
$output = executeCommand(
array(
'--exclude' => 'AlignPHPCode2',
),
$this->code
);

$expected_result = <<<CODE
<?php
require 'foo.php';
?>
<div>
<?php
echo 'foo';
if ('foo' === 'bar') {
}
?>
</div>
CODE;

$this->assertContains( $expected_result, $output );
}

}
3 changes: 3 additions & 0 deletions testing/tests/TestRemoveIncludeParentheses.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class TestRemoveIncludeParentheses extends PHPUnit_Framework_TestCase {
private $code = <<<CODE
<?php
include ('a.php');
require ('a.php');
?>
CODE;

Expand All @@ -20,6 +21,7 @@ public function testEnabled() {
);

$this->assertContains( "include 'a.php';", $output );
$this->assertContains( "require 'a.php';", $output );
}

public function testDisabled() {
Expand All @@ -31,6 +33,7 @@ public function testDisabled() {
);

$this->assertContains( "include ('a.php');", $output );
$this->assertContains( "require ('a.php');", $output );
}

}

0 comments on commit 6485713

Please sign in to comment.