Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/PLATFORM-9057_remove_inline_comm…
Browse files Browse the repository at this point in the history
…ents_for_better_line_number'
  • Loading branch information
Abrosimov Kirill committed Oct 31, 2017
2 parents 4d12e05 + d6d9231 commit d66aaa0
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# SoftMocks v1 Change Log

## v1.3.2

There are next changes:

- Line numbering in rewritten code improved
- Only multiline /**/ comments are present in rewritten file

## v1.3.0

There are next changes:
Expand Down
41 changes: 40 additions & 1 deletion src/Badoo/SoftMocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,43 @@ protected function pStmts(array $nodes, $indent = true)
}
}

protected function pComments(array $comments)
{
$formattedComments = [];

foreach ($comments as $comment) {
$reformattedText = $comment->getReformattedText();
if (mb_orig_strpos($reformattedText, '/**') === 0) {
$formattedComments[] = $reformattedText;
}
}

return !empty($formattedComments) ? implode("\n", $formattedComments) : "";
}

protected function pCommaSeparatedMultiline(array $nodes, $trailingComma)
{
$result = '';
$lastIdx = count($nodes) - 1;
foreach ($nodes as $idx => $node) {
if ($node !== null) {
$comments = $node->getAttribute('comments', array());
if ($comments) {
$result .= $this->pComments($comments);
}

$result .= "\n" . $this->p($node);
} else {
$result .= "\n";
}
if ($trailingComma || $idx !== $lastIdx) {
$result .= ',';
}
}

return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
}

public function prettyPrintFile(array $stmts)
{
$this->cur_ln = 1;
Expand Down Expand Up @@ -121,7 +158,9 @@ protected function pExpr_Array(\PhpParser\Node\Expr\Array_ $node)
}
$prefix = "";
if (!$this->areNodesSingleLine($node->items)) {
$prefix = str_repeat("\n", $node->getAttribute('endLine') - ($node->getLine() + substr_count($res, "\n")));
if ($node->getAttribute('endLine') - ($node->getLine() + substr_count($res, "\n")) >= 0) {
$prefix = str_repeat("\n", $node->getAttribute('endLine') - ($node->getLine() + substr_count($res, "\n")));
}
}
$res .= $prefix . $suffix;
return $res;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Badoo/SoftMocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public function testRewrite($filename)
$this->assertNotFalse($result, "Rewrite failed");

//file_put_contents(__DIR__ . '/fixtures/expected/' . $filename, file_get_contents($result));
$this->assertEquals(file_get_contents(__DIR__ . '/fixtures/expected/' . $filename), file_get_contents($result));
$this->assertEquals(trim(file_get_contents(__DIR__ . '/fixtures/expected/' . $filename)), file_get_contents($result));
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/Badoo/fixtures/expected/php5.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
\Badoo\SoftMocks::callFunction(__NAMESPACE__, 'error_reporting', array(\Badoo\SoftMocks::getConst(__NAMESPACE__, 'E_ALL')));
\Badoo\SoftMocks::callFunction(__NAMESPACE__, 'ini_set', array('display_errors', true));

// Conditions

if (!empty($_SERVER['HTTP_ORIG_DOMAIN'])) {
$_SERVER['SERVER_NAME'] = $_SERVER['HTTP_ORIG_DOMAIN'];

// Various strings

$header = 'HTTP/1.1 301 Moved Permanently';
$redirect_address = 'https://badoo.com';

Expand Down Expand Up @@ -54,8 +54,8 @@

function replaceSomething($string){

// Comment
/* Comment */


return \Badoo\SoftMocks::callFunction(__NAMESPACE__, 'str_replace', array('something', 'somebody', $string));}


Expand All @@ -77,4 +77,4 @@ public function method($string){if (false !== ($__softmocksvariableforcode = \Ba

protected static function methodSelf($string){if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */}

return \Badoo\SoftMocks::callFunction(__NAMESPACE__, 'replaceSomething', array(&$string));}}
return \Badoo\SoftMocks::callFunction(__NAMESPACE__, 'replaceSomething', array(&$string));}}
6 changes: 3 additions & 3 deletions tests/unit/Badoo/fixtures/expected/php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/
function replaceSomething($string) : string{

// Comment
/* Comment */


return \Badoo\SoftMocks::callFunction(__NAMESPACE__, 'str_replace', array('something', 'somebody', $string));}


Expand Down Expand Up @@ -56,4 +56,4 @@ public function methodParamNullableReturn(string $string) : ?string{if (false !=

public function methodNullableParamNullableReturn(?string $string) : ?string{if (false !== ($__softmocksvariableforcode = \Badoo\SoftMocks::isMocked(SomeClass::class, static::class, __FUNCTION__))) {$mm_func_args = func_get_args();$params = array($string);return eval($__softmocksvariableforcode);/** @codeCoverageIgnore */}

return $string;}}
return $string;}}

0 comments on commit d66aaa0

Please sign in to comment.