Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. StringMatchesFormatDescription.php

class StringMatchesFormatDescription

@no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit

Hierarchy

  • class \PHPUnit\Framework\Constraint\Constraint implements \Countable, \PHPUnit\Framework\SelfDescribing
    • class \PHPUnit\Framework\Constraint\StringMatchesFormatDescription extends \PHPUnit\Framework\Constraint\Constraint

Expanded class hierarchy of StringMatchesFormatDescription

2 files declare their use of StringMatchesFormatDescription
Assert.php in vendor/phpunit/phpunit/src/Framework/Assert.php
Functions.php in vendor/phpunit/phpunit/src/Framework/Assert/Functions.php

File

vendor/phpunit/phpunit/src/Framework/Constraint/String/StringMatchesFormatDescription.php, line 26

Namespace

PHPUnit\Framework\Constraint
View source
final class StringMatchesFormatDescription extends Constraint {
    private readonly string $formatDescription;
    public function __construct(string $formatDescription) {
        $this->formatDescription = $formatDescription;
    }
    public function toString() : string {
        return 'matches format description:' . PHP_EOL . $this->formatDescription;
    }
    
    /**
     * Evaluates the constraint for parameter $other. Returns true if the
     * constraint is met, false otherwise.
     */
    protected function matches(mixed $other) : bool {
        $other = $this->convertNewlines($other);
        $matches = preg_match($this->regularExpressionForFormatDescription($this->convertNewlines($this->formatDescription)), $other);
        return $matches > 0;
    }
    protected function failureDescription(mixed $other) : string {
        return 'string matches format description';
    }
    protected function additionalFailureDescription(mixed $other) : string {
        $from = explode("\n", $this->formatDescription);
        $to = explode("\n", $this->convertNewlines($other));
        foreach ($from as $index => $line) {
            if (isset($to[$index]) && $line !== $to[$index]) {
                $line = $this->regularExpressionForFormatDescription($line);
                if (preg_match($line, $to[$index]) > 0) {
                    $from[$index] = $to[$index];
                }
            }
        }
        $from = implode("\n", $from);
        $to = implode("\n", $to);
        return $this->differ()
            ->diff($from, $to);
    }
    private function regularExpressionForFormatDescription(string $string) : string {
        $string = strtr(preg_quote($string, '/'), [
            '%%' => '%',
            '%e' => preg_quote(DIRECTORY_SEPARATOR, '/'),
            '%s' => '[^\\r\\n]+',
            '%S' => '[^\\r\\n]*',
            '%a' => '.+?',
            '%A' => '.*?',
            '%w' => '\\s*',
            '%i' => '[+-]?\\d+',
            '%d' => '\\d+',
            '%x' => '[0-9a-fA-F]+',
            '%f' => '[+-]?(?:\\d+|(?=\\.\\d))(?:\\.\\d+)?(?:[Ee][+-]?\\d+)?',
            '%c' => '.',
            '%0' => '\\x00',
        ]);
        return '/^' . $string . '$/s';
    }
    private function convertNewlines(string $text) : string {
        return preg_replace('/\\r\\n/', "\n", $text);
    }
    private function differ() : Differ {
        return new Differ(new UnifiedDiffOutputBuilder("--- Expected\n+++ Actual\n"));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Constraint::count public function Counts the number of constraint elements. 3
Constraint::evaluate public function Evaluates the constraint for parameter $other. 7
Constraint::exporter protected function
Constraint::fail protected function Throws an exception for the given compared value and test description. 1
Constraint::failureDescriptionInContext protected function Returns the description of the failure when this constraint appears in
context of an $operator expression.
Constraint::reduce protected function Reduces the sub-expression starting at $this by skipping degenerate
sub-expression and returns first descendant constraint that starts
a non-reducible sub-expression.
2
Constraint::toStringInContext protected function Returns a custom string representation of the constraint object when it
appears in context of an $operator expression.
Constraint::valueToTypeStringFragment protected function @psalm-return non-empty-string
StringMatchesFormatDescription::$formatDescription private property
StringMatchesFormatDescription::additionalFailureDescription protected function Return additional failure description where needed. Overrides Constraint::additionalFailureDescription
StringMatchesFormatDescription::convertNewlines private function
StringMatchesFormatDescription::differ private function
StringMatchesFormatDescription::failureDescription protected function Returns the description of the failure. Overrides Constraint::failureDescription
StringMatchesFormatDescription::matches protected function Evaluates the constraint for parameter $other. Returns true if the
constraint is met, false otherwise.
Overrides Constraint::matches
StringMatchesFormatDescription::regularExpressionForFormatDescription private function
StringMatchesFormatDescription::toString public function Returns a string representation of the object. Overrides SelfDescribing::toString
StringMatchesFormatDescription::__construct public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal