Skip to content

Commit 511f727

Browse files
authored
add psalm lints and php hints (#62)
1 parent 53686a7 commit 511f727

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+772
-686
lines changed

.circleci/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ jobs:
3232
- run:
3333
name: install dependencies
3434
command: composer install --no-progress
35+
- run:
36+
name: psalm linting
37+
command: ./vendor/bin/psalm --no-cache
3538
- run: mkdir -p ~/phpunit
3639
- run:
3740
name: run tests with highest compatible dependency versions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ composer.phar
77
integration-tests/vendor
88
composer.lock
99
docs/build
10+
.phpunit.result.cache

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"guzzlehttp/guzzle": "^7",
2424
"kevinrob/guzzle-cache-middleware": "^3",
2525
"phpunit/phpunit": "^9",
26-
"phpunit/php-code-coverage": "^9"
26+
"phpunit/php-code-coverage": "^9",
27+
"vimeo/psalm": "^4.7"
2728
},
2829
"suggest": {
2930
"guzzlehttp/guzzle": "(^7) Required when using GuzzleEventPublisher or the default FeatureRequester",

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0"?>
2-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" beStrictAboutResourceUsageDuringSmallTests="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutTodoAnnotatedTests="true" verbose="true">
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" beStrictAboutChangesToGlobalState="true" beStrictAboutOutputDuringTests="true" beStrictAboutResourceUsageDuringSmallTests="true" beStrictAboutTestsThatDoNotTestAnything="true" beStrictAboutTodoAnnotatedTests="true" verbose="true" defaultTimeLimit="10">
33

44
<logging>
55
<junit outputFile="~/phpunit/junit.xml"/>

psalm.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="4"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
>
9+
<projectFiles>
10+
<directory name="src" />
11+
<ignoreFiles>
12+
<directory name="vendor" />
13+
</ignoreFiles>
14+
</projectFiles>
15+
</psalm>

src/LaunchDarkly/Clause.php

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,38 @@
1212
*/
1313
class Clause
1414
{
15-
/** @var string */
15+
/** @var string|null */
1616
private $_attribute = null;
17-
/** @var string */
17+
/** @var string|null */
1818
private $_op = null;
1919
/** @var array */
2020
private $_values = array();
2121
/** @var bool */
2222
private $_negate = false;
2323

24-
private function __construct($attribute, $op, array $values, $negate)
24+
private function __construct(?string $attribute, ?string $op, array $values, bool $negate)
2525
{
2626
$this->_attribute = $attribute;
2727
$this->_op = $op;
2828
$this->_values = $values;
2929
$this->_negate = $negate;
3030
}
3131

32-
public static function getDecoder()
32+
/**
33+
* @psalm-return \Closure(mixed):self
34+
*/
35+
public static function getDecoder(): \Closure
3336
{
3437
return function ($v) {
3538
return new Clause($v['attribute'], $v['op'], $v['values'], $v['negate']);
3639
};
3740
}
3841

39-
/**
40-
* @param $user LDUser
41-
* @return bool
42-
*/
43-
public function matchesUser($user, $featureRequester)
42+
public function matchesUser(LDUser $user, ?FeatureRequester $featureRequester): bool
4443
{
4544
if ($this->_op === 'segmentMatch') {
4645
foreach ($this->_values as $value) {
47-
$segment = $featureRequester->getSegment($value);
46+
$segment = $featureRequester ? $featureRequester->getSegment($value) : null;
4847
if ($segment) {
4948
if ($segment->matchesUser($user)) {
5049
return $this->_maybeNegate(true);
@@ -57,11 +56,7 @@ public function matchesUser($user, $featureRequester)
5756
}
5857
}
5958

60-
/**
61-
* @param $user LDUser
62-
* @return bool
63-
*/
64-
public function matchesUserNoSegments($user)
59+
public function matchesUserNoSegments(LDUser $user): bool
6560
{
6661
$userValue = $user->getValueForEvaluation($this->_attribute);
6762
if ($userValue === null) {
@@ -79,44 +74,32 @@ public function matchesUserNoSegments($user)
7974
}
8075
}
8176

82-
83-
/**
84-
* @return string
85-
*/
86-
public function getAttribute()
77+
public function getAttribute(): ?string
8778
{
8879
return $this->_attribute;
8980
}
9081

91-
/**
92-
* @return string
93-
*/
94-
public function getOp()
82+
public function getOp(): ?string
9583
{
9684
return $this->_op;
9785
}
9886

99-
/**
100-
* @return array
101-
*/
102-
public function getValues()
87+
public function getValues(): array
10388
{
10489
return $this->_values;
10590
}
10691

107-
/**
108-
* @return boolean
109-
*/
110-
public function isNegate()
92+
public function isNegate(): bool
11193
{
11294
return $this->_negate;
11395
}
11496

11597
/**
116-
* @param $userValue
98+
* @param mixed|null $userValue
99+
*
117100
* @return bool
118101
*/
119-
private function matchAny($userValue)
102+
private function matchAny($userValue): bool
120103
{
121104
foreach ($this->_values as $v) {
122105
$result = Operators::apply($this->_op, $userValue, $v);
@@ -127,7 +110,7 @@ private function matchAny($userValue)
127110
return false;
128111
}
129112

130-
private function _maybeNegate($b)
113+
private function _maybeNegate(bool $b): bool
131114
{
132115
if ($this->_negate) {
133116
return !$b;

src/LaunchDarkly/EvalResult.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,22 @@
1111
class EvalResult
1212
{
1313
/** @var EvaluationDetail */
14-
private $_detail = null;
14+
private $_detail;
1515
/** @var array */
1616
private $_prerequisiteEvents = [];
1717

18-
/**
19-
* EvalResult constructor.
20-
* @param EvaluationDetail $detail
21-
* @param array $prerequisiteEvents
22-
*/
23-
public function __construct($detail, array $prerequisiteEvents)
18+
public function __construct(EvaluationDetail $detail, array $prerequisiteEvents)
2419
{
2520
$this->_detail = $detail;
2621
$this->_prerequisiteEvents = $prerequisiteEvents;
2722
}
2823

29-
/**
30-
* @return EvaluationDetail
31-
*/
32-
public function getDetail()
24+
public function getDetail(): EvaluationDetail
3325
{
3426
return $this->_detail;
3527
}
3628

37-
/**
38-
* @return array
39-
*/
40-
public function getPrerequisiteEvents()
29+
public function getPrerequisiteEvents(): array
4130
{
4231
return $this->_prerequisiteEvents;
4332
}

src/LaunchDarkly/EvaluationDetail.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@
22

33
namespace LaunchDarkly;
44

5+
use \LaunchDarkly\EvaluationReason;
6+
57
/**
68
* An object that combines the result of a flag evaluation with an explanation of how it was calculated.
79
*
810
* This is returned by {@link \LaunchDarkly\LDClient::variationDetail()}.
911
*/
1012
class EvaluationDetail
1113
{
14+
/** @var int|null */
1215
private $_variationIndex = null;
16+
17+
/** @var mixed|null */
1318
private $_value = null;
14-
private $_reason = null;
19+
20+
/** @var EvaluationReason */
21+
private $_reason;
1522

1623
/**
1724
* EvaluationDetail constructor.
18-
* @param mixed $value the value of the flag variation
25+
* @param mixed|null $value the value of the flag variation
1926
* @param int|null $variationIndex the index of the flag variation, or null if it was the default value
2027
* @param EvaluationReason $reason evaluation reason properties
2128
*/
22-
public function __construct($value, $variationIndex, $reason = null)
29+
public function __construct($value, ?int $variationIndex, EvaluationReason $reason)
2330
{
2431
$this->_value = $value;
2532
$this->_variationIndex = $variationIndex;
@@ -42,7 +49,7 @@ public function getValue()
4249
*
4350
* @return int | null
4451
*/
45-
public function getVariationIndex()
52+
public function getVariationIndex(): ?int
4653
{
4754
return $this->_variationIndex;
4855
}
@@ -52,7 +59,7 @@ public function getVariationIndex()
5259
*
5360
* @return EvaluationReason
5461
*/
55-
public function getReason()
62+
public function getReason(): EvaluationReason
5663
{
5764
return $this->_reason;
5865
}
@@ -62,7 +69,7 @@ public function getReason()
6269
*
6370
* @return bool
6471
*/
65-
public function isDefaultValue()
72+
public function isDefaultValue(): bool
6673
{
6774
return ($this->_variationIndex === null);
6875
}

0 commit comments

Comments
 (0)