From 004a1929221f759be2737d14f0138a0dd34a362f Mon Sep 17 00:00:00 2001 From: Vladimir Reznichenko Date: Thu, 7 Sep 2017 19:11:48 +0200 Subject: [PATCH] SCA with Php Inspections (EA Extended) Closes #208 --- src/PhpSpreadsheet/Calculation/DateTime.php | 6 +++--- src/PhpSpreadsheet/Calculation/FormulaParser.php | 6 +++--- src/PhpSpreadsheet/Calculation/LookupRef.php | 2 +- src/PhpSpreadsheet/Reader/Ods.php | 8 ++++---- src/PhpSpreadsheet/Reader/Xls.php | 2 +- src/PhpSpreadsheet/Spreadsheet.php | 2 +- src/PhpSpreadsheet/Style/NumberFormat.php | 2 +- src/PhpSpreadsheet/Writer/Html.php | 9 ++------- src/PhpSpreadsheet/Writer/Xls/Worksheet.php | 1 - src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 2 +- 10 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/PhpSpreadsheet/Calculation/DateTime.php b/src/PhpSpreadsheet/Calculation/DateTime.php index d8a1d000..1c9317b8 100644 --- a/src/PhpSpreadsheet/Calculation/DateTime.php +++ b/src/PhpSpreadsheet/Calculation/DateTime.php @@ -493,7 +493,7 @@ class DateTime // Strip any ordinals because they're allowed in Excel (English only) $dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui', '$1$3', $dateValue); // Convert separators (/ . or space) to hyphens (should also handle dot used for ordinals in some countries, e.g. Denmark, Germany) - $dateValue = str_replace(['/', '.', '-', ' '], [' ', ' ', ' ', ' '], $dateValue); + $dateValue = str_replace(['/', '.', '-', ' '], ' ', $dateValue); $yearFound = false; $t1 = explode(' ', $dateValue); @@ -520,7 +520,7 @@ class DateTime $t1[1] += 1900; array_unshift($t1, 1); } else { - array_push($t1, date('Y')); + $t1[] = date('Y'); } } } @@ -621,7 +621,7 @@ class DateTime public static function TIMEVALUE($timeValue) { $timeValue = trim(Functions::flattenSingleValue($timeValue), '"'); - $timeValue = str_replace(['/', '.'], ['-', '-'], $timeValue); + $timeValue = str_replace(['/', '.'], '-', $timeValue); $arraySplit = preg_split('/[\/:\-\s]/', $timeValue); if ((count($arraySplit) == 2 || count($arraySplit) == 3) && $arraySplit[0] > 24) { diff --git a/src/PhpSpreadsheet/Calculation/FormulaParser.php b/src/PhpSpreadsheet/Calculation/FormulaParser.php index f37c4716..dbb94f29 100644 --- a/src/PhpSpreadsheet/Calculation/FormulaParser.php +++ b/src/PhpSpreadsheet/Calculation/FormulaParser.php @@ -253,7 +253,7 @@ class FormulaParser // establish state-dependent character evaluations if ($this->formula[$index] == self::QUOTE_DOUBLE) { - if (strlen($value > 0)) { + if (strlen($value) > 0) { // unexpected $tokens1[] = new FormulaToken($value, FormulaToken::TOKEN_TYPE_UNKNOWN); $value = ''; @@ -593,7 +593,7 @@ class FormulaParser if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_OPERAND && $token->getTokenSubType() == FormulaToken::TOKEN_SUBTYPE_NOTHING) { if (!is_numeric($token->getValue())) { - if (strtoupper($token->getValue()) == 'TRUE' || strtoupper($token->getValue() == 'FALSE')) { + if (strtoupper($token->getValue()) == 'TRUE' || strtoupper($token->getValue()) == 'FALSE') { $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_LOGICAL); } else { $token->setTokenSubType(FormulaToken::TOKEN_SUBTYPE_RANGE); @@ -607,7 +607,7 @@ class FormulaParser } if ($token->getTokenType() == FormulaToken::TOKEN_TYPE_FUNCTION) { - if (strlen($token->getValue() > 0)) { + if (strlen($token->getValue()) > 0) { if (substr($token->getValue(), 0, 1) == '@') { $token->setValue(substr($token->getValue(), 1)); } diff --git a/src/PhpSpreadsheet/Calculation/LookupRef.php b/src/PhpSpreadsheet/Calculation/LookupRef.php index 010aea93..9672a6da 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef.php @@ -201,7 +201,7 @@ class LookupRef if (is_array($cellAddress)) { foreach ($cellAddress as $columnKey => $rowValue) { foreach ($rowValue as $rowKey => $cellValue) { - return (int) preg_replace('/[^0-9]/i', '', $rowKey); + return (int) preg_replace('/[^0-9]/', '', $rowKey); } } } else { diff --git a/src/PhpSpreadsheet/Reader/Ods.php b/src/PhpSpreadsheet/Reader/Ods.php index d454a86d..31ff4aa2 100644 --- a/src/PhpSpreadsheet/Reader/Ods.php +++ b/src/PhpSpreadsheet/Reader/Ods.php @@ -650,16 +650,16 @@ class Ods extends BaseReader implements IReader // Only replace in alternate array entries (i.e. non-quoted blocks) if ($tKey = !$tKey) { // Cell range reference in another sheet - $value = preg_replace('/\[([^\.]+)\.([^\.]+):\.([^\.]+)\]/Ui', '$1!$2:$3', $value); + $value = preg_replace('/\[([^\.]+)\.([^\.]+):\.([^\.]+)\]/U', '$1!$2:$3', $value); // Cell reference in another sheet - $value = preg_replace('/\[([^\.]+)\.([^\.]+)\]/Ui', '$1!$2', $value); + $value = preg_replace('/\[([^\.]+)\.([^\.]+)\]/U', '$1!$2', $value); // Cell range reference - $value = preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/Ui', '$1:$2', $value); + $value = preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/U', '$1:$2', $value); // Simple cell reference - $value = preg_replace('/\[\.([^\.]+)\]/Ui', '$1', $value); + $value = preg_replace('/\[\.([^\.]+)\]/U', '$1', $value); $value = Calculation::translateSeparator(';', ',', $value, $inBraces); } diff --git a/src/PhpSpreadsheet/Reader/Xls.php b/src/PhpSpreadsheet/Reader/Xls.php index 45798fbe..b8a9f60c 100644 --- a/src/PhpSpreadsheet/Reader/Xls.php +++ b/src/PhpSpreadsheet/Reader/Xls.php @@ -7073,7 +7073,7 @@ class Xls extends BaseReader implements IReader // todo: check if we have identified the whole set of special characters // it seems that the following characters are not accepted for sheet names // and we may assume that they are not present: []*/:\? - if (preg_match("/[ !\"@#£$%&{()}<>=+'|^,;-]/", $sheetRange)) { + if (preg_match("/[ !\"@#£$%&{()}<>=+'|^,;-]/u", $sheetRange)) { $sheetRange = "'$sheetRange'"; } diff --git a/src/PhpSpreadsheet/Spreadsheet.php b/src/PhpSpreadsheet/Spreadsheet.php index 2724286b..f78d94d9 100644 --- a/src/PhpSpreadsheet/Spreadsheet.php +++ b/src/PhpSpreadsheet/Spreadsheet.php @@ -371,7 +371,7 @@ class Spreadsheet */ public function __construct() { - $this->uniqueID = uniqid(); + $this->uniqueID = uniqid('', true); $this->calculationEngine = new Calculation($this); // Initialise worksheet collection and add one worksheet diff --git a/src/PhpSpreadsheet/Style/NumberFormat.php b/src/PhpSpreadsheet/Style/NumberFormat.php index 21aaf40e..2d5bb178 100644 --- a/src/PhpSpreadsheet/Style/NumberFormat.php +++ b/src/PhpSpreadsheet/Style/NumberFormat.php @@ -507,7 +507,7 @@ class NumberFormat extends Supervisor implements IComparable if ($format === self::FORMAT_PERCENTAGE) { $value = round((100 * $value), 0) . '%'; } else { - if (preg_match('/\.[#0]+/i', $format, $m)) { + if (preg_match('/\.[#0]+/', $format, $m)) { $s = substr($m[0], 0, 1) . (strlen($m[0]) - 1); $format = str_replace($m[0], $s, $format); } diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 0a08bc0b..cadf20ba 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -739,7 +739,7 @@ class Html extends BaseWriter implements IWriter if ($chart instanceof Chart) { $chartCoordinates = $chart->getTopLeftPosition(); if ($chartCoordinates['cell'] == $coordinates) { - $chartFileName = File::sysGetTempDir() . '/' . uniqid() . '.png'; + $chartFileName = File::sysGetTempDir() . '/' . uniqid('', true) . '.png'; if (!$chart->render($chartFileName)) { return; } @@ -978,9 +978,6 @@ class Html extends BaseWriter implements IWriter */ private function createCSSStyle(Style $pStyle) { - // Construct CSS - $css = ''; - // Create CSS $css = array_merge( $this->createCSSStyleAlignment($pStyle->getAlignment()), @@ -1235,7 +1232,6 @@ class Html extends BaseWriter implements IWriter $cell = ($cellAddress > '') ? $pSheet->getCell($cellAddress) : ''; $coordinate = Cell::stringFromColumnIndex($colNum) . ($pRow + 1); if (!$this->useInlineCss) { - $cssClass = ''; $cssClass = 'column' . $colNum; } else { $cssClass = []; @@ -1541,8 +1537,7 @@ class Html extends BaseWriter implements IWriter $color_regex = '/^\\[[a-zA-Z]+\\]/'; if (preg_match($color_regex, $pFormat, $matches)) { - $color = str_replace('[', '', $matches[0]); - $color = str_replace(']', '', $color); + $color = str_replace(['[', ']'], '', $matches[0]); $color = strtolower($color); } diff --git a/src/PhpSpreadsheet/Writer/Xls/Worksheet.php b/src/PhpSpreadsheet/Writer/Xls/Worksheet.php index 5f539650..93423002 100644 --- a/src/PhpSpreadsheet/Writer/Xls/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xls/Worksheet.php @@ -1438,7 +1438,6 @@ class Worksheet extends BIFFwriter private function writeSelection() { // look up the selected cell range - $selectedCells = $this->phpSheet->getSelectedCells(); $selectedCells = Cell::splitRange($this->phpSheet->getSelectedCells()); $selectedCells = $selectedCells[0]; if (count($selectedCells) == 2) { diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index a2e1058c..a1a97865 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -790,7 +790,7 @@ class Worksheet extends WriterPart $objWriter->writeAttribute('ref', str_replace('$', '', $range)); $columns = $pSheet->getAutoFilter()->getColumns(); - if (count($columns > 0)) { + if (count($columns) > 0) { foreach ($columns as $columnID => $column) { $rules = $column->getRules(); if (count($rules) > 0) {