Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 833e4ea

Browse files
committed
run code inspection
1 parent 03640a5 commit 833e4ea

File tree

5 files changed

+70
-133
lines changed

5 files changed

+70
-133
lines changed

src/ObjectHelper.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
namespace Toolkit\ObjUtil;
1111

12-
use Inhere\Exceptions\DependencyResolutionException;
13-
1412
/**
1513
* Class ObjectHelper
1614
* @package Toolkit\ObjUtil
@@ -38,14 +36,14 @@ public static function smartConfigure($object, array $options)
3836
public static function init($object, array $options)
3937
{
4038
foreach ($options as $property => $value) {
41-
if (is_numeric($property)) {
39+
if (\is_numeric($property)) {
4240
continue;
4341
}
4442

45-
$setter = 'set' . ucfirst($property);
43+
$setter = 'set' . \ucfirst($property);
4644

4745
// has setter
48-
if (method_exists($object, $setter)) {
46+
if (\method_exists($object, $setter)) {
4947
$object->$setter($value);
5048
} else {
5149
$object->$property = $value;
@@ -84,7 +82,7 @@ public static function setAttrs($object, array $options)
8482
*/
8583
public static function encode($obj): string
8684
{
87-
return base64_encode(gzcompress(serialize($obj)));
85+
return \base64_encode(\gzcompress(\serialize($obj)));
8886
}
8987

9088
/**
@@ -95,7 +93,7 @@ public static function encode($obj): string
9593
*/
9694
public static function decode(string $txt, $allowedClasses = false)
9795
{
98-
return unserialize(gzuncompress(base64_decode($txt)), ['allowed_classes' => $allowedClasses]);
96+
return \unserialize(\gzuncompress(\base64_decode($txt)), ['allowed_classes' => $allowedClasses]);
9997
}
10098

