Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of PHPUnit.
4
 *
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PHPUnit\Framework\Constraint;
11
 
12
use function get_class;
13
use function is_object;
14
use function sprintf;
15
use PHPUnit\Framework\Exception;
16
use ReflectionClass;
17
use ReflectionException;
18
 
19
/**
20
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit
621 lars 21
 *
22
 * @deprecated https://github.com/sebastianbergmann/phpunit/issues/4601
148 lars 23
 */
24
class ClassHasAttribute extends Constraint
25
{
26
    /**
27
     * @var string
28
     */
29
    private $attributeName;
30
 
31
    public function __construct(string $attributeName)
32
    {
33
        $this->attributeName = $attributeName;
34
    }
35
 
36
    /**
37
     * Returns a string representation of the constraint.
38
     */
39
    public function toString(): string
40
    {
41
        return sprintf(
42
            'has attribute "%s"',
43
            $this->attributeName
44
        );
45
    }
46
 
47
    /**
48
     * Evaluates the constraint for parameter $other. Returns true if the
49
     * constraint is met, false otherwise.
50
     *
51
     * @param mixed $other value or object to evaluate
52
     */
53
    protected function matches($other): bool
54
    {
55
        try {
56
            return (new ReflectionClass($other))->hasProperty($this->attributeName);
57
            // @codeCoverageIgnoreStart
58
        } catch (ReflectionException $e) {
59
            throw new Exception(
60
                $e->getMessage(),
61
                $e->getCode(),
62
                $e
63
            );
64
        }
65
        // @codeCoverageIgnoreEnd
66
    }
67
 
68
    /**
69
     * Returns the description of the failure.
70
     *
71
     * The beginning of failure messages is "Failed asserting that" in most
72
     * cases. This method should return the second part of that sentence.
73
     *
74
     * @param mixed $other evaluated value or object
75
     */
76
    protected function failureDescription($other): string
77
    {
78
        return sprintf(
79
            '%sclass "%s" %s',
80
            is_object($other) ? 'object of ' : '',
81
            is_object($other) ? get_class($other) : $other,
82
            $this->toString()
83
        );
84
    }
85
 
86
    protected function attributeName(): string
87
    {
88
        return $this->attributeName;
89
    }
90
}