From a1ff879f19ad7e95b06d9a3437e4c9415a7f1011 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Wed, 24 Dec 2014 08:44:59 +0100 Subject: [PATCH] Review. Thanks @jubianchi! --- String.php | 7 +++++-- Test/Unit/String.php | 31 ++++++++----------------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/String.php b/String.php index 294c2a2..6371a95 100644 --- a/String.php +++ b/String.php @@ -285,10 +285,10 @@ public function pad ( $length, $piece, $side = self::END ) { */ public function compare ( $string ) { - if(false === class_exists('Collator', false)) + if(null === $collator = static::getCollator()) return strcmp($this->_string, (string) $string); - return static::getCollator()->compare($this->_string, $string); + return $collator->compare($this->_string, $string); } /** @@ -299,6 +299,9 @@ public function compare ( $string ) { */ public static function getCollator ( ) { + if(false === class_exists('Collator', false)) + return null; + if(null === static::$_collator) static::$_collator = new \Collator(setlocale(LC_COLLATE, null)); diff --git a/Test/Unit/String.php b/Test/Unit/String.php index 1d81430..3a95b40 100644 --- a/Test/Unit/String.php +++ b/Test/Unit/String.php @@ -57,10 +57,7 @@ public function case_no_mbstring ( ) { ->given( $this->function->function_exists = function ( $name ) { - if('mb_substr' === $name) - return false; - - return true; + return 'mb_substr' !== $name; } ) ->exception(function ( ) { @@ -172,10 +169,7 @@ public function case_compare_no_collator ( ) { ->given( $this->function->class_exists = function ( $name ) { - if('Collator' === $name) - return false; - - return true; + return 'Collator' !== $name; }, $string = new LUT('b') ) @@ -278,7 +272,7 @@ public function case_match_offset ( ) { ->integer($result) ->isEqualTo(0) ->array($matches) - ->isEqualTo([]); + ->isEmpty(); } public function case_match_with_offset ( ) { @@ -860,10 +854,7 @@ public function case_to_ascii_no_normalizer ( ) { ->given( $this->function->class_exists = function ( $name ) { - if('Normalizer' === $name) - return false; - - return true; + return 'Normalizer' !== $name; }, $string = new LUT('Un été brûlant sur la côte') ) @@ -880,10 +871,7 @@ public function case_to_ascii_no_normalizer_try ( ) { ->given( $this->function->class_exists = function ( $name ) { - if('Normalizer' === $name) - return false; - - return true; + return 'Normalizer' !== $name; }, $string = new LUT('Un été brûlant sur la côte') ) @@ -920,13 +908,10 @@ public function case_copy ( ) { public function case_toString ( ) { $this - ->given( - $datum = $this->sample($this->realdom->regex('/\w{7,42}/')), - $string = new LUT($datum) - ) - ->when($result = (string) $string) + ->given($datum = $this->sample($this->realdom->regex('/\w{7,42}/'))) + ->when($result = new LUT($datum)) ->then - ->string($result) + ->castToString($result) ->isEqualTo($datum); } }