Skip to content

Commit

Permalink
Consistent usage of to string (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
siad007 authored and mrook committed May 11, 2018
1 parent 0d9e10b commit 4d1b68a
Showing 42 changed files with 121 additions and 138 deletions.
2 changes: 1 addition & 1 deletion classes/phing/BuildException.php
Original file line number Diff line number Diff line change
@@ -121,6 +121,6 @@ public function getLocation()
public function setLocation(Location $loc)
{
$this->location = $loc;
$this->message = $loc->toString() . ': ' . $this->message;
$this->message = (string) $loc . ': ' . $this->message;
}
}
12 changes: 5 additions & 7 deletions classes/phing/Phing.php
Original file line number Diff line number Diff line change
@@ -183,20 +183,18 @@ public static function start($args, array $additionalUserProperties = null)
$exitCode = 0;
} catch (ExitStatusException $ese) {
$exitCode = $ese->getCode();
if ($exitCode != 0) {
if ($exitCode !== 0) {
self::handleLogfile();
throw $ese;
}
}
} catch (BuildException $exc) {
// avoid printing output twice: self::printMessage($exc);
} catch (Exception $exc) {
echo $exc->getTraceAsString();
} catch (Throwable $exc) {
self::printMessage($exc);
} finally {
self::handleLogfile();
}

// everything fine, shutdown
self::handleLogfile();
self::statusExit($exitCode);
}

@@ -221,7 +219,7 @@ public static function printMessage(Exception $t)
self::initializeOutputStreams();
}
if (self::getMsgOutputLevel() >= Project::MSG_VERBOSE) {
self::$err->write($t->__toString() . PHP_EOL);
self::$err->write((string) $t . PHP_EOL);
} else {
self::$err->write($t->getMessage() . PHP_EOL);
}
6 changes: 3 additions & 3 deletions classes/phing/Project.php
Original file line number Diff line number Diff line change
@@ -795,7 +795,7 @@ public function topoSort($rootTarget)

$retHuman = "";
for ($i = 0, $_i = count($ret); $i < $_i; $i++) {
$retHuman .= $ret[$i]->toString() . " ";
$retHuman .= (string) $ret[$i] . " ";
}
$this->log("Build sequence for target '$rootTarget' is: $retHuman", Project::MSG_VERBOSE);

@@ -817,7 +817,7 @@ public function topoSort($rootTarget)

$retHuman = "";
for ($i = 0, $_i = count($ret); $i < $_i; $i++) {
$retHuman .= $ret[$i]->toString() . " ";
$retHuman .= (string) $ret[$i] . " ";
}
$this->log("Complete build sequence is: $retHuman", Project::MSG_VERBOSE);

@@ -1083,7 +1083,7 @@ public function fireTaskFinished($task, $exception)
* @param $message
* @param $priority
*/
public function fireMessageLoggedEvent($event, $message, $priority)
public function fireMessageLoggedEvent(BuildEvent $event, $message, $priority)
{
$event->setMessage($message, $priority);
foreach ($this->listeners as $listener) {
11 changes: 0 additions & 11 deletions classes/phing/Target.php
Original file line number Diff line number Diff line change
@@ -308,17 +308,6 @@ public function getLogSkipped()
return $this->logSkipped;
}

/**
* Returns a string representation of this target. In our case it
* simply returns the target name field
*
* @return string The string representation of this target
*/
public function toString()
{
return (string) $this;
}

/**
* Returns a string representation of this target. In our case it
* simply returns the target name field
6 changes: 1 addition & 5 deletions classes/phing/dispatch/DispatchUtils.php
Original file line number Diff line number Diff line change
@@ -58,11 +58,7 @@ public static function main($task)
$c = new ReflectionClass($dispatchable);
$actionM = $c->getMethod($mName);
$o = $actionM->invoke($dispatchable);
if (method_exists($o, 'toString')) {
$methodName = trim($o->toString());
} else {
$methodName = trim((string) $o);
}
$methodName = trim((string) $o);
if (empty($methodName)) {
throw new ReflectionException();
}
2 changes: 1 addition & 1 deletion classes/phing/listener/XmlLogger.php
Original file line number Diff line number Diff line change
@@ -241,7 +241,7 @@ public function taskStarted(BuildEvent $event)

$taskElement = $this->doc->createElement(XmlLogger::TASK_TAG);
$taskElement->setAttribute(XmlLogger::NAME_ATTR, $task->getTaskName());
$taskElement->setAttribute(XmlLogger::LOCATION_ATTR, $task->getLocation()->toString());
$taskElement->setAttribute(XmlLogger::LOCATION_ATTR, (string) $task->getLocation());

$this->timesStack[] = Phing::currentTimeMillis();
$this->elementStack[] = $taskElement;
10 changes: 1 addition & 9 deletions classes/phing/parser/Location.php
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ public function getColumnNumber()
*
* @return string the string representation of this Location object
*/
public function toString()
public function __toString()
{
$buf = "";
if ($this->fileName !== null) {
@@ -96,12 +96,4 @@ public function toString()

return (string) $buf;
}

/**
* @return string
*/
public function __toString()
{
return $this->toString();
}
}
10 changes: 0 additions & 10 deletions classes/phing/system/io/PhingFile.php
Original file line number Diff line number Diff line change
@@ -1100,16 +1100,6 @@ public function equals($obj)
return false;
}

