1- <?php namespace October \Rain \Config ;
1+ <?php
2+
3+ namespace Tekreme73 \Laravel \ConfigWriter \DataWriter ;
24
35use Exception ;
46
2628class Rewrite
2729{
2830
29- public function toFile ($ filePath , $ newValues , $ useValidation = true )
31+ public function toFile (string $ filePath , array $ newValues , bool $ useValidation = true ): string
3032 {
3133 $ contents = file_get_contents ($ filePath );
3234 $ contents = $ this ->toContent ($ contents , $ newValues , $ useValidation );
3335 file_put_contents ($ filePath , $ contents );
36+
3437 return $ contents ;
3538 }
3639
37- public function toContent ($ contents , $ newValues , $ useValidation = true )
40+ public function toContent (string $ contents , array $ newValues , bool $ useValidation = true ): string
3841 {
3942 $ contents = $ this ->parseContent ($ contents , $ newValues );
4043
@@ -65,7 +68,7 @@ public function toContent($contents, $newValues, $useValidation = true)
6568 return $ contents ;
6669 }
6770
68- protected function parseContent ($ contents , $ newValues )
71+ protected function parseContent (string $ contents , array $ newValues ): string
6972 {
7073 $ result = $ contents ;
7174
@@ -76,22 +79,22 @@ protected function parseContent($contents, $newValues)
7679 return $ result ;
7780 }
7881
79- protected function parseContentValue ($ contents , $ path , $ value )
82+ protected function parseContentValue (string $ contents , string $ path , $ value ): string
8083 {
8184 $ result = $ contents ;
8285 $ items = explode ('. ' , $ path );
8386 $ key = array_pop ($ items );
8487 $ replaceValue = $ this ->writeValueToPhp ($ value );
8588
8689 $ count = 0 ;
87- $ patterns = array () ;
90+ $ patterns = [] ;
8891 $ patterns [] = $ this ->buildStringExpression ($ key , $ items );
8992 $ patterns [] = $ this ->buildStringExpression ($ key , $ items , '" ' );
9093 $ patterns [] = $ this ->buildConstantExpression ($ key , $ items );
9194 $ patterns [] = $ this ->buildArrayExpression ($ key , $ items );
9295
9396 foreach ($ patterns as $ pattern ) {
94- $ result = preg_replace ($ pattern , '${1}${2} ' . $ replaceValue , $ result , 1 , $ count );
97+ $ result = preg_replace ($ pattern , '${1}${2} ' . $ replaceValue , $ result , 1 , $ count );
9598
9699 if ($ count > 0 ) {
97100 break ;
@@ -101,7 +104,7 @@ protected function parseContentValue($contents, $path, $value)
101104 return $ result ;
102105 }
103106
104- protected function writeValueToPhp ($ value )
107+ protected function writeValueToPhp ($ value ): string
105108 {
106109 if (is_string ($ value ) && strpos ($ value , "' " ) === false ) {
107110 $ replaceValue = "' " .$ value ."' " ;
@@ -127,7 +130,7 @@ protected function writeValueToPhp($value)
127130 return $ replaceValue ;
128131 }
129132
130- protected function writeArrayToPhp ($ array )
133+ protected function writeArrayToPhp (array $ array ): array
131134 {
132135 $ result = [];
133136
@@ -142,9 +145,9 @@ protected function writeArrayToPhp($array)
142145 return $ result ;
143146 }
144147
145- protected function buildStringExpression ($ targetKey , $ arrayItems = array (), $ quoteChar = "' " )
148+ protected function buildStringExpression (string $ targetKey , array $ arrayItems = [], string $ quoteChar = "' " ): string
146149 {
147- $ expression = array () ;
150+ $ expression = [] ;
148151
149152 // Opening expression for array items ($1)
150153 $ expression [] = $ this ->buildArrayOpeningExpression ($ arrayItems );
@@ -164,9 +167,9 @@ protected function buildStringExpression($targetKey, $arrayItems = array(), $quo
164167 /**
165168 * Common constants only (true, false, null, integers)
166169 */
167- protected function buildConstantExpression ($ targetKey , $ arrayItems = array ())
170+ protected function buildConstantExpression (string $ targetKey , array $ arrayItems = []): string
168171 {
169- $ expression = array () ;
172+ $ expression = [] ;
170173
171174 // Opening expression for array items ($1)
172175 $ expression [] = $ this ->buildArrayOpeningExpression ($ arrayItems );
@@ -183,9 +186,9 @@ protected function buildConstantExpression($targetKey, $arrayItems = array())
183186 /**
184187 * Single level arrays only
185188 */
186- protected function buildArrayExpression ($ targetKey , $ arrayItems = array ())
189+ protected function buildArrayExpression (string $ targetKey , array $ arrayItems = []): string
187190 {
188- $ expression = array () ;
191+ $ expression = [] ;
189192
190193 // Opening expression for array items ($1)
191194 $ expression [] = $ this ->buildArrayOpeningExpression ($ arrayItems );
@@ -199,17 +202,17 @@ protected function buildArrayExpression($targetKey, $arrayItems = array())
199202 return '/ ' . implode ('' , $ expression ) . '/ ' ;
200203 }
201204
202- protected function buildArrayOpeningExpression ($ arrayItems )
205+ protected function buildArrayOpeningExpression (array $ arrayItems ): string
203206 {
204207 if (count ($ arrayItems )) {
205- $ itemOpen = array () ;
208+ $ itemOpen = [] ;
206209 foreach ($ arrayItems as $ item ) {
207210 // The left hand array assignment
208211 $ itemOpen [] = '[ \'|"] ' .$ item .'[ \'|"]\s*=>\s*(?:[aA][rR]{2}[aA][yY]\(|[\[]) ' ;
209212 }
210213
211214 // Capture all opening array (non greedy)
212- $ result = '( ' . implode ('[\s\S]* ' , $ itemOpen ) . '[\s\S]*?) ' ;
215+ $ result = '( ' . implode ('[\s\S]*? ' , $ itemOpen ) . '[\s\S]*?) ' ;
213216 }
214217 else {
215218 // Gotta capture something for $1
0 commit comments