Skip to content

Commit 20029a5

Browse files
committed
Psalm Issue Fixes
Signed-off-by: Raj Mohan <[email protected]>
1 parent d213fa2 commit 20029a5

17 files changed

+64
-10
lines changed

src/AbstractServer.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,18 @@ protected function buildCallback(Reflection\AbstractFunction $reflection)
5454
{
5555
$callback = new Method\Callback();
5656
if ($reflection instanceof Reflection\ReflectionMethod) {
57+
/** @var string $declaringClass */
58+
$declaringClass = $reflection->getDeclaringClass()->getName();
59+
/** @var string $methodName */
60+
$methodName = $reflection->getName();
5761
$callback->setType($reflection->isStatic() ? 'static' : 'instance')
58-
->setClass($reflection->getDeclaringClass()->getName())
59-
->setMethod($reflection->getName());
62+
->setClass($declaringClass)
63+
->setMethod($methodName);
6064
} elseif ($reflection instanceof Reflection\ReflectionFunction) {
65+
/** @var string $functionName */
66+
$functionName = $reflection->getName();
6167
$callback->setType('function')
62-
->setFunction($reflection->getName());
68+
->setFunction($functionName);
6369
}
6470
return $callback;
6571
}

src/Cache.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
/**
2828
* \Laminas\Server\Cache: cache server definitions
29+
* @psalm-suppress DeprecatedInterface
2930
*/
3031
class Cache
3132
{
@@ -44,9 +45,9 @@ class Cache
4445
* @param string $filename
4546
* @return bool
4647
*/
47-
public static function save($filename, Server $server)
48+
public static function save(string $filename, Server $server): bool
4849
{
49-
if (! is_string($filename) || (! file_exists($filename) && ! is_writable(dirname($filename)))) {
50+
if ((! file_exists($filename) && ! is_writable(dirname($filename)))) {
5051
return false;
5152
}
5253

@@ -152,10 +153,13 @@ private static function createDefinition($methods)
152153
private static function createDefinitionFromMethodsDefinition(Definition $methods)
153154
{
154155
$definition = new Definition();
156+
/** @psalm-suppress MixedAssignment */
155157
foreach ($methods as $method) {
158+
/** @psalm-suppress MixedMethodCall */
156159
if (in_array($method->getName(), static::$skipMethods, true)) {
157160
continue;
158161
}
162+
/** @psalm-suppress MixedArgument */
159163
$definition->addMethod($method);
160164
}
161165
return $definition;

src/Definition.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
/**
2525
* Server methods metadata
26+
* @psalm-suppress MissingTemplateParam
2627
*/
2728
class Definition implements Countable, Iterator
2829
{
@@ -133,9 +134,9 @@ public function hasMethod($method)
133134
* Get a given method definition
134135
*
135136
* @param string $method
136-
* @return null|\Laminas\Server\Method\Definition
137+
* @return mixed
137138
*/
138-
public function getMethod($method)
139+
public function getMethod($method): mixed
139140
{
140141
if ($this->hasMethod($method)) {
141142
return $this->methods[$method];
@@ -182,13 +183,15 @@ public function clearMethods()
182183
* Cast definition to an array
183184
*
184185
* @return array
186+
* @psalm-suppress MixedReturnTypeCoercion
185187
*/
186188
public function toArray()
187189
{
188190
$methods = [];
189191
foreach ($this->getMethods() as $key => $method) {
190192
$methods[$key] = $method->toArray();
191193
}
194+
/** @psalm-suppress MixedReturnTypeCoercion */
192195
return $methods;
193196
}
194197

@@ -206,11 +209,12 @@ public function count()
206209
/**
207210
* Iterator: current item
208211
*
209-
* @return Method\Definition
212+
* @return
210213
*/
211214
#[ReturnTypeWillChange]
212215
public function current()
213216
{
217+
/** @psalm-suppress MixedReturnStatement */
214218
return current($this->methods);
215219
}
216220

@@ -233,6 +237,7 @@ public function key()
233237
#[ReturnTypeWillChange]
234238
public function next()
235239
{
240+
/** @psalm-suppress MixedReturnStatement */
236241
return next($this->methods);
237242
}
238243

src/Method/Callback.php

+2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ public function setOptions(array $options)
7272
public function setClass($class)
7373
{
7474
if (is_object($class)) {
75+
/** @psalm-suppress MixedAssignment */
7576
$class = $class::class;
7677
}
78+
/** @psalm-suppress MixedAssignment */
7779
$this->class = $class;
7880
return $this;
7981
}

src/Method/Definition.php

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ public function getMethodHelp()
203203
*/
204204
public function setObject($object)
205205
{
206+
/** @psalm-suppress RedundantCondition */
206207
if (! is_object($object) && (null !== $object)) {
207208
throw new Server\Exception\InvalidArgumentException(sprintf(
208209
'Invalid object passed to %s',

src/Method/Prototype.php

+2
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,15 @@ public function getParameterObjects()
143143
*/
144144
public function getParameter($index)
145145
{
146+
/** @psalm-suppress DocblockTypeContradiction */
146147
if (! is_string($index) && ! is_numeric($index)) {
147148
return null;
148149
}
149150
if (array_key_exists($index, $this->parameterNameMap)) {
150151
$index = $this->parameterNameMap[$index];
151152
}
152153
if (array_key_exists($index, $this->parameters)) {
154+
/** @psalm-suppress MixedReturnStatement */
153155
return $this->parameters[$index];
154156
}
155157
return null;

src/Reflection.php

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static function reflectClass($class, $argv = false, $namespace = '')
5151
throw new InvalidArgumentException('Invalid class or object passed to attachClass()');
5252
}
5353

54+
/** @psalm-suppress RiskyTruthyFalsyComparison */
5455
if ($argv && ! is_array($argv)) {
5556
throw new InvalidArgumentException('Invalid argv argument passed to reflectClass');
5657
}

src/Reflection/AbstractFunction.php

+8
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ protected function buildSignatures($return, $returnDesc, $paramTypes, $paramDesc
247247
$return = new ReflectionReturnValue(array_shift($signature), $this->returnDesc);
248248
$tmp = [];
249249
foreach ($signature as $key => $type) {
250+
/** @psalm-suppress PossiblyNullArgument*/
250251
$param = new ReflectionParameter(
251252
$params[$key],
252253
$type,
@@ -280,6 +281,7 @@ protected function reflect()
280281
$this->docComment = $function->getDocComment();
281282
}
282283

284+
/** @psalm-suppress RiskyTruthyFalsyComparison*/
283285
$scanner = new DocBlockReflection($this->docComment ? : '/***/');
284286
$helpText = $scanner->getLongDescription();
285287
/** @var ParamTag[] $paramTags */
@@ -319,6 +321,7 @@ protected function reflect()
319321
$paramDesc = [];
320322
foreach ($paramTags as $paramTag) {
321323
$paramTypesTmp[] = $paramTag->getTypes();
324+
/** @psalm-suppress RiskyTruthyFalsyComparison*/
322325
$paramDesc[] = $paramTag->getDescription() ? : '';
323326
}
324327
}
@@ -340,6 +343,7 @@ protected function reflect()
340343

341344
$paramTypes = [];
342345
foreach ($paramTypesTmp as $i => $param) {
346+
/** @psalm-suppress MixedMethodCall, InvalidArrayOffset */
343347
if ($parameters[$i]->isOptional()) {
344348
array_unshift($param, null);
345349
}
@@ -408,6 +412,7 @@ public function setNamespace($namespace)
408412
return;
409413
}
410414

415+
/** @psalm-suppress TypeDoesNotContainType */
411416
if (! is_string($namespace) || ! preg_match('/[a-z0-9_\.]+/i', $namespace)) {
412417
throw new Exception\InvalidArgumentException('Invalid namespace');
413418
}
@@ -459,6 +464,7 @@ public function getDescription()
459464
*/
460465
public function getPrototypes()
461466
{
467+
/** @psalm-suppress MixedReturnTypeCoercion */
462468
return $this->prototypes;
463469
}
464470

@@ -503,9 +509,11 @@ public function __sleep()
503509
public function __wakeup()
504510
{
505511
if ($this->reflection instanceof PhpReflectionMethod) {
512+
/** @psalm-suppress ArgumentTypeCoercion */
506513
$class = new PhpReflectionClass($this->class);
507514
$this->reflection = new PhpReflectionMethod($class->newInstance(), $this->name);
508515
} else {
516+
/** @psalm-suppress ArgumentTypeCoercion */
509517
$this->reflection = new PhpReflectionFunction($this->name);
510518
}
511519
}

src/Reflection/Node.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function __construct(
4646
$this->setParent($parent, true);
4747
}
4848

49+
/** @psalm-suppress InvalidReturnStatement */
4950
return $this;
5051
}
5152

@@ -103,7 +104,7 @@ public function attachChild(Node $node)
103104
*
104105
* @return array
105106
*/
106-
public function getChildren()
107+
public function getChildren(): array
107108
{
108109
return $this->children;
109110
}
@@ -158,7 +159,7 @@ public function setValue($value)
158159
*
159160
* @return array
160161
*/
161-
public function getEndPoints()
162+
public function getEndPoints(): array
162163
{
163164
$endPoints = [];
164165
if (! $this->hasChildren()) {

src/Reflection/Prototype.php

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Prototype
3232
*/
3333
public function __construct(ReflectionReturnValue $return, array $params = [])
3434
{
35+
/** @psalm-suppress DeprecatedProperty */
3536
$this->return = $return;
3637

3738
foreach ($params as $param) {
@@ -50,6 +51,7 @@ public function __construct(ReflectionReturnValue $return, array $params = [])
5051
*/
5152
public function getReturnType()
5253
{
54+
/** @psalm-suppress DeprecatedProperty */
5355
return $this->return->getType();
5456
}
5557

@@ -60,6 +62,7 @@ public function getReturnType()
6062
*/
6163
public function getReturnValue()
6264
{
65+
/** @psalm-suppress DeprecatedProperty */
6366
return $this->return;
6467
}
6568

src/Reflection/ReflectionClass.php

+2
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public function setNamespace($namespace)
169169
return;
170170
}
171171

172+
/** @psalm-suppress TypeDoesNotContainType */
172173
if (! is_string($namespace) || ! preg_match('/[a-z0-9_\.]+/i', $namespace)) {
173174
throw new Exception\InvalidArgumentException('Invalid namespace');
174175
}
@@ -186,6 +187,7 @@ public function setNamespace($namespace)
186187
*/
187188
public function __wakeup()
188189
{
190+
/** @psalm-suppress ArgumentTypeCoercion */
189191
$this->reflection = new PhpReflectionClass($this->name);
190192
}
191193

src/Reflection/ReflectionMethod.php

+8
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function __construct(ReflectionClass $class, \ReflectionMethod $r, $names
5252
$classNamespace = $class->getNamespace();
5353

5454
// Determine namespace
55+
/** @psalm-suppress RiskyTruthyFalsyComparison */
5556
if (! empty($namespace)) {
5657
$this->setNamespace($namespace);
5758
} elseif (! empty($classNamespace)) {
@@ -62,6 +63,7 @@ public function __construct(ReflectionClass $class, \ReflectionMethod $r, $names
6263
$this->argv = $argv;
6364

6465
// If method call, need to store some info on the class
66+
/** @psalm-suppress MixedAssignment */
6567
$this->class = $class->getName();
6668
$this->name = $r->getName();
6769

@@ -89,11 +91,13 @@ public function getDeclaringClass()
8991
*/
9092
public function __wakeup()
9193
{
94+
/** @psalm-suppress ArgumentTypeCoercion */
9295
$this->classReflection = new ReflectionClass(
9396
new \ReflectionClass($this->class),
9497
$this->getNamespace(),
9598
$this->getInvokeArguments()
9699
);
100+
/** @psalm-suppress MixedArgument */
97101
$this->reflection = new \ReflectionMethod($this->classReflection->getName(), $this->name);
98102
}
99103

@@ -123,7 +127,9 @@ private function fetchRecursiveDocComment()
123127
$docCommentList[] = $this->reflection->getDocComment();
124128

125129
// fetch all doc blocks for method from parent classes
130+
/** @psalm-suppress PossiblyInvalidArgument */
126131
$docCommentFetched = $this->fetchRecursiveDocBlockFromParent($this->classReflection, $currentMethodName);
132+
/** @psalm-suppress RiskyTruthyFalsyComparison */
127133
if ($docCommentFetched) {
128134
$docCommentList = array_merge($docCommentList, $docCommentFetched);
129135
}
@@ -148,6 +154,7 @@ function ($docComment) {
148154
$docCommentList
149155
);
150156

157+
/** @psalm-suppress InvalidArgument */
151158
return '/**' . implode(PHP_EOL, $normalizedDocCommentList) . '*/';
152159
}
153160

@@ -174,6 +181,7 @@ private function fetchRecursiveDocBlockFromParent($reflectionClass, $methodName)
174181
$docCommentLast = $methodReflection->getDocComment();
175182
$docComment[] = $docCommentLast;
176183
if ($this->isInherit($docCommentLast)) {
184+
/** @psalm-suppress RiskyTruthyFalsyComparison */
177185
if ($docCommentFetched = $this->fetchRecursiveDocBlockFromParent($parentReflectionClass, $methodName)) {
178186
$docComment = array_merge($docComment, $docCommentFetched);
179187
}

src/Reflection/ReflectionParameter.php

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function __construct(\ReflectionParameter $r, $type = 'mixed', $descripti
6767

6868
// Store parameters needed for (un)serialization
6969
$this->name = $r->getName();
70+
/** @psalm-suppress PossiblyInvalidPropertyAssignmentValue */
7071
$this->functionName = $r->getDeclaringClass()
7172
? [$r->getDeclaringClass()->getName(), $r->getDeclaringFunction()->getName()]
7273
: $r->getDeclaringFunction()->getName();
@@ -111,6 +112,7 @@ public function getType()
111112
*/
112113
public function setType($type)
113114
{
115+
/** @psalm-suppress DocblockTypeContradiction */
114116
if (! is_string($type) && (null !== $type)) {
115117
throw new Exception\InvalidArgumentException('Invalid parameter type');
116118
}
@@ -137,6 +139,7 @@ public function getDescription()
137139
*/
138140
public function setDescription($description)
139141
{
142+
/** @psalm-suppress DocblockTypeContradiction */
140143
if (! is_string($description) && (null !== $description)) {
141144
throw new Exception\InvalidArgumentException('Invalid parameter description');
142145
}

src/Reflection/ReflectionReturnValue.php

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function getType()
6060
*/
6161
public function setType($type)
6262
{
63+
/** @psalm-suppress DocblockTypeContradiction */
6364
if (! is_string($type) && (null !== $type)) {
6465
throw new Exception\InvalidArgumentException('Invalid parameter type');
6566
}
@@ -86,6 +87,7 @@ public function getDescription()
8687
*/
8788
public function setDescription($description)
8889
{
90+
/** @psalm-suppress DocblockTypeContradiction */
8991
if (! is_string($description) && (null !== $description)) {
9092
throw new Exception\InvalidArgumentException('Invalid parameter description');
9193
}

test/DefinitionTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public function testDefinitionShouldSerializeToArray(): void
133133
$this->assertEquals($method['name'], $test['name']);
134134
$this->assertEquals($method['methodHelp'], $test['methodHelp']);
135135
$this->assertEquals($method['invokeArguments'], $test['invokeArguments']);
136+
/** @psalm-suppress MixedArrayAccess */
136137
$this->assertEquals($method['prototypes'][0]['returnType'], $test['prototypes'][0]['returnType']);
137138
}
138139

@@ -161,6 +162,7 @@ public function testPassingOptionsToConstructorShouldSetObjectState(): void
161162
$this->assertEquals($method['name'], $test['name']);
162163
$this->assertEquals($method['methodHelp'], $test['methodHelp']);
163164
$this->assertEquals($method['invokeArguments'], $test['invokeArguments']);
165+
/** @psalm-suppress MixedArrayAccess */
164166
$this->assertEquals($method['prototypes'][0]['returnType'], $test['prototypes'][0]['returnType']);
165167
}
166168
}

0 commit comments

Comments
 (0)