/**
* Backwards compatibility - @see __toString()
*
* @return string
*/
public function toString()
{
return $this->__toString();
}

/**
* Return string representation of the object
*
2 changes: 1 addition & 1 deletion classes/phing/system/lang/EventObject.php
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ public function getSource()
}

/** Returns a String representation of this EventObject.*/
public function toString()
public function __toString()
{
if (method_exists($this->source, "toString")) {
return get_class($this) . "[source=" . $this->source->toString() . "]";
4 changes: 2 additions & 2 deletions classes/phing/system/util/Properties.php
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ protected function outVal($val)
*
* @return string
*/
public function toString()
public function __toString()
{
$buf = "";
foreach ($this->properties as $key => $item) {
@@ -147,7 +147,7 @@ public function store(PhingFile $file = null, $header = null)
if ($header !== null) {
$fw->write("# " . $header . PHP_EOL);
}
$fw->write($this->toString());
$fw->write((string) $this);
$fw->close();
} catch (IOException $e) {
throw new IOException("Error writing property file: " . $e->getMessage());
2 changes: 1 addition & 1 deletion classes/phing/tasks/ext/property/PathToFileSet.php
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ public function main()
}
if (!$this->dir->isDirectory()) {
throw new BuildException(
$this->dir->toString() . " is not a directory");
(string) $this->dir . " is not a directory");
}
$path = $this->getProject()->getReference($this->pathRefId);
if ($path == null) {
4 changes: 2 additions & 2 deletions classes/phing/tasks/system/MatchingTask.php
Original file line number Diff line number Diff line change
@@ -217,9 +217,9 @@ public function hasSelectors()
*
* @return int The number of selectors in this container
*/
public function selectorCount()
public function count()
{
return $this->fileset->selectorCount();
return $this->fileset->count();
}

/**
6 changes: 1 addition & 5 deletions classes/phing/tasks/system/PropertyTask.php
Original file line number Diff line number Diff line change
@@ -255,7 +255,7 @@ public function getOverride()
/**
* @return string
*/
public function toString()
public function __toString()
{
return (string) $this->value;
}
@@ -351,8 +351,6 @@ public function main()

if ($referencedObject instanceof Exception) {
$reference = $referencedObject->getMessage();
} elseif (method_exists($referencedObject, 'toString')) {
$reference = $referencedObject->toString();
} else {
$reference = (string) $referencedObject;
}
@@ -364,8 +362,6 @@ public function main()

if ($referencedObject instanceof Exception) {
$reference = $referencedObject->getMessage();
} elseif (method_exists($referencedObject, 'toString')) {
$reference = $referencedObject->toString();
} else {
$reference = (string) $referencedObject;
}
2 changes: 1 addition & 1 deletion classes/phing/tasks/system/TempFile.php
Original file line number Diff line number Diff line change
@@ -173,6 +173,6 @@ public function main()
$this->deleteOnExit,
$this->createFile
);
$this->getProject()->setNewProperty($this->property, $tmpFile->toString());
$this->getProject()->setNewProperty($this->property, (string) $tmpFile);
}
}
2 changes: 1 addition & 1 deletion classes/phing/tasks/system/condition/IsFileSelected.php
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ public function setBaseDir(PhingFile $baseDir)
*/
public function validate()
{
if ($this->selectorCount() != 1) {
if ($this->count() != 1) {
throw new BuildException("Only one selector allowed");
}
parent::validate();
57 changes: 50 additions & 7 deletions classes/phing/types/AbstractFileSet.php
Original file line number Diff line number Diff line change
@@ -339,9 +339,11 @@ protected function setupDirectoryScanner(DirectoryScanner $ds, Project $p)
$this->getRef($p)->setupDirectoryScanner($ds, $p);
return;
}
if ($ds === null) {
throw new BuildException("DirectoryScanner cannot be null");
}

$stk[] = $this;
$this->dieOnCircularReference($stk, $p);
array_pop($stk);

// FIXME - pass dir directly when dirscanner supports File
$ds->setBasedir($this->dir->getPath());

@@ -353,12 +355,16 @@ protected function setupDirectoryScanner(DirectoryScanner $ds, Project $p)
$ds->setExcludes($this->defaultPatterns->getExcludePatterns($p));

$p->log(
"FileSet: Setup file scanner in dir " . (string) $this->dir . " with " . $this->defaultPatterns->toString(),
$this->getDataTypeName() . ": Setup file scanner in dir " . (string) $this->dir . " with " . (string) $this->defaultPatterns,
Project::MSG_DEBUG
);

if ($ds instanceof SelectorScanner) {
$ds->setSelectors($this->getSelectors($p));
$selectors = $this->getSelectors($p);
foreach ($selectors as $selector) {
$p->log((string) $selector . PHP_EOL, Project::MSG_DEBUG);
}
$ds->setSelectors($selectors);
}

if ($this->useDefaultExcludes) {
@@ -367,6 +373,25 @@ protected function setupDirectoryScanner(DirectoryScanner $ds, Project $p)
$ds->setCaseSensitive($this->isCaseSensitive);
}

public function dieOnCircularReference(&$stk, Project $p = null)
{
if ($this->checked) {
return;
}
if ($this->isReference()) {
parent::dieOnCircularReference($stk, $p);
} else {
foreach ($this->selectorsList as $fileSelector) {
if ($fileSelector instanceof DataType) {
static::pushAndInvokeCircularReferenceCheck($fileSelector, $stk, $p);
}
}
foreach ($this->additionalPatterns as $ps) {
static::pushAndInvokeCircularReferenceCheck($ps, $stk, $p);
}
$this->setChecked(true);
}
}

/**
* Performs the check for circular references and returns the
@@ -431,11 +456,11 @@ public function hasPatterns()
* @throws Exception
* @return int The number of selectors in this container
*/
public function selectorCount()
public function count()
{
if ($this->isReference() && $this->getProject() !== null) {
try {
return $this->getRef($this->getProject())->selectorCount();
return $this->getRef($this->getProject())->count();
} catch (Exception $e) {
throw $e;
}
@@ -509,4 +534,22 @@ public function getIterator(...$options): \ArrayIterator
}

abstract protected function getFiles(...$options);

public function __toString()
{
try {
if ($this->isReference()) {
return (string) $this->getRef($this->getProject());
}
$stk[] = $this;
$this->dieOnCircularReference($stk, $this->getProject());
$ds = $this->getDirectoryScanner($this->getProject());
$files = $ds->getIncludedFiles();
$result = implode(';', $files);
} catch (BuildException $e) {
$result = '';
}

return $result;
}
}
2 changes: 1 addition & 1 deletion classes/phing/types/DataType.php
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ class DataType extends ProjectComponent
*
* @var Reference $ref
*/
public $ref = null;
private $ref;

/**
* Are we sure we don't hold circular references?
2 changes: 1 addition & 1 deletion classes/phing/types/ExcludesNameEntry.php
Original file line number Diff line number Diff line change
@@ -72,7 +72,7 @@ public function getName()
*
* @return string
*/
public function toString()
public function __toString()
{
return $this->name;
}
6 changes: 3 additions & 3 deletions classes/phing/types/PatternSet.php
Original file line number Diff line number Diff line change
@@ -413,7 +413,7 @@ private function readFiles(Project $p)
/**
* @return string
*/
public function toString()
public function __toString()
{

// We can't compile includeList into array because, toString() does
@@ -427,7 +427,7 @@ public function toString()
} else {
$includes = "";
foreach ($this->includeList as $ne) {
$includes .= $ne->toString() . ",";
$includes .= (string) $ne . ",";
}
$includes = rtrim($includes, ",");
}
@@ -437,7 +437,7 @@ public function toString()
} else {
$excludes = "";
foreach ($this->excludeList as $ne) {
$excludes .= $ne->toString() . ",";
$excludes .= (string) $ne . ",";
}
$excludes = rtrim($excludes, ",");
}
2 changes: 1 addition & 1 deletion classes/phing/types/PatternSetNameEntry.php
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ public function valid(Project $project)
* Gets a string representation of this pattern.
* @return string
*/
public function toString()
public function __toString()
{
$buf = $this->name;
if (($this->ifCond !== null) || ($this->unlessCond !== null)) {
2 changes: 1 addition & 1 deletion classes/phing/types/PropertyValue.php
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ public function getValue()
/**
* @return string
*/
public function toString()
public function __toString()
{
return $this->getValue();
}
Loading

0 comments on commit 4d1b68a

Please sign in to comment.