diff --git a/src/Collections/DeltaConfigCollection.php b/src/Collections/DeltaConfigCollection.php index d381f37..dcbeb0d 100644 --- a/src/Collections/DeltaConfigCollection.php +++ b/src/Collections/DeltaConfigCollection.php @@ -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] : []; @@ -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; @@ -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]); } ); @@ -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] = []; } diff --git a/src/Collections/MemoryConfigCollection.php b/src/Collections/MemoryConfigCollection.php index 6f56c45..82859f6 100644 --- a/src/Collections/MemoryConfigCollection.php +++ b/src/Collections/MemoryConfigCollection.php @@ -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] = []; @@ -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] : []; } @@ -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 { @@ -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; }); } @@ -289,7 +289,7 @@ public function serialize() */ public function unserialize($serialized) { - $data = unserialize($serialized); + $data = unserialize($serialized ?? ''); $this->__unserialize($data); } @@ -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] = []; diff --git a/src/MergeStrategy/Priority.php b/src/MergeStrategy/Priority.php index 163fe74..6557981 100644 --- a/src/MergeStrategy/Priority.php +++ b/src/MergeStrategy/Priority.php @@ -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 { diff --git a/src/Middleware/DeltaMiddleware.php b/src/Middleware/DeltaMiddleware.php index 16b3283..cf5ddc8 100644 --- a/src/Middleware/DeltaMiddleware.php +++ b/src/Middleware/DeltaMiddleware.php @@ -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']); } diff --git a/src/Middleware/MiddlewareAware.php b/src/Middleware/MiddlewareAware.php index 8c4cb28..412b6bb 100644 --- a/src/Middleware/MiddlewareAware.php +++ b/src/Middleware/MiddlewareAware.php @@ -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); }; diff --git a/src/Middleware/MiddlewareCommon.php b/src/Middleware/MiddlewareCommon.php index bd47e4c..b30bc7e 100644 --- a/src/Middleware/MiddlewareCommon.php +++ b/src/Middleware/MiddlewareCommon.php @@ -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() ?? [])); } /** @@ -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; diff --git a/src/Transformer/PrivateStaticTransformer.php b/src/Transformer/PrivateStaticTransformer.php index 5899a33..a9fa09e 100644 --- a/src/Transformer/PrivateStaticTransformer.php +++ b/src/Transformer/PrivateStaticTransformer.php @@ -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; } @@ -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; diff --git a/src/Transformer/YamlTransformer.php b/src/Transformer/YamlTransformer.php index b64683d..10ec831 100644 --- a/src/Transformer/YamlTransformer.php +++ b/src/Transformer/YamlTransformer.php @@ -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; } @@ -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]); } @@ -168,7 +168,7 @@ protected function hasRule($rule) */ public function ignoreRule($rule) { - $rule = strtolower($rule); + $rule = strtolower($rule ?? ''); $this->ignoreRules[$rule] = $rule; } @@ -181,7 +181,7 @@ public function ignoreRule($rule) */ protected function isRuleIgnored($rule) { - $rule = strtolower($rule); + $rule = strtolower($rule ?? ''); return isset($this->ignoreRules[$rule]); } @@ -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']); @@ -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'; @@ -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]]; } @@ -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 !== '*'; } @@ -428,11 +428,11 @@ 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 @@ -440,9 +440,9 @@ function ($document) use ($flag) { '[^\.][a-zA-Z0-9\-_\/\.]+', array_map( function ($part) { - return preg_quote($part, '%'); + return preg_quote($part ?? '', '%'); }, - explode('*', trim($pattern, '/\\')) + explode('*', trim($pattern ?? '', '/\\')) ) ).'([./\\\\]|$)%'; @@ -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 ?? '')) { if (!empty($documentName) && $documentName !== $document['header']['name']) { // If we're looking for a specific document. If not found we can continue continue; @@ -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); } /** @@ -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)); } diff --git a/tests/Transformer/PrivateStaticTransformerTest.php b/tests/Transformer/PrivateStaticTransformerTest.php index 1755add..09db7cc 100644 --- a/tests/Transformer/PrivateStaticTransformerTest.php +++ b/tests/Transformer/PrivateStaticTransformerTest.php @@ -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.'); } diff --git a/tests/Transformer/YamlTransformerTest.php b/tests/Transformer/YamlTransformerTest.php index b8bd587..42606eb 100644 --- a/tests/Transformer/YamlTransformerTest.php +++ b/tests/Transformer/YamlTransformerTest.php @@ -65,8 +65,8 @@ protected function getConfigDirectory() $dir = $this->root->url().'/'.$this->directory; // Create the directory if it doesn't exist. - if (!is_dir($dir)) { - mkdir($dir); + if (!is_dir($dir ?? '')) { + mkdir($dir ?? ''); } return $dir; @@ -77,7 +77,7 @@ protected function getConfigDirectory() */ public function testEmptyFileIgnored() { - file_put_contents($this->getFilePath('empty.yml'), ''); + file_put_contents($this->getFilePath('empty.yml') ?? '', ''); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -107,7 +107,7 @@ public function testEmptyDocumentIgnored() third: thirdValue YAML; - file_put_contents($this->getFilePath('config2.yml'), $content); + file_put_contents($this->getFilePath('config2.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -137,7 +137,7 @@ public function testNoHeader() YAML; $file = $this->getFilePath('config.yml'); - file_put_contents($file, $content); + file_put_contents($file ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -172,7 +172,7 @@ public function testNoNameStatement() --- Test: blah YAML; - file_put_contents($this->getFilePath('config.yml'), $content); + file_put_contents($this->getFilePath('config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -196,7 +196,7 @@ public function testBeforeAndAfterStatements() --- test: 'blah' YAML; - file_put_contents($this->getFilePath('first.yml'), $content); + file_put_contents($this->getFilePath('first.yml') ?? '', $content); $content = <<<'YAML' --- @@ -205,7 +205,7 @@ public function testBeforeAndAfterStatements() --- test: 'overwritten' YAML; - file_put_contents($this->getFilePath('second.yml'), $content); + file_put_contents($this->getFilePath('second.yml') ?? '', $content); $content = <<<'YAML' --- @@ -214,7 +214,7 @@ public function testBeforeAndAfterStatements() --- test: 'set first' YAML; - file_put_contents($this->getFilePath('zzz.yml'), $content); + file_put_contents($this->getFilePath('zzz.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -244,7 +244,7 @@ public function testBeforeAfterStatementsByHash() --- test: 'overwritten' YAML; - file_put_contents($this->getFilePath('first.yml'), $content); + file_put_contents($this->getFilePath('first.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -268,7 +268,7 @@ public function testBeforeAfterStatementWithPath() test: 'test' YAML; mkdir($this->getConfigDirectory().'/test'); - file_put_contents($this->getFilePath('test/config.yml'), $content); + file_put_contents($this->getFilePath('test/config.yml') ?? '', $content); $content = <<<'YAML' --- @@ -278,7 +278,7 @@ public function testBeforeAfterStatementWithPath() test: 'should not overwrite' YAML; mkdir($this->getConfigDirectory().'/test2'); - file_put_contents($this->getFilePath('test2/config.yml'), $content); + file_put_contents($this->getFilePath('test2/config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -297,7 +297,7 @@ public function testBeforeAfterStatementWithPath() --- test: 'overwrite' YAML; - file_put_contents($this->getFilePath('test2/config.yml'), $content); + file_put_contents($this->getFilePath('test2/config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -315,7 +315,7 @@ public function testBeforeAfterStatementWithPath() --- test: 'anything else' YAML; - file_put_contents($this->getFilePath('test2/config.yml'), $content); + file_put_contents($this->getFilePath('test2/config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -333,7 +333,7 @@ public function testBeforeAfterStatementWithPath() --- test: 'another thing' YAML; - file_put_contents($this->getFilePath('test2/config.yml'), $content); + file_put_contents($this->getFilePath('test2/config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -355,7 +355,7 @@ public function testBeforeAfterStatementWithNestedPath() YAML; mkdir($this->getConfigDirectory().'/test'); mkdir($this->getConfigDirectory().'/test/test1-1'); - file_put_contents($this->getFilePath('test/test1-1/config.yml'), $content); + file_put_contents($this->getFilePath('test/test1-1/config.yml') ?? '', $content); $content = <<<'YAML' --- @@ -365,7 +365,7 @@ public function testBeforeAfterStatementWithNestedPath() test: 'should not overwrite' YAML; mkdir($this->getConfigDirectory().'/test2'); - file_put_contents($this->getFilePath('test2/config.yml'), $content); + file_put_contents($this->getFilePath('test2/config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -383,7 +383,7 @@ public function testBeforeAfterStatementWithNestedPath() --- test: 'overwrite' YAML; - file_put_contents($this->getFilePath('test2/config.yml'), $content); + file_put_contents($this->getFilePath('test2/config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -401,7 +401,7 @@ public function testBeforeAfterStatementWithNestedPath() --- test: 'this will not overwrite' YAML; - file_put_contents($this->getFilePath('test2/config.yml'), $content); + file_put_contents($this->getFilePath('test2/config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -419,7 +419,7 @@ public function testBeforeAfterStatementWithNestedPath() --- test: 'this will overwrite' YAML; - file_put_contents($this->getFilePath('test2/config.yml'), $content); + file_put_contents($this->getFilePath('test2/config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -451,7 +451,7 @@ public function testCircularDependency() --- test: test YAML; - file_put_contents($this->getFilePath('config.yml'), $content); + file_put_contents($this->getFilePath('config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $transformer = new YamlTransformer( @@ -490,7 +490,7 @@ public function testSingleOnlyExceptStatements() --- test: 'not applied' YAML; - file_put_contents($this->getFilePath('config.yml'), $content); + file_put_contents($this->getFilePath('config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $yaml = new YamlTransformer( @@ -537,7 +537,7 @@ public function testMultipleOnlyExceptStatements() --- test: 'not applied' YAML; - file_put_contents($this->getFilePath('config.yml'), $content); + file_put_contents($this->getFilePath('config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $yaml = new YamlTransformer( @@ -647,7 +647,7 @@ public function testFailedOnlyExceptStatements() test2: 'test2-only-error' YAML; - file_put_contents($this->getFilePath('config.yml'), $content); + file_put_contents($this->getFilePath('config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $yaml = new YamlTransformer( @@ -687,7 +687,7 @@ public function testIgnoredOnlyExceptRule() --- test2: 'test2' YAML; - file_put_contents($this->getFilePath('config.yml'), $content); + file_put_contents($this->getFilePath('config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $yaml = new YamlTransformer( @@ -740,7 +740,7 @@ public function testKeyValueOnlyExceptStatements() --- arrays: passed YAML; - file_put_contents($this->getFilePath('config.yml'), $content); + file_put_contents($this->getFilePath('config.yml') ?? '', $content); $collection = new MemoryConfigCollection; $yaml = new YamlTransformer(