minor #14890 [2.3] Static Code Analysis for Components (kalessil)

This PR was squashed before being merged into the 2.3 branch (closes #14890).

Discussion
----------

[2.3] Static Code Analysis for Components

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

Static Code Analysis with Php Inspections (EA Extended):
    - not optimal regular expressions usage
    - strlen miss-use
    - not optimal conditional statements in Process and Filesystem
    - unsafe uniquid usage

PS: re-creating to no spam history log with reverts

Commits
-------

4a4fea7 [2.3] Static Code Analysis for Components
This commit is contained in:
Tobias Schultze 2015-06-15 15:02:12 +02:00
commit d4b669bb63
14 changed files with 19 additions and 19 deletions

View File

@ -1161,7 +1161,7 @@ class Application
$lines[] = str_pad($line, $width); $lines[] = str_pad($line, $width);
$line = $char; $line = $char;
} }
if (strlen($line)) { if ('' !== $line) {
$lines[] = count($lines) ? str_pad($line, $width) : $line; $lines[] = count($lines) ? str_pad($line, $width) : $line;
} }

View File

@ -33,7 +33,7 @@ class OutputFormatter implements OutputFormatterInterface
*/ */
public static function escape($text) public static function escape($text)
{ {
return preg_replace('/([^\\\\]?)</is', '$1\\<', $text); return preg_replace('/([^\\\\]?)</', '$1\\<', $text);
} }
/** /**
@ -146,7 +146,7 @@ class OutputFormatter implements OutputFormatterInterface
$offset = 0; $offset = 0;
$output = ''; $output = '';
$tagRegex = '[a-z][a-z0-9_=;-]*'; $tagRegex = '[a-z][a-z0-9_=;-]*';
preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#isx", $message, $matches, PREG_OFFSET_CAPTURE); preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
foreach ($matches[0] as $i => $match) { foreach ($matches[0] as $i => $match) {
$pos = $match[1]; $pos = $match[1];
$text = $match[0]; $text = $match[0];

View File

@ -207,7 +207,7 @@ EOF;
} else { } else {
$this->output->write($this->getPrompt()); $this->output->write($this->getPrompt());
$line = fgets(STDIN, 1024); $line = fgets(STDIN, 1024);
$line = (!$line && strlen($line) == 0) ? false : rtrim($line); $line = (false === $line || '' === $line) ? false : rtrim($line);
} }
return $line; return $line;

View File

@ -271,7 +271,7 @@ class GraphvizDumper extends Dumper
*/ */
private function dotize($id) private function dotize($id)
{ {
return strtolower(preg_replace('/[^\w]/i', '_', $id)); return strtolower(preg_replace('/\W/i', '_', $id));
} }
/** /**

View File

@ -675,7 +675,7 @@ class FilesystemTest extends \PHPUnit_Framework_TestCase
*/ */
public function testRenameThrowsExceptionOnError() public function testRenameThrowsExceptionOnError()
{ {
$file = $this->workspace.DIRECTORY_SEPARATOR.uniqid(); $file = $this->workspace.DIRECTORY_SEPARATOR.uniqid('fs_test_', true);
$newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file'; $newPath = $this->workspace.DIRECTORY_SEPARATOR.'new_file';
$this->filesystem->rename($file, $newPath); $this->filesystem->rename($file, $newPath);

View File

@ -272,7 +272,7 @@ class FinderTest extends Iterator\RealIteratorTestCase
public function testFilter($adapter) public function testFilter($adapter)
{ {
$finder = $this->buildFinder($adapter); $finder = $this->buildFinder($adapter);
$this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return preg_match('/test/', $f) > 0; })); $this->assertSame($finder, $finder->filter(function (\SplFileInfo $f) { return false !== strpos($f, 'test'); }));
$this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator()); $this->assertIterator($this->toAbsolute(array('test.php', 'test.py')), $finder->in(self::$tmpDir)->getIterator());
} }

View File

@ -39,7 +39,7 @@ class CustomFilterIteratorTest extends IteratorTestCase
{ {
return array( return array(
array(array(function (\SplFileInfo $fileinfo) { return false; }), array()), array(array(function (\SplFileInfo $fileinfo) { return false; }), array()),
array(array(function (\SplFileInfo $fileinfo) { return preg_match('/^test/', $fileinfo) > 0; }), array('test.php', 'test.py')), array(array(function (\SplFileInfo $fileinfo) { return 0 === strpos($fileinfo, 'test'); }), array('test.php', 'test.py')),
array(array('is_dir'), array()), array(array('is_dir'), array()),
); );
} }

