10000 [3.0] Removed all $that variables by saro0h · Pull Request #12841 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[3.0] Removed all $that variables #12841

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

Merged
merged 1 commit into from
Dec 21, 2014
Merged
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
Removed all $that variables
  • Loading branch information
saro0h committed Dec 21, 2014
commit 7b33d1a06074bce5c24a758eb53832cc1eb2b9c3
5 changes: 2 additions & 3 deletions src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$choiceListCache = & $this->choiceListCache;
$registry = $this->registry;
$propertyAccessor = $this->propertyAccessor;
$type = $this;

$loader = function (Options $options) use ($type) {
$loader = function (Options $options) {
if (null !== $options['query_builder']) {
return $type->getLoader($options['em'], $options['query_builder'], $options['class']);
return $this->getLoader($options['em'], $options['query_builder'], $options['class']);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ public function getProxyFactoryCode(Definition $definition, $id)

return <<<EOF
if (\$lazyLoad) {
\$container = \$this;

$instantiation new $proxyClass(
function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) use (\$container) {
\$wrappedInstance = \$container->$methodName(false);
function (&\$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface \$proxy) {
\$wrappedInstance = \$this->$methodName(false);

\$proxy->setProxyInitializer(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ public function __construct()
public function getFooService($lazyLoad = true)
{
if ($lazyLoad) {
$container = $this;

return $this->services['foo'] = new stdClass_c1d194250ee2e2b7d2eab8b8212368a8(
function (& $wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {
$wrappedInstance = $container->getFooService(false);
function (& $wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) {
$wrappedInstance = $this->getFooService(false);

$proxy->setProxyInitializer(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ class ProjectServiceContainer extends Container
public function getFooService($lazyLoad = true)
{
if ($lazyLoad) {
$container = $this;

return $this->services['foo'] = new stdClass_%s(
function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {
$wrappedInstance = $container->getFooService(false);
function (&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) {
$wrappedInstance = $this->getFooService(false);

$proxy->setProxyInitializer(null);

Expand All @@ -24,4 +23,4 @@ class ProjectServiceContainer extends Container
}

class stdClass_%s extends \stdClass implements \ProxyManager\Proxy\VirtualProxyInterface
{%a}%A
{%a}%A
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public function testGetProxyFactoryCode()
$code = $this->dumper->getProxyFactoryCode($definition, 'foo');

$this->assertStringMatchesFormat(
'%wif ($lazyLoad) {%w$container = $this;%wreturn $this->services[\'foo\'] = new '
'%wif ($lazyLoad) {%wreturn $this->services[\'foo\'] = new '
.'SymfonyBridgeProxyManagerTestsLazyProxyPhpDumperProxyDumperTest_%s(%wfunction '
.'(&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) use ($container) {'
.'%w$wrappedInstance = $container->getFooService(false);%w$proxy->setProxyInitializer(null);'
.'(&$wrappedInstance, \ProxyManager\Proxy\LazyLoadingInterface $proxy) {'
.'%w$wrappedInstance = $this->getFooService(false);%w$proxy->setProxyInitializer(null);'
.'%wreturn true;%w}%w);%w}%w',
$code
);
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Bridge/Twig/Extension/CodeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,8 @@ public function getFileLink($file, $line)

public function formatFileFromText($text)
{
$that = $this;

return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) use ($that) {
return 'in '.$that->formatFile($match[2], $match[3]);
return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {
return 'in '.$this->formatFile($match[2], $match[3]);
}, $text);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,8 @@ public function getFileLink($file, $line)

public function formatFileFromText($text)
{
$that = $this;

return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) use ($that) {
return 'in '.$that->formatFile($match[2], $match[3]);
return preg_replace_callback('/in ("|&quot;)?(.+?)\1(?: +(?:on|at))? +line (\d+)/s', function ($match) {
return 'in '.$this->formatFile($match[2], $match[3]);
}, $text);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function parseEscapeSequences($str, $quote)
);
}

public static function parseCallback($matches)
private static function parseCallback($matches)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change the visibility of a method who are not flagged as @internal? (even if it seem to be used only in closure)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's meant to be merged into 3.0 where BC breaks are allowed.

{
$str = $matches[1];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class PreviewErrorControllerTest extends TestCase
{
public function testForwardRequestToConfiguredController()
{
$self = $this;

$request = Request::create('whatever');
$response = new Response("");
$code = 123;
Expand All @@ -33,15 +31,14 @@ public function testForwardRequestToConfiguredController()
->expects($this->once())
->method('handle')
->with(
$this->callback(function (Request $request) use ($self, $logicalControllerName, $code) {
$this->callback(function (Request $request) use ($logicalControllerName, $code) {

$self->assertEquals($logicalControllerName, $request->attributes->get('_controller'));
$this->assertEquals($logicalControllerName, $request->attributes->get('_controller'));

$exception = $request->attributes->get('exception');
$self->assertInstanceOf('Symfony\Component\Debug\Exception\FlattenException', $exception);
$self->assertEquals($code, $exception->getStatusCode());

$self->assertFalse($request->attributes->get('showException'));
$this->assertInstanceOf('Symfony\Component\HttpKernel\Exception\FlattenException', $exception);
$this->assertEquals($code, $exception->getStatusCode());
$this->assertFalse($request->attributes->get('showException'));

return true;
}),
Expand Down
12 changes: 4 additions & 8 deletions src/Symfony/Component/Console/Helper/DialogHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,8 @@ public function askHiddenResponse(OutputInterface $output, $question, $fallback
*/
public function askAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $default = null, array $autocomplete = null)
{
$that = $this;

$interviewer = function () use ($output, $question, $default, $autocomplete, $that) {
return $that->ask($output, $question, $default, $autocomplete);
$interviewer = function () use ($output, $question, $default, $autocomplete) {
return $this->ask($output, $question, $default, $autocomplete);
};

return $this->validateAttempts($interviewer, $output, $validator, $attempts);
Expand All @@ -376,10 +374,8 @@ public function askAndValidate(OutputInterface $output, $question, $validator, $
*/
public function askHiddenResponseAndValidate(OutputInterface $output, $question, $validator, $attempts = false, $fallback = true)
{
$that = $this;

$interviewer = function () use ($output, $question, $fallback, $that) {
return $that->askHiddenResponse($output, $question, $fallback);
$interviewer = function () use ($output, $question, $fallback) {
return $this->askHiddenResponse($output, $question, $fallback);
};

return $this->validateAttempts($interviewer, $output, $validator, $attempts);
Expand Down
13 changes: 3 additions & 10 deletions src/Symfony/Component/Console/Helper/ProcessHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,16 @@ public function wrapCallback(OutputInterface $output, Process $process, $callbac
{
$formatter = $this->getHelperSet()->get('debug_formatter');

$that = $this;

return function ($type, $buffer) use ($output, $process, $callback, $formatter, $that) {
$output->write($formatter->progress(spl_object_hash($process), $that->escapeString($buffer), Process::ERR === $type));
return function ($type, $buffer) use ($output, $process, $callback, $formatter) {
$output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type));

if (null !== $callback) {
call_user_func($callback, $type, $buffer);
}
};
}

/**
* This method is public for PHP 5.3 compatibility, it should be private.
*
* @internal
*/
public function escapeString($str)
private function escapeString($str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about inlining this one? Any other ones that could be inlined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is used 4 times in this class. Let me check in the other 39 files

{
return str_replace('<', '\\<', $str);
}
Expand Down
20 changes: 7 additions & 13 deletions src/Symfony/Component/Console/Helper/ProgressBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,9 @@ public function getProgress()
/**
* Gets the progress bar step width.
*
* @internal This method is public for PHP 5.3 compatibility, it should not be used.
*
* @return int The progress bar step width
* @return int The progress bar step width
*/
public function getStepWidth()
private function getStepWidth()
{
return $this->stepWidth;
}
Expand Down Expand Up @@ -432,15 +430,11 @@ public function display()
return;
}

// these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped.
$self = $this;
$output = $this->output;
$messages = $this->messages;
$this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) use ($self, $output, $messages) {
if ($formatter = $self::getPlaceholderFormatterDefinition($matches[1])) {
$text = call_user_func($formatter, $self, $output);
} elseif (isset($messages[$matches[1]])) {
$text = $messages[$matches[1]];
$this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) {
if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
$text = call_user_func($formatter, $this, $this->output);
} elseif (isset($this->messages[$matches[1]])) {
$text = $this->messages[$matches[1]];
} else {
return $matches[0];
}
Expand Down
6 changes: 2 additions & 4 deletions src/Symfony/Component/Console/Helper/QuestionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public function ask(InputInterface $input, OutputInterface $output, Question $qu
return $this->doAsk($output, $question);
}

$that = $this;

$interviewer = function () use ($output, $question, $that) {
return $that->doAsk($output, $question);
$interviewer = function () use ($output, $question) {
return $this->doAsk($output, $question);
};

return $this->validateAttempts($interviewer, $output, $question);
Expand Down
7 changes: 3 additions & 4 deletions src/Symfony/Component/Console/Input/ArgvInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,14 +333,13 @@ public function getParameterOption($values, $default = false)
*/
public function __toString()
{
$self = $this;
$tokens = array_map(function ($token) use ($self) {
$tokens = array_map(function ($token) {
if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) {
return $match[1].$self->escapeToken($match[2]);
return $match[1].$this->escapeToken($match[2]);
}

if ($token && $token[0] !== '-') {
return $self->escapeToken($token);
return $this->escapeToken($token);
}

return $token;
Expand Down
47 changes: 19 additions & 28 deletions src/Symfony/Component/Debug/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,30 @@ public function handle(\Exception $exception)

$caughtLength = $this->caughtLength = 0;

ob_start(array($this, 'catchOutput'));
ob_start(function($buffer) {
$this->caughtBuffer = $buffer;

return '';
});


$this->failSafeHandle($exception);
while (null === $this->caughtBuffer && ob_end_flush()) {
// Empty loop, everything is in the condition
}
if (isset($this->caughtBuffer[0])) {
ob_start(array($this, 'cleanOutput'));
ob_start(function($buffer) {
if ($this->caughtLength) {
// use substr_replace() instead of substr() for mbstring overloading resistance
$cleanBuffer = substr_replace($buffer, '', 0, $this->caughtLength);
if (isset($cleanBuffer[0])) {
$buffer = $cleanBuffer;
}
}

return $buffer;
});

echo $this->caughtBuffer;
$caughtLength = ob_get_length();
}
Expand Down Expand Up @@ -426,30 +443,4 @@ protected static function utf8Htmlize($str)

return htmlspecialchars($str, ENT_QUOTES | (PHP_VERSION_ID >= 50400 ? ENT_SUBSTITUTE : 0), 'UTF-8');
}

/**
* @internal
*/
public function catchOutput($buffer)
{
$this->caughtBuffer = $buffer;

return '';
}

/**
* @internal
*/
public function cleanOutput($buffer)
{
if ($this->caughtLength) {
// use substr_replace() instead of substr() for mbstring overloading resistance
$cleanBuffer = substr_replace($buffer, '', 0, $this->caughtLength);
if (isset($cleanBuffer[0])) {
$buffer = $cleanBuffer;
}
}

return $buffer;
}
}
Loading
0