Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | 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
21
 */
22
class ClassHasAttribute extends Constraint
23
{
24
    /**
25
     * @var string
26
     */
27
    private $attributeName;
28
 
29
    public function __construct(string $attributeName)
30
    {
31
        $this->attributeName = $attributeName;
32
    }
33
 
34
    /**
35
     * Returns a string representation of the constraint.
36
     */
37
    public function toString(): string
38
    {
39
        return sprintf(
40
            'has attribute "%s"',
41
            $this->attributeName
42
        );
43
    }
44
 
45
    /**
46
     * Evaluates the constraint for parameter $other. Returns true if the
47
     * constraint is met, false otherwise.
48
     *
49
     * @param mixed $other value or object to evaluate
50
     */
51
    protected function matches($other): bool
52
    {
53
        try {
54
            return (new ReflectionClass($other))->hasProperty($this->attributeName);
55
            // @codeCoverageIgnoreStart
56
        } catch (ReflectionException $e) {
57
            throw new Exception(
58
                $e->getMessage(),
59
                $e->getCode(),
60
                $e
61
            );
62
        }
63
        // @codeCoverageIgnoreEnd
64
    }
65
 
66
    /**
67
     * Returns the description of the failure.
68
     *
69
     * The beginning of failure messages is "Failed asserting that" in most
70
     * cases. This method should return the second part of that sentence.
71
     *
72
     * @param mixed $other evaluated value or object
73
     */
74
    protected function failureDescription($other): string
75
    {
76
        return sprintf(
77
            '%sclass "%s" %s',
78
            is_object($other) ? 'object of ' : '',
79
            is_object($other) ? get_class($other) : $other,
80
            $this->toString()
81
        );
82
    }
83
 
84
    protected function attributeName(): string
85
    {
86
        return $this->attributeName;
87
    }
88
}