Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid infinite loops during command info parsing. #309

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions src/Parser/CommandInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@
/**
* @var boolean
* @var string
*/
*/
protected $docBlockIsParsed = false;

/**
* @var boolean
* @var string
*/
protected $parsingInProgress = false;

/**
* @var string
*/
Expand Down Expand Up @@ -164,6 +170,9 @@
$this->simpleOptionParametersAllowed = empty($optionsFromParameters);
$this->options = new DefaultsWithDescriptions($optionsFromParameters, false);
$this->arguments = $this->determineAgumentClassifications();

// Construct the object from docblock annotations or php attributes
$this->parseDocBlock();
}

/**
Expand All @@ -183,7 +192,7 @@
*/
public function getName()
{
$this->parseDocBlock();
// getName() is the only attribute that may be used during parsing.
return $this->name;
}

Expand Down Expand Up @@ -227,13 +236,13 @@

public function getReturnType()
{
$this->parseDocBlock();
$this->requireConsistentState();
return $this->returnType;
}

public function getInjectedClasses()
{
$this->parseDocBlock();
$this->requireConsistentState();
return $this->injectedClasses;
}

Expand All @@ -258,7 +267,7 @@
*/
public function getRawAnnotations()
{
$this->parseDocBlock();
$this->requireConsistentState();
return $this->otherAnnotations;
}

Expand Down Expand Up @@ -336,7 +345,7 @@
*/
public function hasAnnotation($annotation)
{
$this->parseDocBlock();
$this->requireConsistentState();
return isset($this->otherAnnotations[$annotation]);
}

Expand Down Expand Up @@ -369,7 +378,7 @@
*/
public function getDescription()
{
$this->parseDocBlock();
$this->requireConsistentState();
return $this->description;
}

Expand Down Expand Up @@ -397,7 +406,7 @@
*/
public function getHelp()
{
$this->parseDocBlock();
$this->requireConsistentState();
return $this->help;
}
/**
Expand All @@ -417,7 +426,7 @@
*/
public function getAliases()
{
$this->parseDocBlock();
$this->requireConsistentState();
return $this->aliases;
}

Expand All @@ -441,7 +450,7 @@
*/
public function getHidden()
{
$this->parseDocBlock();
$this->requireConsistentState();
return $this->hasAnnotation('hidden');
}

Expand All @@ -465,7 +474,7 @@
*/
public function getExampleUsages()
{
$this->parseDocBlock();
$this->requireConsistentState();
return $this->exampleUsage;
}

Expand Down Expand Up @@ -865,17 +874,29 @@
return strtolower($camel);
}

/**
* Guard against invalid usage of CommandInfo during parsing.
*/
protected function requireConsistentState()
{
if ($this->parsingInProgress == true) {
throw new \Exception("Cannot use CommandInfo object while it is in an inconsistant state!");

Check warning on line 883 in src/Parser/CommandInfo.php

View check run for this annotation

Codecov / codecov/patch

src/Parser/CommandInfo.php#L883

Added line #L883 was not covered by tests
}
}

/**
* Parse the docBlock comment for this command, and set the
* fields of this class with the data thereby obtained.
*/
protected function parseDocBlock()
{
if (!$this->docBlockIsParsed) {
$this->docBlockIsParsed = true;
$this->parsingInProgress = true;
// The parse function will insert data from the provided method
// into this object, using our accessors.
CommandDocBlockParserFactory::parse($this, $this->reflection);
$this->docBlockIsParsed = true;
$this->parsingInProgress = false;
// Use method's return type if @return is not present.
if ($this->reflection->hasReturnType() && !$this->getReturnType()) {
$type = $this->reflection->getReturnType();
Expand Down
Loading