minor #21537 Static code analysis with Php Inspections (EA Extended) (kalessil)

This PR was squashed before being merged into the 2.7 branch (closes #21537).

Discussion
----------

Static code analysis with Php Inspections (EA Extended)

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | --
| License       | MIT
| Doc PR        | --

- Unnecessary proxy methods and methods duplicating  parents ones removed;
- Type casings instead of PHP4 functions;
- Usage of copy instead of file combining file get/put contents;
- One time use variables cleaned up;
- Superfluous functions calls in loop termination conditions fixed;

Commits
-------

3feeca74d0 Static code analysis with Php Inspections (EA Extended)
This commit is contained in:
Fabien Potencier 2017-02-16 14:27:03 -08:00
commit a5c099c829
15 changed files with 19 additions and 54 deletions

View File

@ -138,7 +138,7 @@ class CodeExtension extends \Twig_Extension
$code = @highlight_file($file, true);
// remove main code/span tags
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
$content = preg_split('#<br />#', $code);
$content = explode('<br />', $code);
$lines = array();
for ($i = max($line - 3, 1), $max = min($line + 3, count($content)); $i <= $max; ++$i) {

View File

@ -62,9 +62,7 @@ class HttpKernelExtensionTest extends \PHPUnit_Framework_TestCase
$context->expects($this->any())->method('getCurrentRequest')->will($this->returnValue(Request::create('/')));
$renderer = new FragmentHandler(array($strategy), false, $context);
return $renderer;
return new FragmentHandler(array($strategy), false, $context);
}
protected function renderTemplate(FragmentHandler $renderer, $template = '{{ render("foo") }}')

View File

@ -132,7 +132,7 @@ class CodeHelper extends Helper
$code = @highlight_file($file, true);
// remove main code/span tags
$code = preg_replace('#^<code.*?>\s*<span.*?>(.*)</span>\s*</code>#s', '\\1', $code);
$content = preg_split('#<br />#', $code);
$content = explode('<br />', $code);
$lines = array();
for ($i = max($line - 3, 1), $max = min($line + 3, count($content)); $i <= $max; ++$i) {

View File

@ -46,4 +46,4 @@ EOF
<?php echo $view['translator']->trans('typecast', ['a' => (int) '123'], 'not_messages'); ?>
<?php echo $view['translator']->transChoice('msg1', 10 + 1, [], 'not_messages'); ?>
<?php echo $view['translator']->transChoice('msg2', intval(4.5), [], 'not_messages'); ?>
<?php echo $view['translator']->transChoice('msg2', ceil(4.5), [], 'not_messages'); ?>

View File

@ -77,17 +77,4 @@ class SimpleFormFactory extends FormLoginFactory
return $listenerId;
}
protected function createEntryPoint($container, $id, $config, $defaultEntryPoint)
{
$entryPointId = 'security.authentication.form_entry_point.'.$id;
$container
->setDefinition($entryPointId, new DefinitionDecorator('security.authentication.form_entry_point'))
->addArgument(new Reference('security.http_utils'))
->addArgument($config['login_path'])
->addArgument($config['use_forward'])
;
return $entryPointId;
}
}

View File

@ -36,7 +36,7 @@ class CrossCheckTest extends \PHPUnit_Framework_TestCase
$tmp = tempnam(sys_get_temp_dir(), 'sf');
file_put_contents($tmp, file_get_contents(self::$fixturesPath.'/'.$type.'/'.$fixture));
copy(self::$fixturesPath.'/'.$type.'/'.$fixture, $tmp);
$container1 = new ContainerBuilder();
$loader1 = new $loaderClass($container1, new FileLocator());

View File

