-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Understand types returned by Container::getParameter() and similar me…
…thods
- Loading branch information
1 parent
2dad4de
commit 4e4353e
Showing
21 changed files
with
1,020 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Symfony; | ||
|
||
use PhpParser\Node\Expr; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Type\TypeUtils; | ||
use function count; | ||
|
||
final class DefaultParameterMap implements ParameterMap | ||
{ | ||
|
||
/** @var \PHPStan\Symfony\ParameterDefinition[] */ | ||
private $parameters; | ||
|
||
/** | ||
* @param \PHPStan\Symfony\ParameterDefinition[] $parameters | ||
*/ | ||
public function __construct(array $parameters) | ||
{ | ||
$this->parameters = $parameters; | ||
} | ||
|
||
/** | ||
* @return \PHPStan\Symfony\ParameterDefinition[] | ||
*/ | ||
public function getParameters(): array | ||
{ | ||
return $this->parameters; | ||
} | ||
|
||
public function getParameter(string $key): ?ParameterDefinition | ||
{ | ||
return $this->parameters[$key] ?? null; | ||
} | ||
|
||
public static function getParameterKeyFromNode(Expr $node, Scope $scope): ?string | ||
{ | ||
$strings = TypeUtils::getConstantStrings($scope->getType($node)); | ||
return count($strings) === 1 ? $strings[0]->getValue() : null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Symfony; | ||
|
||
use PhpParser\Node\Expr; | ||
use PHPStan\Analyser\Scope; | ||
|
||
final class FakeParameterMap implements ParameterMap | ||
{ | ||
|
||
/** | ||
* @return \PHPStan\Symfony\ParameterDefinition[] | ||
*/ | ||
public function getParameters(): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function getParameter(string $key): ?ParameterDefinition | ||
{ | ||
return null; | ||
} | ||
|
||
public static function getParameterKeyFromNode(Expr $node, Scope $scope): ?string | ||
{ | ||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Symfony; | ||
|
||
final class Parameter implements ParameterDefinition | ||
{ | ||
|
||
/** @var string */ | ||
private $key; | ||
|
||
/** @var array<mixed>|bool|float|int|string */ | ||
private $value; | ||
|
||
/** | ||
* @param string $key | ||
* @param array<mixed>|bool|float|int|string $value | ||
*/ | ||
public function __construct( | ||
string $key, | ||
$value | ||
) | ||
{ | ||
$this->key = $key; | ||
$this->value = $value; | ||
} | ||
|
||
public function getKey(): string | ||
{ | ||
return $this->key; | ||
} | ||
|
||
/** | ||
* @return array<mixed>|bool|float|int|string | ||
*/ | ||
public function getValue() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Symfony; | ||
|
||
interface ParameterDefinition | ||
{ | ||
|
||
public function getKey(): string; | ||
|
||
/** | ||
* @return array<mixed>|bool|float|int|string | ||
*/ | ||
public function getValue(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Symfony; | ||
|
||
use PhpParser\Node\Expr; | ||
use PHPStan\Analyser\Scope; | ||
|
||
interface ParameterMap | ||
{ | ||
|
||
/** | ||
* @return \PHPStan\Symfony\ParameterDefinition[] | ||
*/ | ||
public function getParameters(): array; | ||
|
||
public function getParameter(string $key): ?ParameterDefinition; | ||
|
||
public static function getParameterKeyFromNode(Expr $node, Scope $scope): ?string; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Symfony; | ||
|
||
interface ParameterMapFactory | ||
{ | ||
|
||
public function create(): ParameterMap; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Symfony; | ||
|
||
use function sprintf; | ||
|
||
final class XmlParameterMapFactory implements ParameterMapFactory | ||
{ | ||
|
||
/** @var string|null */ | ||
private $containerXml; | ||
|
||
public function __construct(?string $containerXml) | ||
{ | ||
$this->containerXml = $containerXml; | ||
} | ||
|
||
public function create(): ParameterMap | ||
{ | ||
if ($this->containerXml === null) { | ||
return new FakeParameterMap(); | ||
} | ||
|
||
$fileContents = file_get_contents($this->containerXml); | ||
if ($fileContents === false) { | ||
throw new XmlContainerNotExistsException(sprintf('Container %s does not exist', $this->containerXml)); | ||
} | ||
|
||
$xml = @simplexml_load_string($fileContents); | ||
if ($xml === false) { | ||
throw new XmlContainerNotExistsException(sprintf('Container %s cannot be parsed', $this->containerXml)); | ||
} | ||
|
||
/** @var \PHPStan\Symfony\Parameter[] $parameters */ | ||
$parameters = []; | ||
foreach ($xml->parameters->parameter as $def) { | ||
/** @var \SimpleXMLElement $attrs */ | ||
$attrs = $def->attributes(); | ||
|
||
$parameter = new Parameter( | ||
(string) $attrs->key, | ||
$this->getNodeValue($def) | ||
); | ||
|
||
$parameters[$parameter->getKey()] = $parameter; | ||
} | ||
|
||
return new DefaultParameterMap($parameters); | ||
} | ||
|
||
/** | ||
* @return array<mixed>|bool|float|int|string | ||
*/ | ||
private function getNodeValue(\SimpleXMLElement $def) | ||
{ | ||
/** @var \SimpleXMLElement $attrs */ | ||
$attrs = $def->attributes(); | ||
|
||
$value = null; | ||
switch ((string) $attrs->type) { | ||
case 'collection': | ||
$value = []; | ||
foreach ($def->children() as $child) { | ||
/** @var \SimpleXMLElement $childAttrs */ | ||
$childAttrs = $child->attributes(); | ||
|
||
if (isset($childAttrs->key)) { | ||
$value[(string) $childAttrs->key] = $this->getNodeValue($child); | ||
} else { | ||
$value[] = $this->getNodeValue($child); | ||
} | ||
} | ||
break; | ||
|
||
case 'string': | ||
$value = (string) $def; | ||
break; | ||
|
||
case 'binary': | ||
$value = base64_decode((string) $def, true); | ||
if ($value === false) { | ||
throw new \InvalidArgumentException(sprintf('Parameter "%s" of binary type is not valid base64 encoded string.', (string) $attrs->key)); | ||
} | ||
|
||
break; | ||
|
||
default: | ||
$value = (string) $def; | ||
|
||
if (is_numeric($value)) { | ||
if (strpos($value, '.') !== false) { | ||
$value = (float) $value; | ||
} else { | ||
$value = (int) $value; | ||
} | ||
} elseif ($value === 'true') { | ||
$value = true; | ||
} elseif ($value === 'false') { | ||
$value = false; | ||
} | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
} |
Oops, something went wrong.