Skip to content

Commit 9343ba3

Browse files
committed
Fix Coding Quality Issues
1 parent 96fffe0 commit 9343ba3

7 files changed

Lines changed: 102 additions & 108 deletions

File tree

webfiori/json/CaseConverter.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
<?php
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2022 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/.github/blob/main/LICENSE
9+
*
10+
*/
211
namespace webfiori\json;
312

413
/**
5-
* A class which is used to convert string case from one to another (e.g. camle to snake).
14+
* A class which is used to convert string case from one to another (e.g. camel to snake).
615
*
716
* @author Ibrahim
817
*
@@ -40,13 +49,13 @@ class CaseConverter {
4049
* <li>kebab</li>
4150
* <li>camel</li>
4251
* </ul>
43-
* If the given value is non of the given 3, the string woun't be changed.
52+
* If the given value is none of the given 3, the string wouldn't be changed.
4453
*
4554
* @return string The same string converted to selected style.
4655
*
4756
* @since 1.0
4857
*/
49-
public static function convert($value, $style) {
58+
public static function convert(string $value, string $style) : string {
5059
if ($style == 'snake') {
5160
return self::toSnackCase($value);
5261
} else if ($style == 'kebab') {
@@ -67,7 +76,7 @@ public static function convert($value, $style) {
6776
*
6877
* @since 1.0
6978
*/
70-
public static function toCamelCase($value) {
79+
public static function toCamelCase(string $value) : string {
7180
$retVal = '';
7281
$changeNextCharCase = false;
7382
$valueTrim = trim($value);
@@ -100,7 +109,7 @@ public static function toCamelCase($value) {
100109
*
101110
* @since 1.0
102111
*/
103-
public static function toKebabCase($value) {
112+
public static function toKebabCase(string $value) : string {
104113
return self::_toSnakeOrKebab($value, '_', '-');
105114
}
106115
/**
@@ -113,13 +122,13 @@ public static function toKebabCase($value) {
113122
*
114123
* @since 1.0
115124
*/
116-
public static function toSnackCase($value) {
125+
public static function toSnackCase(string $value) : string {
117126
return self::_toSnakeOrKebab($value, '-', '_');
118127
}
119-
private static function _isUpper($char) {
128+
private static function _isUpper(string $char) : bool {
120129
return $char >= 'A' && $char <= 'Z';
121130
}
122-
private static function _toSnakeOrKebab($value, $from, $to) {
131+
private static function _toSnakeOrKebab(string $value, string $from, string $to) : string {
123132
$attr1 = str_replace($from, $to, trim($value));
124133
$retVal = '';
125134
$isNumFound = false;
@@ -140,7 +149,7 @@ private static function _toSnakeOrKebab($value, $from, $to) {
140149

141150
return $retVal;
142151
}
143-
private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) {
152+
private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) : string {
144153
$isUpper = self::_isUpper($char);
145154
$retVal = '';
146155

@@ -160,7 +169,7 @@ private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFoun
160169
return $retVal;
161170
}
162171

163-
private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) {
172+
private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) : string {
164173
$retVal = '';
165174

166175
if ($x == 0) {

webfiori/json/Json.php

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
<?php
2-
/*
3-
* The MIT License
4-
*
5-
* Copyright 2019 Ibrahim, WebFiori Json library.
2+
/**
3+
* This file is licensed under MIT License.
64
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
5+
* Copyright (c) 2019 Ibrahim BinAlshikh
136
*
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/.github/blob/main/LICENSE
169
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
* THE SOFTWARE.
2410
*/
2511
namespace webfiori\json;
2612

@@ -95,12 +81,12 @@ class Json {
9581
* Creates new instance of the class.
9682
*
9783
* @param array|string $initialData Initial data which is used to initialize
98-
* the object. It can be a string which looks like JSON or it can be an
84+
* the object. It can be a string which looks like JSON, or it can be an
9985
* associative array. If it is an associative array, then the keys will be
10086
* acting as properties and the value of each key will be the value of
10187
* the property.
10288
*
103-
* @param boolean $isFormatted If this attribute is set to true, the generated
89+
* @param bool $isFormatted If this attribute is set to true, the generated
10490
* JSON will be indented and have new lines (readable). Note that the parameter
10591
* will be ignored if the constant 'WF_VERBOSE' is defined and is set to true.
10692
*
@@ -124,9 +110,9 @@ public function __construct(array $initialData = [], $isFormatted = false) {
124110
* @param string $key The value of the key. Note that the style of the key
125111
* does not matter.
126112
*
127-
* @return Json|mixed|null The return type will depends on the value which
113+
* @return Json|mixed|null The return type will depend on the value which
128114
* was set by any method which can be used to add props. It can be a number,
129-
* a boolean, string, an object or null if does not exist.
115+
* a boolean, string, an object or null if it does not exist.
130116
*
131117
* @since 1.2
132118
*/
@@ -189,7 +175,7 @@ public function __toString() {
189175
* is an array. If set to true, the array will be added as an object.
190176
* Default is false.
191177
*
192-
* @return boolean The method will return true if the value is set.
178+
* @return bool The method will return true if the value is set.
193179
* If the given value or key is invalid, the method will return false.
194180
*
195181
* @since 1.1
@@ -215,11 +201,11 @@ public function add(string $key, $value, $arrayAsObj = false) {
215201
*
216202
* @param array $value The array that will be added.
217203
*
218-
* @param boolean $asObject If this parameter is set to true,
204+
* @param bool $asObject If this parameter is set to true,
219205
* the array will be added as an object in JSON string. Note that if the
220206
* array is associative, each index will be added as an object. Default is false.
221207
*
222-
* @return boolean The method will return false if the given key is invalid
208+
* @return bool The method will return false if the given key is invalid
223209
* or the given value is not an array.
224210
*/
225211
public function addArray(string $key, $value, $asObject = false) {
@@ -248,16 +234,16 @@ public function addArray(string $key, $value, $asObject = false) {
248234
*
249235
* @param string $key The name of the key.
250236
*
251-
* @param boolean $val true or false. If not specified,
237+
* @param bool $val true or false. If not specified,
252238
* The default will be true.
253239
*
254-
* @return boolean The method will return true in case the value is set.
240+
* @return bool The method will return true in case the value is set.
255241
* If the given value is not a boolean or the key value is invalid string,
256242
* the method will return false.
257243
*
258244
* @since 1.0
259245
*/
260-
public function addBoolean($key, $val = true) {
246+
public function addBoolean($key, $val = true) : bool {
261247
if (!$this->updateExisting($key, $val)) {
262248
$prop = $this->createProb($key, $val);
263249

@@ -294,10 +280,10 @@ public function addMultiple(array $arr) {
294280
*
295281
* @param string $key The name of value key.
296282
*
297-
* @return boolean The method will return true if the value is set.
283+
* @return bool The method will return true if the value is set.
298284
* If the given value or key is invalid, the method will return false.
299285
*/
300-
public function addNull(string $key) {
286+
public function addNull(string $key) : bool {
301287
$nul = null;
302288

303289
if (!$this->updateExisting($key, $nul)) {
@@ -327,7 +313,7 @@ public function addNull(string $key) {
327313
*
328314
* @param int|double $value The value of the key.
329315
*
330-
* @return boolean The method will return true in case the number is
316+
* @return bool The method will return true in case the number is
331317
* added. If the given value is not a number or the key value is invalid
332318
* string, the method
333319
* will return false.
@@ -356,19 +342,19 @@ public function addNumber(string $key, $value) {
356342
* The object that will be added can implement the interface JsonI to make
357343
* the generated JSON string customizable. Also, the object can be of
358344
* type Json. If the given value is an object that does not implement the
359-
* interface JsonI or it is not of type Json,
345+
* interface JsonI, or it is not of type Json,
360346
* The method will try to extract object information based on its "getXxxxx()" public
361347
* methods. Assuming that the object has 2 public methods with names
362348
* <code>getFirstProp()</code> and <code>getSecondProp()</code>.
363-
* In that case, the generated JSON will be on the formate
349+
* In that case, the generated JSON will be on the format
364350
* <b>{"FirstProp":"prop-1","SecondProp":""}</b>.
365351
* This method also can be used to update the value of an existing property.
366352
*
367353
* @param string $key The key value.
368354
*
369355
* @param JsonI|Json|object $val The object that will be added.
370356
*
371-
* @return boolean The method will return true if the object is added.
357+
* @return bool The method will return true if the object is added.
372358
* If the key value is invalid string, the method will return false.
373359
*
374360
* @since 1.0
@@ -394,11 +380,11 @@ public function addObject(string $key, &$val) {
394380
*
395381
* This method also can be used to update the value of an existing property.
396382
*
397-
* @param string $key The name of the key. Must be non empty string.
383+
* @param string $key The name of the key. Must be non-empty string.
398384
*
399385
* @param string $val The value of the string.
400386
*
401-
* @return boolean The method will return true in case the string is added.
387+
* @return bool The method will return true in case the string is added.
402388
* If the given value is not a string or the given key is invalid, the
403389
* method will return false.
404390
*
@@ -558,7 +544,7 @@ public function getPropStyle() {
558544
*
559545
* @param string $key The name of the key.
560546
*
561-
* @return boolean The method will return true if the
547+
* @return bool The method will return true if the
562548
* key exists. false if not.
563549
*
564550
* @since 1.2
@@ -574,7 +560,8 @@ public function hasKey($key) {
574560
* This can be used to make the generated output readable by adding
575561
* indentation and new lines.
576562
*
577-
* @return boolean True if will be formatted. False otherwise.
563+
* @return bool The method will return true if the output will be formatted.
564+
* False otherwise.
578565
*/
579566
public function isFormatted() {
580567
return $this->formatted;
@@ -616,7 +603,7 @@ public function remove($keyName) {
616603
* a tree. If not formatted, the output string will be generated as one line.
617604
*
618605
*
619-
* @param boolean $bool True to make the output formatted and false to make
606+
* @param bool $bool True to make the output formatted and false to make
620607
* it not.
621608
*
622609
* @since 1.2.5
@@ -671,7 +658,7 @@ public function setPropsStyle($style) {
671658
* sent to. If the file does not exist, the method will attempt to create it.
672659
*
673660
* @param string $path The folder in file system that the file will be created
674-
* at. If does not exist, the method will attempt to create it.
661+
* at. If it does not exist, the method will attempt to create it.
675662
*
676663
* @param bool $override If a file exist in the specified location with same
677664
* name and this parameter is set to true, the method will override existing

webfiori/json/JsonConverter.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
<?php
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2022 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/.github/blob/main/LICENSE
9+
*
10+
*/
211
namespace webfiori\json;
312

413
/**
@@ -163,15 +172,15 @@ public static function toJsonXString(Json $json) {
163172
*
164173
* @param array $array
165174
*
166-
* @param type $asObj
175+
* @param bool $asObj
167176
*
168-
* @param type $propsStyle
177+
* @param string $propsStyle
169178
*
170179
* @return string
171180
*
172181
* @since 1.0
173182
*/
174-
private static function arrayToJsonString(array $array, $asObj, $propsStyle = 'snake') {
183+
private static function arrayToJsonString(array $array, bool $asObj, string $propsStyle = 'snake') {
175184
$retVal = '';
176185

177186
if ($asObj === true) {
@@ -300,7 +309,7 @@ private static function checkJsonXType($datatype, $value, Property $prop = null,
300309
}
301310
/**
302311
*
303-
* @param type $val
312+
* @param mixed $val
304313
*
305314
* @return string
306315
*
@@ -321,15 +330,15 @@ private static function getNumberVal($val) {
321330
}
322331
/**
323332
*
324-
* @param type $probVal
333+
* @param object $probVal
325334
*
326-
* @param type $style
335+
* @param string $style
327336
*
328337
* @return string
329338
*
330339
* @since 1.0
331340
*/
332-
private static function objToJson($probVal, $style) {
341+
private static function objToJson($probVal, string $style) {
333342
if (!($probVal instanceof Json)) {
334343
if (!is_subclass_of($probVal, 'webfiori\\json\\JsonI')) {
335344
$probVal = self::objectToJson($probVal);
@@ -401,7 +410,7 @@ private static function setIsFormatted($bool) {
401410
*
402411
* @since 1.0
403412
*/
404-
private static function updateTab($increase = true) {
413+
private static function updateTab(bool $increase = true) {
405414
if ($increase === true) {
406415
self::$CurrentTab++;
407416
} else {

webfiori/json/JsonException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
<?php
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2022 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/.github/blob/main/LICENSE
9+
*
10+
*/
211
namespace webfiori\json;
312

413
use Exception;

0 commit comments

Comments
 (0)