Skip to content

Commit cc9dd78

Browse files
oakbanimikeproeng37
authored andcommitted
ci: Enforce Static Code Analyzer (#155)
1 parent c0ad1db commit cc9dd78

31 files changed

+459
-385
lines changed

Diff for: .travis.yml

+11-6
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,34 @@ php:
66
- '7.1'
77
- '7.2'
88
- '7.3'
9-
before_install:
10-
- composer install --dev
9+
install: "composer install"
1110
addons:
1211
srcclr: true
1312
script:
1413
- mkdir -p build/logs
1514
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
16-
after_script:
17-
- vendor/bin/php-coveralls -v
15+
after_script: "vendor/bin/php-coveralls -v"
1816

19-
# Integration tests need to run first to reset the PR build status to pending
17+
# Linting and integration tests need to run first to reset the PR build status to pending
2018
stages:
19+
- 'Linting'
2120
- 'Integration tests'
2221
- 'Test'
2322

2423
jobs:
2524
include:
25+
- stage: 'Linting'
26+
language: php
27+
php: '7.0'
28+
install: 'composer require "squizlabs/php_codesniffer=*"'
29+
script: 'composer lint'
30+
after_script: skip
31+
after_success: travis_terminate 0
2632
- stage: 'Integration tests'
2733
merge_mode: replace
2834
env: SDK=php
2935
cache: false
3036
language: python
31-
before_install: skip
3237
install:
3338
- "pip install awscli"
3439
before_script:

Diff for: CONTRIBUTING.md

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ We welcome contributions and feedback! All contributors must sign our [Contribut
1717

1818
* **All code must have test coverage.** We use PHPUnit. Changes in functionality should have accompanying unit tests. Bug fixes should have accompanying regression tests.
1919
* Tests are located in `tests` with one file per class.
20+
* Lint your code with PHP CodeSniffer before submitting.
21+
22+
## Style
23+
We enforce [PSR-2](https://www.php-fig.org/psr/psr-2/) rules with some minor [deviations](phpcs.xml). Run linter by executing `composer lint` and autocorrect lint errors by executing `composer beautify`.
24+
2025

2126
## License
2227

Diff for: composer.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
"email": "[email protected]"
1010
}
1111
],
12+
"scripts": {
13+
"test": "phpunit",
14+
"lint": "phpcs",
15+
"beautify": "phpcbf"
16+
},
1217
"require": {
1318
"php": ">=5.5",
1419
"justinrainbow/json-schema": "^1.6 || ^2.0 || ^4.0 || ^5.0",
@@ -19,7 +24,8 @@
1924
},
2025
"require-dev": {
2126
"phpunit/phpunit": "^4.8|^5.0",
22-
"php-coveralls/php-coveralls": "v2.0.0"
27+
"php-coveralls/php-coveralls": "v2.0.0",
28+
"squizlabs/php_codesniffer": "3.*"
2329
},
2430
"autoload": {
2531
"psr-4": {

Diff for: phpcs.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="OptimizelyPSR2">
3+
<!-- description for custom ruleset -->
4+
<description>A custom coding standard for Optimizely PHP-SDK based on PSR2</description>
5+
6+
<!-- If no files or directories are specified on commandline, these files will be sniffed -->
7+
<file>./src</file>
8+
<file>./tests</file>
9+
10+
<!-- Exclude patterns for files to be excluded from sniffing -->
11+
<!-- <exclude-pattern>./tests/EventTests/*</exclude-pattern> -->
12+
13+
<!-- Embed command line arguments in config file. -->
14+
<arg name="tab-width" value="4"/>
15+
16+
<!-- To exclude any rule sniff, get sniff name by running phpcs with -s switch -->
17+
<rule ref="PSR2">
18+
<exclude name="Generic.Files.LineLength.TooLong"/>
19+
<exclude name="PSR2.Classes.PropertyDeclaration.Underscore"/>
20+
<exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses"/>
21+
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols"/>
22+
</rule >
23+
</ruleset>

Diff for: src/Optimizely/Bucketer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2018, Optimizely
3+
* Copyright 2016-2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -90,9 +90,9 @@ protected function generateBucketValue($bucketingKey)
9090

9191
/* murmurhash3_int returns both positive and negative integers for PHP x86 versions
9292
it returns negative integers when it tries to create 2^32 integers while PHP doesn't support
93-
unsigned integers and can store integers only upto 2^31.
93+
unsigned integers and can store integers only upto 2^31.
9494
Observing generated hashcodes and their corresponding bucket values after normalization
95-
indicates that a negative bucket number on x86 is exactly 10,000 less than it's
95+
indicates that a negative bucket number on x86 is exactly 10,000 less than it's
9696
corresponding bucket number on x64. Hence we can safely add 10,000 to a negative number to
9797
make it consistent across both of the PHP variants. */
9898

Diff for: src/Optimizely/DecisionService/DecisionService.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2017-2018, Optimizely Inc and Contributors
3+
* Copyright 2017-2019, Optimizely Inc and Contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -93,8 +93,8 @@ protected function getBucketingId($userId, $userAttributes)
9393
{
9494
$bucketingIdKey = ControlAttributes::BUCKETING_ID;
9595

96-
if (isset($userAttributes[$bucketingIdKey])){
97-
if (is_string($userAttributes[$bucketingIdKey])){
96+
if (isset($userAttributes[$bucketingIdKey])) {
97+
if (is_string($userAttributes[$bucketingIdKey])) {
9898
return $userAttributes[$bucketingIdKey];
9999
}
100100
$this->_logger->log(Logger::WARNING, 'Bucketing ID attribute is not a string. Defaulted to user ID.');
@@ -308,7 +308,7 @@ public function getVariationForFeatureRollout(FeatureFlag $featureFlag, $userId,
308308
break;
309309
}
310310
// Evaluate Everyone Else Rule / Last Rule now
311-
$experiment = $rolloutRules[sizeof($rolloutRules)-1];
311+
$experiment = $rolloutRules[sizeof($rolloutRules)-1];
312312

313313
// Evaluate if user meets the audience condition of Everyone Else Rule / Last Rule now
314314
if (!Validator::isUserInExperiment($this->_projectConfig, $experiment, $userAttributes)) {

Diff for: src/Optimizely/Entity/Experiment.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016, 2018, Optimizely
3+
* Copyright 2016, 2018-2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -78,7 +78,7 @@ class Experiment
7878

7979
/**
8080
* @var array/string Single audience ID the experiment is attached to or
81-
* hierarchical conditions array of audience IDs related by AND/OR/NOT operators.
81+
* hierarchical conditions array of audience IDs related by AND/OR/NOT operators.
8282
*/
8383
private $_audienceConditions;
8484

Diff for: src/Optimizely/Entity/FeatureVariable.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2017, Optimizely
3+
* Copyright 2017, 2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -61,7 +61,7 @@ class FeatureVariable
6161
private $_defaultValue;
6262

6363

64-
public function __construct($id =null, $key = null, $type = null, $defaultValue = null)
64+
public function __construct($id = null, $key = null, $type = null, $defaultValue = null)
6565
{
6666
$this->_id = $id;
6767
$this->_key = $key;

Diff for: src/Optimizely/Entity/Variation.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016-2018, Optimizely
3+
* Copyright 2016-2019, Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -115,7 +115,7 @@ public function getVariables()
115115
}
116116

117117
/**
118-
* @param string Variable ID
118+
* @param string Variable ID
119119
*
120120
* @return VariableUsage Variable usage instance corresponding to given variable ID
121121
*/

Diff for: src/Optimizely/Event/Builder/EventBuilder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private function getCommonParams($config, $userId, $attributes)
100100
ENRICH_DECISIONS => true
101101
];
102102

103-
if(!is_null($attributes)) {
103+
if (!is_null($attributes)) {
104104
foreach ($attributes as $attributeKey => $attributeValue) {
105105
// Omit attributes that are not supported by the log endpoint.
106106
if (Validator::isAttributeValid($attributeKey, $attributeValue)) {
@@ -199,7 +199,7 @@ private function getConversionParams($eventEntity, $eventTags)
199199
$eventDict[EventTagUtils::NUMERIC_EVENT_METRIC_NAME] = $eventValue;
200200
}
201201

202-
if(count($eventTags) > 0) {
202+
if (count($eventTags) > 0) {
203203
$eventDict['tags'] = $eventTags;
204204
}
205205
}

Diff for: src/Optimizely/Event/Builder/Params.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
define('DECISIONS', 'decisions');
2727
define('ENRICH_DECISIONS', 'enrich_decisions');
2828
define('ENTITY_ID', 'entity_id');
29-
define('EVENTS', 'events');;
29+
define('EVENTS', 'events');
3030
define('EXPERIMENT_ID', 'experiment_id');
3131
define('KEY', 'key');
3232
define('PROJECT_ID', 'project_id');

Diff for: src/Optimizely/Logger/DefaultLogger.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2016,2018 Optimizely
3+
* Copyright 2016, 2018-2019 Optimizely
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -35,8 +35,8 @@ class DefaultLogger implements LoggerInterface
3535
/**
3636
* DefaultLogger constructor.
3737
*
38-
* @param int $minLevel Minimum level of messages to be logged.
39-
* @param string $stream The PHP stream to log output.
38+
* @param int $minLevel Minimum level of messages to be logged.
39+
* @param string $stream The PHP stream to log output.
4040
*/
4141
public function __construct($minLevel = Logger::INFO, $stream = "stdout")
4242
{

Diff for: src/Optimizely/Notification/NotificationCenter.php

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Copyright 2017-2018, Optimizely Inc and Contributors
3+
* Copyright 2017-2019, Optimizely Inc and Contributors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -58,7 +58,7 @@ public function getNotifications()
5858
/**
5959
* Adds a notification callback for a notification type to the notification center
6060
*
61-
* @param string $notification_type One of the constants defined in NotificationType
61+
* @param string $notification_type One of the constants defined in NotificationType
6262
* @param callable $notification_callback A valid PHP callback
6363
*
6464
* @return null Given invalid notification type/callback
@@ -122,12 +122,13 @@ public function removeNotificationListener($notification_id)
122122
*
123123
* @deprecated Use 'clearNotificationListeners' instead.
124124
*/
125-
public function clearNotifications($notification_type){
126-
$this->_logger->log(
127-
Logger::WARNING,
128-
"'clearNotifications' is deprecated. Call 'clearNotificationListeners' instead."
129-
);
130-
$this->clearNotificationListeners($notification_type);
125+
public function clearNotifications($notification_type)
126+
{
127+
$this->_logger->log(
128+
Logger::WARNING,
129+
"'clearNotifications' is deprecated. Call 'clearNotificationListeners' instead."
130+
);
131+
$this->clearNotificationListeners($notification_type);
131132
}
132133

133134
/**
@@ -152,12 +153,13 @@ public function clearNotificationListeners($notification_type)
152153
*
153154
* @deprecated Use 'clearAllNotificationListeners' instead.
154155
*/
155-
public function cleanAllNotifications(){
156-
$this->_logger->log(
157-
Logger::WARNING,
158-
"'cleanAllNotifications' is deprecated. Call 'clearAllNotificationListeners' instead."
159-
);
160-
$this->clearAllNotificationListeners();
156+
public function cleanAllNotifications()
157+
{
158+
$this->_logger->log(
159+
Logger::WARNING,
160+
"'cleanAllNotifications' is deprecated. Call 'clearAllNotificationListeners' instead."
161+
);
162+
$this->clearAllNotificationListeners();
161163
}
162164

163165
/**

Diff for: src/Optimizely/Optimizely.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public function activate($experimentKey, $userId, $attributes = null)
265265
self::EXPERIMENT_KEY =>$experimentKey,
266266
self::USER_ID => $userId
267267
]
268-
)
268+
)
269269
) {
270270
return null;
271271
}
@@ -301,7 +301,7 @@ public function track($eventKey, $userId, $attributes = null, $eventTags = null)
301301
self::EVENT_KEY =>$eventKey,
302302
self::USER_ID => $userId
303303
]
304-
)
304+
)
305305
) {
306306
return null;
307307
}
@@ -389,7 +389,7 @@ public function getVariation($experimentKey, $userId, $attributes = null)
389389
self::EXPERIMENT_KEY =>$experimentKey,
390390
self::USER_ID => $userId
391391
]
392-
)
392+
)
393393
) {
394394
return null;
395395
}
@@ -484,7 +484,7 @@ public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null)
484484
self::FEATURE_FLAG_KEY =>$featureFlagKey,
485485
self::USER_ID => $userId
486486
]
487-
)
487+
)
488488
) {
489489
return false;
490490
}
@@ -539,7 +539,7 @@ public function getEnabledFeatures($userId, $attributes = null)
539539
[
540540
self::USER_ID => $userId
541541
]
542-
)
542+
)
543543
) {
544544
return $enabledFeatureKeys;
545545
}
@@ -584,7 +584,7 @@ public function getFeatureVariableValueForType(
584584
self::VARIABLE_KEY => $variableKey,
585585
self::USER_ID => $userId
586586
]
587-
)
587+
)
588588
) {
589589
return null;
590590
}

Diff for: src/Optimizely/ProjectConfig.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public function __construct($datafile, $logger, $errorHandler)
196196
$this->_logger = $logger;
197197
$this->_errorHandler = $errorHandler;
198198
$this->_version = $config['version'];
199-
if(!in_array($this->_version, $supportedVersions)){
199+
if (!in_array($this->_version, $supportedVersions)) {
200200
throw new InvalidDatafileVersionException(
201201
"This version of the PHP SDK does not support the given datafile version: {$this->_version}."
202202
);
@@ -256,7 +256,7 @@ public function __construct($datafile, $logger, $errorHandler)
256256
}
257257

258258
// Conditions in typedAudiences are not expected to be string-encoded so they don't need
259-
// to be decoded unlike audiences.
259+
// to be decoded unlike audiences.
260260
foreach (array_values($typedAudienceIdMap) as $typedAudience) {
261261
$typedAudience->setConditionsList($typedAudience->getConditions());
262262
}
@@ -402,7 +402,7 @@ public function getExperimentFromId($experimentId)
402402
}
403403

404404
/**
405-
* @param String $featureKey Key of the feature flag
405+
* @param String $featureKey Key of the feature flag
406406
*
407407
* @return FeatureFlag Entity corresponding to the key.
408408
*/
@@ -418,7 +418,7 @@ public function getFeatureFlagFromKey($featureKey)
418418
}
419419

420420
/**
421-
* @param String $rolloutId
421+
* @param String $rolloutId
422422
*
423423
* @return Rollout
424424
*/
@@ -577,8 +577,6 @@ public function getFeatureVariableFromKey($featureFlagKey, $variableKey)
577577

578578
$this->_logger->log(
579579
Logger::ERROR,
580-
581-
582580
sprintf(
583581
'No variable key "%s" defined in datafile for feature flag "%s".',
584582
$variableKey,

0 commit comments

Comments
 (0)