Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove comments from mock pass #1030

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ public function apply($code, MockConfiguration $config)

$className = $config->getShortName();

$code = str_replace(
$parts = preg_split("/{/", $code, 2);

$parts[0] = str_replace(
'namespace Mockery;',
$namespace ? 'namespace ' . $namespace . ';' : '',
$code
$parts[0]
);

$code = str_replace(
$parts[0] = str_replace(
'class Mock',
'class ' . $className,
$code
$parts[0]
);

return $code;
return $parts[0] . "{" . $parts[1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ public function apply($code, MockConfiguration $config)
\Mockery::declareClass($className);
}

$code = str_replace(
$parts = preg_split("/{/", $code, 2);

$parts[0] = str_replace(
"implements MockInterface",
"extends \\" . $className . " implements MockInterface",
$code
$parts[0]
);

return $code;
return $parts[0] . "{" . $parts[1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public function apply($code, MockConfiguration $config)
return $code . ", \\" . ltrim($i->getName(), "\\");
}, "");

$code = str_replace(
$parts = preg_split("/{/", $code, 2);

$parts[0] = str_replace(
"implements MockInterface",
"implements MockInterface" . $interfaces,
$code
$parts[0]
);

return $code;
return $parts[0] . "{" . $parts[1];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Mockery
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://github.com/padraic/mockery/blob/master/LICENSE
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to padraic@php.net so we can send you a copy immediately.
*
* @category Mockery
* @package Mockery
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
*/

namespace Mockery\Generator\StringManipulation\Pass;

use Mockery\Generator\MockConfiguration;

class RemoveCommentsFromMockPass implements Pass
{
private $strippedCode;

private $tokens;

/**
* @param string $code
* @param MockConfiguration $config
* @return string
*/
public function apply($code, MockConfiguration $config)
{
if ($this->strippedCode !== null) {
return $this->strippedCode;
}

$this->strippedCode = '';

$this->removeComments($code);

$this->removeEmptyLines();

return $this->strippedCode;
}

/**
* @return string
*/
private function removeEmptyLines()
{
$this->strippedCode = preg_replace('#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#', "\n", $this->strippedCode);
}

/**
* @param string $code
*/
private function removeComments($code)
{
$tokens = token_get_all($code);

foreach ($tokens as $token) {
if (is_array($token)) {
if ($token[0] == T_DOC_COMMENT || $token[0] == T_COMMENT) {
continue;
}

$token = $token[1];
}
$this->strippedCode .= $token;
}
}
}
2 changes: 2 additions & 0 deletions library/Mockery/Generator/StringManipulationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Mockery\Generator\StringManipulation\Pass\MethodDefinitionPass;
use Mockery\Generator\StringManipulation\Pass\Pass;
use Mockery\Generator\StringManipulation\Pass\RemoveBuiltinMethodsThatAreFinalPass;
use Mockery\Generator\StringManipulation\Pass\RemoveCommentsFromMockPass;
use Mockery\Generator\StringManipulation\Pass\RemoveDestructorPass;
use Mockery\Generator\StringManipulation\Pass\RemoveUnserializeForInternalSerializableClassesPass;
use Mockery\Generator\StringManipulation\Pass\TraitPass;
Expand All @@ -47,6 +48,7 @@ class StringManipulationGenerator implements Generator
public static function withDefaultPasses()
{
return new static([
new RemoveCommentsFromMockPass(),
new CallTypeHintPass(),
new MagicMethodTypeHintsPass(),
new ClassPass(),
Expand Down