Skip to content

Commit

Permalink
feature #20680 DoctrineDataCollector: taught sanitizeParam to support…
Browse files Browse the repository at this point in the history
… classes with __toString implemented. (FractalizeR)

This PR was squashed before being merged into the 3.3-dev branch (closes #20680).

Discussion
----------

DoctrineDataCollector: taught sanitizeParam to support classes with __toString implemented.

This PR teaches \Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector::sanitizeParam support objects, which implement __toString and therefore can be represented as a string with more sense, than "(object) ClassName". It also includes test for the feature.

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | [20673](#20673)
| License       | MIT
| Doc PR        | no

Commits
-------

f2970f2 DoctrineDataCollector: taught sanitizeParam to support classes with __toString implemented.
  • Loading branch information
fabpot committed Mar 2, 2017
2 parents e2e9c94 + f2970f2 commit 3b4a8f3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Expand Up @@ -159,7 +159,11 @@ private function sanitizeQuery($connectionName, $query)
private function sanitizeParam($var)
{
if (is_object($var)) {
return array(sprintf('Object(%s)', get_class($var)), false);
$className = get_class($var);

return method_exists($var, '__toString') ?
array(sprintf('Object(%s): "%s"', $className, $var->__toString()), false) :
array(sprintf('Object(%s)', $className), false);
}

if (is_array($var)) {
Expand Down
Expand Up @@ -127,7 +127,13 @@ public function paramProvider()
array(null, array(), null, true),
array(new \DateTime('2011-09-11'), array('date'), '2011-09-11', true),
array(fopen(__FILE__, 'r'), array(), 'Resource(stream)', false),
array(new \SplFileInfo(__FILE__), array(), 'Object(SplFileInfo)', false),
array(new \stdClass(), array(), 'Object(stdClass)', false),
array(
new StringRepresentableClass('presentation test'),
array(),
'Object(Symfony\Bridge\Doctrine\Tests\DataCollector\StringRepresentableClass): "presentation test"',
false,
),
);
}

Expand Down
@@ -0,0 +1,29 @@
<?php

namespace Symfony\Bridge\Doctrine\Tests\DataCollector;

/**
* A class for testing __toString method behaviour. It's __toString returns a value, that was passed into constructor.
*/
class StringRepresentableClass
{
/**
* @var string
*/
private $representation;

/**
* CustomStringableClass constructor.
*
* @param string $representation
*/
public function __construct($representation)
{
$this->representation = $representation;
}

public function __toString()
{
return $this->representation;
}
}

0 comments on commit 3b4a8f3

Please sign in to comment.