1
0
Fork 0

PHPStan level 6 in src/Composer/Json (#10172)

pull/10173/head
Fabien Villepinte 2021-10-16 22:42:32 +02:00 committed by GitHub
parent 04c157bf32
commit 9fde6ec88b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 90 additions and 3 deletions

View File

@ -115,9 +115,10 @@ class JsonFile
/**
* Writes json file.
*
* @param array $hash writes hash into json file
* @param mixed[] $hash writes hash into json file
* @param int $options json_encode options (defaults to JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
* @throws \UnexpectedValueException|\Exception
* @return void
*/
public function write(array $hash, $options = 448)
{
@ -158,7 +159,11 @@ class JsonFile
}
/**
* modify file properties only if content modified
* Modify file properties only if content modified
*
* @param string $path
* @param string $content
* @return int|false
*/
private function filePutContentsIfModified($path, $content)
{
@ -268,6 +273,7 @@ class JsonFile
*
* @param int $code return code of json_last_error function
* @throws \RuntimeException
* @return void
*/
private static function throwEncodeError($code)
{

View File

@ -37,6 +37,9 @@ class JsonManipulator
/** @var string */
private $indent;
/**
* @param string $contents
*/
public function __construct($contents)
{
$contents = trim($contents);
@ -51,11 +54,21 @@ class JsonManipulator
$this->detectIndenting();
}
/**
* @return string
*/
public function getContents()
{
return $this->contents . $this->newline;
}
/**
* @param string $type
* @param string $package
* @param string $constraint
* @param bool $sortPackages
* @return bool
*/
public function addLink($type, $package, $constraint, $sortPackages = false)
{
$decoded = JsonFile::parseJson($this->contents);
@ -116,7 +129,8 @@ class JsonManipulator
*
* @link https://getcomposer.org/doc/02-libraries.md#platform-packages
*
* @param array $packages
* @param array<string> $packages
* @return void
*/
private function sortPackages(array &$packages = array())
{
@ -149,26 +163,50 @@ class JsonManipulator
});
}
/**
* @param string $name
* @param array<string, mixed> $config
* @param bool $append
* @return bool
*/
public function addRepository($name, $config, $append = true)
{
return $this->addSubNode('repositories', $name, $config, $append);
}
/**
* @param string $name
* @return bool
*/
public function removeRepository($name)
{
return $this->removeSubNode('repositories', $name);
}
/**
* @param string $name
* @param mixed $value
* @return bool
*/
public function addConfigSetting($name, $value)
{
return $this->addSubNode('config', $name, $value);
}
/**
* @param string $name
* @return bool
*/
public function removeConfigSetting($name)
{
return $this->removeSubNode('config', $name);
}
/**
* @param string $name
* @param mixed $value
* @return bool
*/
public function addProperty($name, $value)
{
if (strpos($name, 'suggest.') === 0) {
@ -186,6 +224,10 @@ class JsonManipulator
return $this->addMainKey($name, $value);
}
/**
* @param string $name
* @return bool
*/
public function removeProperty($name)
{
if (strpos($name, 'suggest.') === 0) {
@ -203,6 +245,13 @@ class JsonManipulator
return $this->removeMainKey($name);
}
/**
* @param string $mainNode
* @param string $name
* @param mixed $value
* @param bool $append
* @return bool
*/
public function addSubNode($mainNode, $name, $value, $append = true)
{
$decoded = JsonFile::parseJson($this->contents);
@ -310,6 +359,11 @@ class JsonManipulator
return true;
}
/**
* @param string $mainNode
* @param string $name
* @return bool
*/
public function removeSubNode($mainNode, $name)
{
$decoded = JsonFile::parseJson($this->contents);
@ -411,6 +465,11 @@ class JsonManipulator
return true;
}
/**
* @param string $key
* @param mixed $content
* @return bool
*/
public function addMainKey($key, $content)
{
$decoded = JsonFile::parseJson($this->contents);
@ -451,6 +510,10 @@ class JsonManipulator
return true;
}
/**
* @param string $key
* @return bool
*/
public function removeMainKey($key)
{
$decoded = JsonFile::parseJson($this->contents);
@ -484,6 +547,10 @@ class JsonManipulator
return false;
}
/**
* @param string $key
* @return bool
*/
public function removeMainKeyIfEmpty($key)
{
$decoded = JsonFile::parseJson($this->contents);
@ -499,6 +566,11 @@ class JsonManipulator
return true;
}
/**
* @param mixed $data
* @param int $depth
* @return string
*/
public function format($data, $depth = 0)
{
if (is_array($data)) {
@ -524,6 +596,9 @@ class JsonManipulator
return JsonFile::encode($data);
}
/**
* @return void
*/
protected function detectIndenting()
{
if ($this->pregMatch('{^([ \t]+)"}m', $this->contents, $match)) {
@ -533,6 +608,12 @@ class JsonManipulator
}
}
/**
* @param string $re
* @param string $str
* @param array<string> $matches
* @return int
*/
protected function pregMatch($re, $str, &$matches = array())
{
$count = preg_match($re, $str, $matches);