From bc0f17fb0972749cf5ca7b0b1815a7f330727e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Werner=20M=2E=20Krau=C3=9F?= Date: Wed, 7 Nov 2018 17:45:01 +0100 Subject: [PATCH] Cleanup Convert * remove unneeded parenthesis and double quotes * simplify flow / remove unneeded else --- src/Core/Convert.php | 84 +++++++++++++++++----------------- tests/php/Core/ConvertTest.php | 20 ++++---- 2 files changed, 52 insertions(+), 52 deletions(-) diff --git a/src/Core/Convert.php b/src/Core/Convert.php index 313d64aae..3ff3336ce 100644 --- a/src/Core/Convert.php +++ b/src/Core/Convert.php @@ -70,9 +70,9 @@ class Convert } return $val; - } else { - return self::raw2att($val); } + + return self::raw2att($val); } /** @@ -93,16 +93,16 @@ class Convert } return $val; - } else { - return trim( - preg_replace( - '/_+/', - '_', - preg_replace('/[^a-zA-Z0-9\-_:.]+/', '_', $val) - ), - '_' - ); } + + return trim( + preg_replace( + '/_+/', + '_', + preg_replace('/[^a-zA-Z0-9\-_:.]+/', '_', $val) + ), + '_' + ); } /** @@ -119,9 +119,9 @@ class Convert $val[$k] = self::raw2xml($v); } return $val; - } else { - return htmlspecialchars($val, ENT_QUOTES, 'UTF-8'); } + + return htmlspecialchars($val, ENT_QUOTES, 'UTF-8'); } /** @@ -137,14 +137,14 @@ class Convert $val[$k] = self::raw2js($v); } return $val; - } else { - return str_replace( - // Intercepts some characters such as <, >, and & which can interfere - array("\\", '"', "\n", "\r", "'", "<", ">", "&"), - array("\\\\", '\"', '\n', '\r', "\\'", "\\x3c", "\\x3e", "\\x26"), - $val - ); } + + return str_replace( + // Intercepts some characters such as <, >, and & which can interfere + array("\\", '"', "\n", "\r", "'", '<', '>', '&'), + array("\\\\", '\"', '\n', '\r', "\\'", "\\x3c", "\\x3e", "\\x26"), + $val + ); } /** @@ -195,13 +195,13 @@ class Convert $val[$k] = self::raw2sql($v, $quoted); } return $val; - } else { - if ($quoted) { - return DB::get_conn()->quoteString($val); - } else { - return DB::get_conn()->escapeString($val); - } } + + if ($quoted) { + return DB::get_conn()->quoteString($val); + } + + return DB::get_conn()->escapeString($val); } /** @@ -233,14 +233,14 @@ class Convert $val[$k] = self::xml2raw($v); } return $val; - } else { - // More complex text needs to use html2raw instead - if (strpos($val, '<') !== false) { - return self::html2raw($val); - } else { - return html_entity_decode($val, ENT_QUOTES, 'UTF-8'); - } } + + // More complex text needs to use html2raw instead + if (strpos($val, '<') !== false) { + return self::html2raw($val); + } + + return html_entity_decode($val, ENT_QUOTES, 'UTF-8'); } /** @@ -332,7 +332,7 @@ class Convert $xml = get_object_vars($xml); } if (is_array($xml)) { - if (count($xml) == 0) { + if (count($xml) === 0) { return (string)$x; } // for CDATA $r = []; @@ -359,9 +359,9 @@ class Convert { if (preg_match('/^[a-z+]+\:\/\/[a-zA-Z0-9$-_.+?&=!*\'()%]+$/', $string)) { return "$string"; - } else { - return $string; } + + return $string; } /** @@ -387,8 +387,8 @@ class Convert $config = $defaultConfig; } - $data = preg_replace("/][^>]*)?>.*?<\/style[^>]*>/is", "", $data); - $data = preg_replace("/][^>]*)?>.*?<\/script[^>]*>/is", "", $data); + $data = preg_replace("/][^>]*)?>.*?<\/style[^>]*>/is", '', $data); + $data = preg_replace("/][^>]*)?>.*?<\/script[^>]*>/is", '', $data); if ($config['ReplaceBoldAsterisk']) { $data = preg_replace('%<(strong|b)( [^>]*)?>|%i', '*', $data); @@ -412,7 +412,7 @@ class Convert // Compress whitespace if ($config['CompressWhitespace']) { - $data = preg_replace("/\s+/u", " ", $data); + $data = preg_replace("/\s+/u", ' ', $data); } // Parse newline tags @@ -421,9 +421,9 @@ class Convert $data = preg_replace("/\s*<[Dd][Ii][Vv]([^A-Za-z0-9>][^>]*)?> */u", "\n\n", $data); $data = preg_replace("/\n\n\n+/", "\n\n", $data); - $data = preg_replace("/<[Bb][Rr]([^A-Za-z0-9>][^>]*)?> */", "\n", $data); - $data = preg_replace("/<[Tt][Rr]([^A-Za-z0-9>][^>]*)?> */", "\n", $data); - $data = preg_replace("/<\/[Tt][Dd]([^A-Za-z0-9>][^>]*)?> */", " ", $data); + $data = preg_replace('/<[Bb][Rr]([^A-Za-z0-9>][^>]*)?> */', "\n", $data); + $data = preg_replace('/<[Tt][Rr]([^A-Za-z0-9>][^>]*)?> */', "\n", $data); + $data = preg_replace("/<\/[Tt][Dd]([^A-Za-z0-9>][^>]*)?> */", ' ', $data); $data = preg_replace('/<\/p>/i', "\n\n", $data); // Replace HTML entities diff --git a/tests/php/Core/ConvertTest.php b/tests/php/Core/ConvertTest.php index af8d454c1..a37fbc81c 100644 --- a/tests/php/Core/ConvertTest.php +++ b/tests/php/Core/ConvertTest.php @@ -133,7 +133,7 @@ class ConvertTest extends SapphireTest $this->assertEquals( "That's absolutely correct", Convert::html2raw($val7), - "Single quotes are decoded correctly" + 'Single quotes are decoded correctly' ); $val8 = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ' . 'incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud ' . 'exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute ' . 'irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla ' . 'pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia ' . 'deserunt mollit anim id est laborum.'; @@ -280,7 +280,7 @@ PHP protected function assertEqualsQuoted($expected, $actual) { $message = sprintf( - "Expected \"%s\" but given \"%s\"", + 'Expected "%s" but given "%s"', addcslashes($expected, "\r\n"), addcslashes($actual, "\r\n") ); @@ -296,8 +296,8 @@ PHP foreach (array("\r\n", "\r", "\n") as $nl) { // Base case: no action $this->assertEqualsQuoted( - "Base case", - Convert::nl2os("Base case", $nl) + 'Base case', + Convert::nl2os('Base case', $nl) ); // Mixed formats @@ -426,7 +426,7 @@ XML // Test without doctype validation $expected = array( 'result' => array( - "Now include SOME_SUPER_LONG_STRING lots of times to expand the in-memory size of this XML structure", + 'Now include SOME_SUPER_LONG_STRING lots of times to expand the in-memory size of this XML structure', array( 'long' => array( array( @@ -607,11 +607,11 @@ XML { return [ [200, '200B'], - [(2 * 1024), '2K'], - [(512 * 1024 * 1024), '512M'], - [(512 * 1024 * 1024 * 1024), '512G'], - [(512 * 1024 * 1024 * 1024 * 1024), '512T'], - [(512 * 1024 * 1024 * 1024 * 1024 * 1024), '512P'] + [2 * 1024, '2K'], + [512 * 1024 * 1024, '512M'], + [512 * 1024 * 1024 * 1024, '512G'], + [512 * 1024 * 1024 * 1024 * 1024, '512T'], + [512 * 1024 * 1024 * 1024 * 1024 * 1024, '512P'] ]; }