View File

@ -51,7 +51,7 @@ class MockSplFileInfo extends \SplFileInfo
public function isFile() public function isFile()
{ {
if (null === $this->type) { if (null === $this->type) {
return preg_match('/file/', $this->getFilename()); return false !== strpos($this->getFilename(), 'file');
}; };
return self::TYPE_FILE === $this->type; return self::TYPE_FILE === $this->type;
@ -60,7 +60,7 @@ class MockSplFileInfo extends \SplFileInfo
public function isDir() public function isDir()
{ {
if (null === $this->type) { if (null === $this->type) {
return preg_match('/directory/', $this->getFilename()); return false !== strpos($this->getFilename(), 'directory');
} }
return self::TYPE_DIRECTORY === $this->type; return self::TYPE_DIRECTORY === $this->type;

View File

@ -249,7 +249,7 @@ class MockArraySessionStorage implements SessionStorageInterface
*/ */
protected function generateId() protected function generateId()
{ {
return sha1(uniqid(mt_rand())); return sha1(uniqid('ss_mock_', true));
} }
protected function loadSession() protected function loadSession()

View File

@ -183,7 +183,7 @@ class Esi
$chunks[$i] = sprintf('<?php echo $this->esi->handle($this, %s, %s, %s) ?>'."\n", $chunks[$i] = sprintf('<?php echo $this->esi->handle($this, %s, %s, %s) ?>'."\n",
var_export($options['src'], true), var_export($options['src'], true),
var_export(isset($options['alt']) ? $options['alt'] : '', true), var_export(isset($options['alt']) ? $options['alt'] : '', true),
isset($options['onerror']) && 'continue' == $options['onerror'] ? 'true' : 'false' isset($options['onerror']) && 'continue' === $options['onerror'] ? 'true' : 'false'
); );
++$i; ++$i;
$chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]); $chunks[$i] = str_replace($this->phpEscapeMap[0], $this->phpEscapeMap[1], $chunks[$i]);

View File

@ -49,7 +49,7 @@ class ProcessUtils
$escapedArgument = ''; $escapedArgument = '';
$quote = false; $quote = false;
foreach (preg_split('/(")/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) { foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
if ('"' === $part) { if ('"' === $part) {
$escapedArgument .= '\\"'; $escapedArgument .= '\\"';
} elseif (self::isSurroundedBy($part, '%')) { } elseif (self::isSurroundedBy($part, '%')) {

View File

@ -32,7 +32,7 @@ while ($read || $write) {
} }
$out = (binary) substr($out, $written); $out = (binary) substr($out, $written);
} }
if (null === $read && strlen($out) < 1) { if (null === $read && '' === $out) {
$write = array_diff($write, array(STDOUT)); $write = array_diff($write, array(STDOUT));
} }
@ -43,7 +43,7 @@ while ($read || $write) {
} }
$err = (binary) substr($err, $written); $err = (binary) substr($err, $written);
} }
if (null === $read && strlen($err) < 1) { if (null === $read && '' === $err) {
$write = array_diff($write, array(STDERR)); $write = array_diff($write, array(STDERR));
} }

View File

@ -45,7 +45,7 @@ class Inline
$value = trim($value); $value = trim($value);
if (0 == strlen($value)) { if ('' === $value) {
return ''; return '';
} }

View File

@ -509,9 +509,9 @@ class Parser
// deal with trailing newlines as indicated // deal with trailing newlines as indicated
if ('' === $indicator) { if ('' === $indicator) {
$text = preg_replace('/\n+$/s', "\n", $text); $text = preg_replace('/\n+$/', "\n", $text);
} elseif ('-' === $indicator) { } elseif ('-' === $indicator) {
$text = preg_replace('/\n+$/s', '', $text); $text = preg_replace('/\n+$/', '', $text);
} }
return $text; return $text;
@ -610,7 +610,7 @@ class Parser
$value = $trimmedValue; $value = $trimmedValue;
// remove end of the document marker (...) // remove end of the document marker (...)
$value = preg_replace('#\.\.\.\s*$#s', '', $value); $value = preg_replace('#\.\.\.\s*$#', '', $value);
} }
return $value; return $value;