From 2a3a57c5195ffe3094ef6c6862ce0b73e7cd1fc3 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 7 Oct 2021 20:53:15 +0100 Subject: [PATCH] Enable parsing/emitting .properties without creating a Config object this is useful when the contents are just going to get passed straight into a model, making Config object redundant anyway. --- src/utils/Config.php | 22 ++++++++++--------- .../check-explicit-mixed-baseline.neon | 5 +++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/utils/Config.php b/src/utils/Config.php index db0b8c464..9449f768d 100644 --- a/src/utils/Config.php +++ b/src/utils/Config.php @@ -170,7 +170,7 @@ class Config{ $config = null; switch($this->type){ case Config::PROPERTIES: - $config = $this->parseProperties($content); + $config = self::parseProperties($content); break; case Config::JSON: $config = json_decode($content, true); @@ -211,7 +211,7 @@ class Config{ $content = null; switch($this->type){ case Config::PROPERTIES: - $content = $this->writeProperties(); + $content = self::writeProperties($this->config); break; case Config::JSON: $content = json_encode($this->config, $this->jsonOptions); @@ -524,9 +524,13 @@ class Config{ return $result; } - private function writeProperties() : string{ + /** + * @param string[]|int[]|float[]|bool[] $config + * @phpstan-param array $config + */ + public static function writeProperties(array $config) : string{ $content = "#Properties Config file\r\n#" . date("D M j H:i:s T Y") . "\r\n"; - foreach($this->config as $k => $v){ + foreach($config as $k => $v){ if(is_bool($v)){ $v = $v ? "on" : "off"; } @@ -537,9 +541,10 @@ class Config{ } /** - * @return mixed[] + * @return string[]|int[]|float[]|bool[] + * @phpstan-return array */ - private function parseProperties(string $content) : array{ + public static function parseProperties(string $content) : array{ $result = []; if(preg_match_all('/^\s*([a-zA-Z0-9\-_\.]+)[ \t]*=([^\r\n]*)/um', $content, $matches) > 0){ //false or 0 matches foreach($matches[1] as $i => $k){ @@ -563,10 +568,7 @@ class Config{ }; break; } - if(isset($result[$k])){ - \GlobalLogger::get()->debug("[Config] Repeated property " . $k . " on file " . $this->file); - } - $result[$k] = $v; + $result[(string) $k] = $v; } } diff --git a/tests/phpstan/configs/check-explicit-mixed-baseline.neon b/tests/phpstan/configs/check-explicit-mixed-baseline.neon index cb0893173..a13bb3a1b 100644 --- a/tests/phpstan/configs/check-explicit-mixed-baseline.neon +++ b/tests/phpstan/configs/check-explicit-mixed-baseline.neon @@ -165,6 +165,11 @@ parameters: count: 1 path: ../../../src/timings/TimingsHandler.php + - + message: "#^Parameter \\#1 \\$config of static method pocketmine\\\\utils\\\\Config\\:\\:writeProperties\\(\\) expects array\\, array\\ given\\.$#" + count: 1 + path: ../../../src/utils/Config.php + - message: "#^Parameter \\#2 \\$offset of function substr expects int, mixed given\\.$#" count: 1