Skip to content

Commit d56677b

Browse files
committed
Fix design flaw with __isset() by refactoring and combining both it and __get().
1 parent 22b3ea7 commit d56677b

1 file changed

Lines changed: 40 additions & 15 deletions

File tree

src/Config.php

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -485,28 +485,20 @@ protected function decode($variable)
485485
*/
486486
public function __get($property)
487487
{
488-
$isBuildVar = in_array($property, array_keys($this->directVariables));
489-
$isRuntimeVar = in_array($property, array_keys($this->directVariablesRuntime));
490488
// For now, all unprefixed variables are also runtime variables. If that ever changes this
491489
// logic will change with it.
490+
$isBuildVar = in_array($property, array_keys($this->directVariables));
491+
$isRuntimeVar = in_array($property, array_keys($this->directVariablesRuntime));
492492
$isUnprefixedVar = in_array($property, array_keys($this->unPrefixedVariablesRuntime));
493493

494-
495-
if ($isBuildVar) {
496-
$value = $this->getValue($this->directVariables[$property]);
497-
}
498-
else if ($isUnprefixedVar) {
499-
$value = $this->environmentVariables[$this->unPrefixedVariablesRuntime[$property]] ?? null;
500-
}
501-
else if ($isRuntimeVar) {
502-
$value = $this->getValue($this->directVariablesRuntime[$property]);
503-
}
504-
else {
494+
if (!($isBuildVar || $isUnprefixedVar || $isRuntimeVar)) {
505495
throw new \InvalidArgumentException(sprintf('No such variable defined: %s', $property));
506496
}
507497

498+
$value = $this->getPropertyValue($property);
499+
508500
if (is_null($value)) {
509-
if ($this->inBuild() && ($isRuntimeVar || $isUnprefixedVar)) {
501+
if ($this->inBuild() && !$isBuildVar) {
510502
throw new BuildTimeVariableAccessException(sprintf('The %s variable is not available during build time.', $property));
511503
}
512504
throw new NotValidPlatformException(sprintf('The %s variable is not defined. Are you sure you\'re running on Platform.sh?', $property));
@@ -526,7 +518,40 @@ public function __get($property)
526518
*/
527519
public function __isset($property)
528520
{
529-
return !is_null($this->$property);
521+
$value = $this->getPropertyValue($property);
522+
523+
return !is_null($value);
524+
}
525+
526+
/**
527+
* Returns the value of a dynamic property, whatever it's configuration.
528+
*
529+
* @param string $property
530+
* The property value to get.
531+
* @return string|null
532+
*/
533+
protected function getPropertyValue(string $property) : ?string
534+
{
535+
// For now, all unprefixed variables are also runtime variables. If that ever changes this
536+
// logic will change with it.
537+
$isBuildVar = in_array($property, array_keys($this->directVariables));
538+
$isRuntimeVar = in_array($property, array_keys($this->directVariablesRuntime));
539+
$isUnprefixedVar = in_array($property, array_keys($this->unPrefixedVariablesRuntime));
540+
541+
if ($isBuildVar) {
542+
$value = $this->getValue($this->directVariables[$property]);
543+
}
544+
else if ($isUnprefixedVar) {
545+
$value = $this->environmentVariables[$this->unPrefixedVariablesRuntime[$property]] ?? null;
546+
}
547+
else if ($isRuntimeVar) {
548+
$value = $this->getValue($this->directVariablesRuntime[$property]);
549+
}
550+
else {
551+
$value = null;
552+
}
553+
554+
return $value;
530555
}
531556

532557
/**

0 commit comments

Comments
 (0)