Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH PHP 8.1 compatibility #56

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Collections/DeltaConfigCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function getMiddlewares()
*/
public function getDeltas($class)
{
$classKey = strtolower($class);
$classKey = strtolower($class ?? '');
return isset($this->deltas[$classKey])
? $this->deltas[$classKey]
: [];
Expand Down Expand Up @@ -239,7 +239,7 @@ protected function clearDeltas($class = null, $key = null)
}

// Clear just one class
$classKey = strtolower($class);
$classKey = strtolower($class ?? '');
if (!$key) {
unset($this->deltas[$classKey]);
return;
Expand All @@ -250,13 +250,13 @@ protected function clearDeltas($class = null, $key = null)
return;
}
$this->deltas[$classKey] = array_filter(
$this->deltas[$classKey],
$this->deltas[$classKey] ?? [],
function ($delta) use ($key) {
// Clear if an array with exactly one element, with the key
// being the affected config property
return !isset($delta['config'])
|| !is_array($delta['config'])
|| (count($delta['config']) !== 1)
|| (count($delta['config'] ?? []) !== 1)
|| !isset($delta['config'][$key]);
}
);
Expand All @@ -270,7 +270,7 @@ function ($delta) use ($key) {
*/
protected function addDelta($class, $delta)
{
$classKey = strtolower($class);
$classKey = strtolower($class ?? '');
if (!isset($this->deltas[$classKey])) {
$this->deltas[$classKey] = [];
}
Expand Down
14 changes: 7 additions & 7 deletions src/Collections/MemoryConfigCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function set($class, $name, $data, $metadata = [])
{
$this->saveMetadata($class, $metadata);

$classKey = strtolower($class);
$classKey = strtolower($class ?? '');
if ($name) {
if (!isset($this->config[$classKey])) {
$this->config[$classKey] = [];
Expand Down Expand Up @@ -122,7 +122,7 @@ public function get($class, $name = null, $excludeMiddleware = 0)
protected function getClassConfig($class, $excludeMiddleware = 0)
{
// `true` excludes all middleware, so bypass call cache
$classKey = strtolower($class);
$classKey = strtolower($class ?? '');
if ($excludeMiddleware === true) {
return isset($this->config[$classKey]) ? $this->config[$classKey] : [];
}
Expand Down Expand Up @@ -156,14 +156,14 @@ public function exists($class, $name = null, $excludeMiddleware = 0)
return false;
}
if ($name) {
return array_key_exists($name, $config);
return array_key_exists($name, $config ?? []);
}
return true;
}

public function remove($class, $name = null)
{
$classKey = strtolower($class);
$classKey = strtolower($class ?? '');
if ($name) {
unset($this->config[$classKey][$name]);
} else {
Expand Down Expand Up @@ -248,7 +248,7 @@ protected function getSerializedMembers()
{
return array_filter(array_keys(get_object_vars($this)), function ($key) {
// Skip $_underscoreProp
return strpos($key, '_') !== 0;
return strpos($key ?? '', '_') !== 0;
});
}

Expand Down Expand Up @@ -289,7 +289,7 @@ public function serialize()
*/
public function unserialize($serialized)
{
$data = unserialize($serialized);
$data = unserialize($serialized ?? '');
$this->__unserialize($data);
}

Expand All @@ -310,7 +310,7 @@ protected function saveMetadata($class, $metadata)
return;
}

$classKey = strtolower($class);
$classKey = strtolower($class ?? '');
if (isset($this->metadata[$classKey]) && isset($this->config[$classKey])) {
if (!isset($this->history[$classKey])) {
$this->history[$classKey] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/MergeStrategy/Priority.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static function mergeArray(array $highPriority, array $lowPriority)
}

// If not set, or we're changing type we can set low priority
if (is_int($key) || !array_key_exists($key, $lowPriority) || !is_array($lowPriority[$key])) {
if (is_int($key) || !array_key_exists($key, $lowPriority ?? []) || !is_array($lowPriority[$key])) {
if (is_int($key)) {
$lowPriority[] = $value;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/DeltaMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected function applyDelta($config, $delta)
case DeltaConfigCollection::REPLACE:
return $delta['config'];
case DeltaConfigCollection::REMOVE:
return array_diff_key($config, $delta['config']);
return array_diff_key($config ?? [], $delta['config']);
default:
throw new InvalidArgumentException("Invalid delta " . $delta['type']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/MiddlewareAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function callMiddleware($class, $excludeMiddleware, $last)
$next = $last;

/** @var Middleware $middleware */
foreach (array_reverse($this->getMiddlewares()) as $middleware) {
foreach (array_reverse($this->getMiddlewares() ?? []) as $middleware) {
$next = function ($class, $excludeMiddleware) use ($middleware, $next) {
return $middleware->getClassConfig($class, $excludeMiddleware, $next);
};
Expand Down
8 changes: 4 additions & 4 deletions src/Middleware/MiddlewareCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __unserialize(array $data): void
*/
public function serialize()
{
return json_encode(array_values($this->__serialize()));
return json_encode(array_values($this->__serialize() ?? []));
}

/**
Expand All @@ -86,9 +86,9 @@ public function serialize()
*/
public function unserialize($serialized)
{
$values = json_decode($serialized, true);
foreach (array_keys($this->__serialize()) as $i => $key) {
if (!property_exists($this, $key)) {
$values = json_decode($serialized ?? '', true);
foreach (array_keys($this->__serialize() ?? []) as $i => $key) {
if (!property_exists($this, $key ?? '')) {
continue;
}
$this->{$key} = $values[$i] ?? 0;
Expand Down
6 changes: 3 additions & 3 deletions src/Transformer/PrivateStaticTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function transform(MutableConfigCollectionInterface $collection)

foreach ($classes as $class) {
// Skip if the class doesn't exist
if (!class_exists($class)) {
if (!class_exists($class ?? '')) {
continue;
}

Expand Down Expand Up @@ -123,11 +123,11 @@ protected function isConfigProperty(ReflectionProperty $prop)
}
$annotations = $prop->getDocComment();
// Whitelist @config
if (strstr($annotations, '@config')) {
if (strstr($annotations ?? '', '@config')) {
return true;
}
// Don't treat @internal as config
if (strstr($annotations, '@internal')) {
if (strstr($annotations ?? '', '@internal')) {
return false;
}
return true;
Expand Down
40 changes: 20 additions & 20 deletions src/Transformer/YamlTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function transform(MutableConfigCollectionInterface $collection)
*/
public function addRule($rule, Closure $func)
{
$rule = strtolower($rule);
$rule = strtolower($rule ?? '');
$this->rules[$rule] = $func;
return $this;
}
Expand All @@ -154,7 +154,7 @@ public function addRule($rule, Closure $func)
*/
protected function hasRule($rule)
{
$rule = strtolower($rule);
$rule = strtolower($rule ?? '');
return isset($this->rules[$rule]);
}

Expand All @@ -168,7 +168,7 @@ protected function hasRule($rule)
*/
public function ignoreRule($rule)
{
$rule = strtolower($rule);
$rule = strtolower($rule ?? '');
$this->ignoreRules[$rule] = $rule;
}

Expand All @@ -181,7 +181,7 @@ public function ignoreRule($rule)
*/
protected function isRuleIgnored($rule)
{
$rule = strtolower($rule);
$rule = strtolower($rule ?? '');

return isset($this->ignoreRules[$rule]);
}
Expand All @@ -199,7 +199,7 @@ protected function getNamedYamlDocuments()
$documents = [];
foreach ($unnamed as $uniqueKey => $document) {
$header = YamlParser::parse($document['header']) ?: [];
$header = array_change_key_case($header, CASE_LOWER);
$header = array_change_key_case($header ?? [], CASE_LOWER);

$content = YamlParser::parse($document['content']);

Expand Down Expand Up @@ -248,7 +248,7 @@ protected function splitYamlDocuments()

// We need to loop through each file and parse the yaml content
foreach ($this->files as $file) {
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$lines = file($file ?? '', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

$firstLine = true;
$context = 'content';
Expand Down Expand Up @@ -398,8 +398,8 @@ protected function getMatchingDocuments($pattern, $documents, $flag)

// If the pattern starts with a hash, it means we're looking for a single document
// named without the hash.
if (strpos($pattern, '#') === 0) {
$name = substr($pattern, 1);
if (strpos($pattern ?? '', '#') === 0) {
$name = substr($pattern ?? '', 1);
if (isset($documents[$name])) {
return [$documents[$name]];
}
Expand All @@ -410,14 +410,14 @@ protected function getMatchingDocuments($pattern, $documents, $flag)
// and likewise for Before="*"
if ($pattern === '*') {
return array_filter(
$documents,
$documents ?? [],
function ($document) use ($flag) {
if (empty($document['header'][$flag])) {
return true;
}
$otherPatterns = $document['header'][$flag];
if (is_array($otherPatterns)) {
return !in_array('*', $otherPatterns);
return !in_array('*', $otherPatterns ?? []);
}
return $otherPatterns !== '*';
}
Expand All @@ -428,21 +428,21 @@ function ($document) use ($flag) {
// and check their filename and maybe their document name, depending on the pattern.
// We don't want to do any pattern matching after the first hash as the document name
// is assumed to follow it.
$firstHash = strpos($pattern, '#');
$firstHash = strpos($pattern ?? '', '#');
$documentName = false;
if ($firstHash !== false) {
$documentName = substr($pattern, $firstHash + 1);
$pattern = substr($pattern, 0, $firstHash);
$documentName = substr($pattern ?? '', $firstHash + 1);
$pattern = substr($pattern ?? '', 0, $firstHash);
}

// Replace all `*` with `[^\.][a-zA-Z0-9\-_\/\.]+`, and quote other characters
$patternRegExp = '%(^|[/\\\\])'.implode(
'[^\.][a-zA-Z0-9\-_\/\.]+',
array_map(
function ($part) {
return preg_quote($part, '%');
return preg_quote($part ?? '', '%');
},
explode('*', trim($pattern, '/\\'))
explode('*', trim($pattern ?? '', '/\\'))
)
).'([./\\\\]|$)%';

Expand All @@ -451,7 +451,7 @@ function ($part) {
// Ensure filename is relative
$filename = $this->makeRelative($document['filename']);

if (preg_match($patternRegExp, $filename)) {
if (preg_match($patternRegExp ?? '', $filename ?? '')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (preg_match($patternRegExp ?? '', $filename ?? '')) {
if (preg_match($patternRegExp ?? '//', $filename ?? '')) {

A warning is emitted if this is an empty string.

if (!empty($documentName) && $documentName !== $document['header']['name']) {
// If we're looking for a specific document. If not found we can continue
continue;
Expand All @@ -475,12 +475,12 @@ function ($part) {
*/
protected function makeRelative($filename)
{
$dir = substr($filename, 0, strlen($this->baseDirectory));
$dir = substr($filename ?? '', 0, strlen($this->baseDirectory ?? ''));
if ($dir == $this->baseDirectory) {
return trim(substr($filename, strlen($this->baseDirectory)), DIRECTORY_SEPARATOR);
return trim(substr($filename ?? '', strlen($this->baseDirectory ?? '')), DIRECTORY_SEPARATOR);
}

return trim($filename, DIRECTORY_SEPARATOR);
return trim($filename ?? '', DIRECTORY_SEPARATOR);
}

/**
Expand Down Expand Up @@ -577,7 +577,7 @@ protected function testRules($header, $flag)
*/
protected function testSingleRule($rule, $params, $flag = self::ONLY_FLAG)
{
$rule = strtolower($rule);
$rule = strtolower($rule ?? '');
if (!$this->hasRule($rule)) {
throw new Exception(sprintf('Rule \'%s\' doesn\'t exist.', $rule));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Transformer/PrivateStaticTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testMerge()
public function testInvalidClass()
{
$class = 'SomeNonExistentClass';
if (class_exists($class)) {
if (class_exists($class ?? '')) {
$this->markTestSkipped($class . ' exists but the test expects it not to.');
}

Expand Down
Loading