@ -164,7 +164,7 @@ class BinaryFileResponse extends Response
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
$encoding = mb_detect_encoding($filename, null, true);
for ($i = 0; $i < mb_strlen($filename, $encoding); ++$i) {
for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) {
$char = mb_substr($filename, $i, 1, $encoding);
if ('%' === $char || ord($char) < 32 || ord($char) > 126) {

View File

@ -108,7 +108,7 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
if (phpversion('mongodb')) {
$that->assertInstanceOf('MongoDB\BSON\UTCDateTime', $criteria[$that->options['expiry_field']]['$gte']);
$that->assertGreaterThanOrEqual(round(intval((string) $criteria[$that->options['expiry_field']]['$gte']) / 1000), $testTimeout);
$that->assertGreaterThanOrEqual(round(((int) $criteria[$that->options['expiry_field']]['$gte']) / 1000), $testTimeout);
} else {
$that->assertInstanceOf('MongoDate', $criteria[$that->options['expiry_field']]['$gte']);
$that->assertGreaterThanOrEqual($criteria[$that->options['expiry_field']]['$gte']->sec, $testTimeout);
@ -167,7 +167,7 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
$that->assertEquals('bar', $data[$that->options['data_field']]->getData());
$that->assertInstanceOf('MongoDB\BSON\UTCDateTime', $data[$that->options['time_field']]);
$that->assertInstanceOf('MongoDB\BSON\UTCDateTime', $data[$that->options['expiry_field']]);
$that->assertGreaterThanOrEqual($expectedExpiry, round(intval((string) $data[$that->options['expiry_field']]) / 1000));
$that->assertGreaterThanOrEqual($expectedExpiry, round(((int) $data[$that->options['expiry_field']]) / 1000));
} else {
$that->assertEquals('bar', $data[$that->options['data_field']]->bin);
$that->assertInstanceOf('MongoDate', $data[$that->options['time_field']]);
@ -293,7 +293,7 @@ class MongoDbSessionHandlerTest extends \PHPUnit_Framework_TestCase
->will($this->returnCallback(function ($criteria) use ($that) {
if (phpversion('mongodb')) {
$that->assertInstanceOf('MongoDB\BSON\UTCDateTime', $criteria[$that->options['expiry_field']]['$lt']);
$that->assertGreaterThanOrEqual(time() - 1, round(intval((string) $criteria[$that->options['expiry_field']]['$lt']) / 1000));
$that->assertGreaterThanOrEqual(time() - 1, round(((int) $criteria[$that->options['expiry_field']]['$lt']) / 1000));
} else {
$that->assertInstanceOf('MongoDate', $criteria[$that->options['expiry_field']]['$lt']);
$that->assertGreaterThanOrEqual(time() - 1, $criteria[$that->options['expiry_field']]['$lt']->sec);

View File

@ -25,6 +25,9 @@ use Symfony\Component\HttpFoundation\Response;
* Client simulates a browser and makes requests to a Kernel object.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @method Request|null getRequest() A Request instance
* @method Response|null getResponse() A Response instance
*/
class Client extends BaseClient
{
@ -47,26 +50,6 @@ class Client extends BaseClient
parent::__construct($server, $history, $cookieJar);
}
/**
* {@inheritdoc}
*
* @return Request|null A Request instance
*/
public function getRequest()
{
return parent::getRequest();
}
/**
* {@inheritdoc}
*
* @return Response|null A Response instance
*/
public function getResponse()
{
return parent::getResponse();
}
/**
* Makes a request.
*

View File

@ -33,7 +33,7 @@ class ValueExporter
if (is_object($value)) {
if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
return sprintf('Object(%s) - %s', get_class($value), $value->format(\DateTime::ISO8601));
return sprintf('Object(%s) - %s', get_class($value), $value->format(\DateTime::ATOM));
}
return sprintf('Object(%s)', get_class($value));

View File

@ -41,7 +41,7 @@ class MemcacheProfilerStorage extends BaseMemcacheProfilerStorage
$port = $matches[3];
$memcache = new \Memcache();
$memcache->addserver($host, $port);
$memcache->addServer($host, $port);
$this->memcache = $memcache;
}

View File

@ -28,7 +28,7 @@ class ValueExporterTest extends \PHPUnit_Framework_TestCase
public function testDateTime()
{
$dateTime = new \DateTime('2014-06-10 07:35:40', new \DateTimeZone('UTC'));
$this->assertSame('Object(DateTime) - 2014-06-10T07:35:40+0000', $this->valueExporter->exportValue($dateTime));
$this->assertSame('Object(DateTime) - 2014-06-10T07:35:40+00:00', $this->valueExporter->exportValue($dateTime));
}
/**
@ -37,7 +37,7 @@ class ValueExporterTest extends \PHPUnit_Framework_TestCase
public function testDateTimeImmutable()
{
$dateTime = new \DateTimeImmutable('2014-06-10 07:35:40', new \DateTimeZone('UTC'));
$this->assertSame('Object(DateTimeImmutable) - 2014-06-10T07:35:40+0000', $this->valueExporter->exportValue($dateTime));
$this->assertSame('Object(DateTimeImmutable) - 2014-06-10T07:35:40+00:00', $this->valueExporter->exportValue($dateTime));
}
public function testIncompleteClass()

View File

@ -662,9 +662,8 @@ QUERY;
protected function getDeleteSecurityIdentityIdSql(SecurityIdentityInterface $sid)
{
$select = $this->getSelectSecurityIdentityIdSql($sid);
$delete = preg_replace('/^SELECT id FROM/', 'DELETE FROM', $select);
return $delete;
return preg_replace('/^SELECT id FROM/', 'DELETE FROM', $select);
}
/**

View File

@ -68,8 +68,6 @@ class DataCollectorTranslatorTest extends \PHPUnit_Framework_TestCase
$translator->addResource('array', array('bar' => 'bar (fr)'), 'fr');
$translator->addResource('array', array('bar_ru' => 'bar (ru)'), 'ru');
$collector = new DataCollectorTranslator($translator);
return $collector;
return new DataCollectorTranslator($translator);
}
}

View File

@ -610,7 +610,7 @@ class Parser
$previousLineIndented = false;
$previousLineBlank = false;
for ($i = 0; $i < count($blockLines); ++$i) {
for ($i = 0, $blockLinesCount = count($blockLines); $i < $blockLinesCount; ++$i) {
if ('' === $blockLines[$i]) {
$text .= "\n";
$previousLineIndented = false;