10199
/**
@@ -111,8 +109,8 @@ public static function toArray($data, bool $recursive = false)
111109
// Ensure the input data is an array.
112110
if (\is_object($data)) {
113111
if ($data instanceof \Traversable) {
114-
$arr = iterator_to_array($data);
115-
} elseif (method_exists($data, 'toArray')) {
112+
$arr = \iterator_to_array($data);
113+
} elseif (\method_exists($data, 'toArray')) {
116114
$arr = $data->toArray();
117115
}
118116
} else {
@@ -138,17 +136,17 @@ public static function toArray($data, bool $recursive = false)
138136
public static function hash($object, $unique = true): string
139137
{
140138
if (\is_object($object)) {
141-
$hash = spl_object_hash($object);
139+
$hash = \spl_object_hash($object);
142140

143141
if ($unique) {
144-
$hash = md5($hash);
142+
$hash = \md5($hash);
145143
}
146144

147145
return $hash;
148146
}
149147

150148
// a class
151-
return \is_string($object) ? md5($object) : '';
149+
return \is_string($object) ? \md5($object) : '';
152150
}
153151

154152
/**
@@ -157,7 +155,7 @@ public static function hash($object, $unique = true): string
157155
* @param \ReflectionMethod $method Method for which to build the argument array.
158156
* @param array $extraArgs
159157
* @return array
160-
* @throws DependencyResolutionException
158+
* @throws \RuntimeException
161159
*/
162160
public static function getMethodArgs(\ReflectionMethod $method, array $extraArgs = []): array
163161
{
@@ -191,7 +189,7 @@ public static function getMethodArgs(\ReflectionMethod $method, array $extraArgs
191189

192190
// $dependencyVarName = $param->getName();
193191
// Couldn't resolve dependency, and no default was provided.
194-
throw new DependencyResolutionException(sprintf(
192+
throw new \RuntimeException(sprintf(
195193
'Could not resolve dependency: %s for the %dth parameter',
196194
$param->getPosition(),
197195
$param->getName()
@@ -205,8 +203,8 @@ public static function getMethodArgs(\ReflectionMethod $method, array $extraArgs
205203
* 从类名创建服务实例对象,会尽可能自动补完构造函数依赖
206204
* @from windWalker https://github.com/ventoviro/windwalker
207205
* @param string $class a className
208-
* @throws DependencyResolutionException
209206
* @return mixed
207+
* @throws \RuntimeException
210208
*/
211209
public static function create(string $class)
212210
{

src/ObjectStorage.php

Lines changed: 0 additions & 70 deletions
This file was deleted.

src/Traits/ArrayAccessByGetterSetterTrait.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,27 @@ public function offsetExists($offset): bool
3737
*/
3838
public function offsetGet($offset)
3939
{
40-
return $this->$offset;
40+
$getter = 'get' . ucfirst($offset);
41+
42+
if (\method_exists($this, $getter)) {
43+
$this->$getter();
44+
}
45+
46+
return null;
4147
}
4248

4349
/**
4450
* Sets an offset in the iterator.
4551
* @param mixed $offset The array offset.
4652
* @param mixed $value The array value.
47-
* @return void
4853
*/
4954
public function offsetSet($offset, $value)
5055
{
51-
$this->$offset = $value;
56+
$setter = 'set' . ucfirst($offset);
57+
58+
if (\method_exists($this, $setter)) {
59+
$this->$setter($value);
60+
}
5261
}
5362

5463
/**

src/Traits/PropertyAccessByGetterSetterTrait.php

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
namespace Toolkit\ObjUtil\Traits;
1010

11-
use Inhere\Exceptions\GetPropertyException;
12-
use Inhere\Exceptions\NotFoundException;
13-
use Inhere\Exceptions\SetPropertyException;
11+
use Toolkit\ObjUtil\Exception\GetPropertyException;
12+
use Toolkit\ObjUtil\Exception\PropertyException;
13+
use Toolkit\ObjUtil\Exception\SetPropertyException;
1414

1515
/**
1616
* trait PropertyAccessByGetterSetterTrait
@@ -25,74 +25,75 @@
2525
trait PropertyAccessByGetterSetterTrait
2626
{
2727
/**
28+
* @reference yii2 yii\base\Object::__set()
2829
* @param $name
29-
* @return bool
30+
* @param $value
31+
* @throws SetPropertyException
3032
*/
31-
public function __isset($name)
33+
public function __set($name, $value)
3234
{
33-
$getter = 'get' . ucfirst($name);
35+
$setter = 'set' . ucfirst($name);
3436

35-
if (method_exists($this, $getter)) {
36-
return $this->$getter() !== null;
37+
if (\method_exists($this, $setter)) {
38+
$this->$setter($value);
39+
} elseif (\method_exists($this, 'get' . ucfirst($name))) {
40+
throw new SetPropertyException('Setting a Read-only property! ' . \get_class($this) . "::{$name}");
41+
} else {
42+
throw new SetPropertyException('Setting a Unknown property! ' . \get_class($this) . "::{$name}");
3743
}
38-
39-
return false;
4044
}
4145

4246
/**
47+
* @reference yii2 yii\base\Object::__set()
4348
* @param $name
44-
* @throws NotFoundException
49+
* @throws GetPropertyException
50+
* @return mixed
4551
*/
46-
public function __unset($name)
52+
public function __get($name)
4753
{
48-
$setter = 'set' . ucfirst($name);
54+
$getter = 'get' . ucfirst($name);
4955

50-
if (method_exists($this, $setter)) {
51-
$this->$setter(null);
56+
if (\method_exists($this, $getter)) {
57+
return $this->$getter();
58+
}
5259

53-
return;
60+
if (\method_exists($this, 'set' . ucfirst($name))) {
61+
throw new GetPropertyException('Getting a Write-only property! ' . \get_class($this) . "::{$name}");
5462
}
5563

56-
throw new NotFoundException('Unset an unknown or read-only property: ' . \get_class($this) . '::' . $name);
64+
throw new GetPropertyException('Getting a Unknown property! ' . \get_class($this) . "::{$name}");
5765
}
5866

5967
/**
60-
* @reference yii2 yii\base\Object::__set()
6168
* @param $name
62-
* @param $value
63-
* @throws SetPropertyException
69+
* @return bool
6470
*/
65-
public function __set($name, $value)
71+
public function __isset($name)
6672
{
67-
$method = 'set' . ucfirst($name);
73+
$getter = 'get' . ucfirst($name);
6874

69-
if (method_exists($this, $method)) {
70-
$this->$method($value);
71-
} elseif (method_exists($this, 'get' . ucfirst($name))) {
72-
throw new SetPropertyException('Setting a Read-only property! ' . \get_class($this) . "::{$name}");
73-
} else {
74-
throw new SetPropertyException('Setting a Unknown property! ' . \get_class($this) . "::{$name}");
75+
if (\method_exists($this, $getter)) {
76+
return $this->$getter() !== null;
7577
}
78+
79+
return false;
7680
}
7781

7882
/**
79-
* @reference yii2 yii\base\Object::__set()
8083
* @param $name
81-
* @throws GetPropertyException
82-
* @return mixed
84+
* @throws \Toolkit\ObjUtil\Exception\PropertyException
8385
*/
84-
public function __get($name)
86+
public function __unset($name)
8587
{
86-
$method = 'get' . ucfirst($name);
88+
$setter = 'set' . ucfirst($name);
8789

88-
if (method_exists($this, $method)) {
89-
return $this->$method();
90-
}
90+
if (\method_exists($this, $setter)) {
91+
$this->$setter(null);
9192

92-
if (method_exists($this, 'set' . ucfirst($name))) {
93-
throw new GetPropertyException('Getting a Write-only property! ' . \get_class($this) . "::{$name}");
93+
return;
9494
}
9595

96-
throw new GetPropertyException('Getting a Unknown property! ' . \get_class($this) . "::{$name}");
96+
throw new PropertyException('Unset an unknown or read-only property: ' . \get_class($this) . '::' . $name);
9797
}
98+
9899
}

src/Traits/StdObjectTrait.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Toolkit\ObjUtil\Traits;
1010

11-
use Inhere\Exceptions\UnknownCalledException;
1211
use Toolkit\ObjUtil\Obj;
1312

1413
/**
@@ -34,9 +33,9 @@ final public static function fullName(): string
3433
final public static function spaceName(string $fullName = null): string
3534
{
3635
$fullName = $fullName ?: self::fullName();
37-
$fullName = str_replace('\\', '/', $fullName);
36+
$fullName = \str_replace('\\', '/', $fullName);
3837

39-
return strpos($fullName, '/') ? \dirname($fullName) : null;
38+
return \strpos($fullName, '/') ? \dirname($fullName) : null;
4039
}
4140

4241
/**
@@ -47,9 +46,9 @@ final public static function spaceName(string $fullName = null): string
4746
final public static function className(string $fullName = null): string
4847
{
4948
$fullName = $fullName ?: self::fullName();
50-
$fullName = str_replace('\\', '/', $fullName);
49+
$fullName = \str_replace('\\', '/', $fullName);
5150

52-
return basename($fullName);
51+
return \basename($fullName);
5352
}
5453

5554
/**
@@ -58,7 +57,7 @@ final public static function className(string $fullName = null): string
5857
*/
5958
public function __construct(array $config = [])
6059
{
61-
Obj::configure($this, $config);
60+
Obj::init($this, $config);
6261

6362
$this->init();
6463
}

0 commit comments

Comments
 (0)