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

Breadcrumb

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

class StringContains

@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\StringContains extends \PHPUnit\Framework\Constraint\Constraint

Expanded class hierarchy of StringContains

2 files declare their use of StringContains
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/StringContains.php, line 25

Namespace

PHPUnit\Framework\Constraint
View source
final class StringContains extends Constraint {
    private readonly string $needle;
    private readonly bool $ignoreCase;
    private readonly bool $ignoreLineEndings;
    public function __construct(string $needle, bool $ignoreCase = false, bool $ignoreLineEndings = false) {
        if ($ignoreLineEndings) {
            $needle = $this->normalizeLineEndings($needle);
        }
        $this->needle = $needle;
        $this->ignoreCase = $ignoreCase;
        $this->ignoreLineEndings = $ignoreLineEndings;
    }
    
    /**
     * Returns a string representation of the constraint.
     */
    public function toString() : string {
        $needle = $this->needle;
        if ($this->ignoreCase) {
            $needle = mb_strtolower($this->needle, 'UTF-8');
        }
        return sprintf('contains "%s" [%s](length: %s)', $needle, $this->getDetectedEncoding($needle), strlen($needle));
    }
    public function failureDescription(mixed $other) : string {
        $stringifiedHaystack = Exporter::export($other, true);
        $haystackEncoding = $this->getDetectedEncoding($other);
        $haystackLength = $this->getHaystackLength($other);
        $haystackInformation = sprintf('%s [%s](length: %s) ', $stringifiedHaystack, $haystackEncoding, $haystackLength);
        $needleInformation = $this->toString(true);
        return $haystackInformation . $needleInformation;
    }
    
    /**
     * Evaluates the constraint for parameter $other. Returns true if the
     * constraint is met, false otherwise.
     */
    protected function matches(mixed $other) : bool {
        $haystack = $other;
        if ('' === $this->needle) {
            return true;
        }
        if (!is_string($haystack)) {
            return false;
        }
        if ($this->ignoreLineEndings) {
            $haystack = $this->normalizeLineEndings($haystack);
        }
        if ($this->ignoreCase) {
            
            /*
             * We must use the multibyte-safe version, so we can accurately compare non-latin uppercase characters with
             * their lowercase equivalents.
             */
            return mb_stripos($haystack, $this->needle, 0, 'UTF-8') !== false;
        }
        
        /*
         * Use the non-multibyte safe functions to see if the string is contained in $other.
         *
         * This function is very fast, and we don't care about the character position in the string.
         *
         * Additionally, we want this method to be binary safe, so we can check if some binary data is in other binary
         * data.
         */
        return str_contains($haystack, $this->needle);
    }
    private function getDetectedEncoding(mixed $other) : string {
        if ($this->ignoreCase) {
            return 'Encoding ignored';
        }
        if (!is_string($other)) {
            return 'Encoding detection failed';
        }
        $detectedEncoding = mb_detect_encoding($other, null, true);
        if ($detectedEncoding === false) {
            return 'Encoding detection failed';
        }
        return $detectedEncoding;
    }
    private function getHaystackLength(mixed $haystack) : int {
        if (!is_string($haystack)) {
            return 0;
        }
        if ($this->ignoreLineEndings) {
            $haystack = $this->normalizeLineEndings($haystack);
        }
        return strlen($haystack);
    }
    private function normalizeLineEndings(string $string) : string {
        return strtr($string, [
            "\r\n" => "\n",
            "\r" => "\n",
        ]);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
Constraint::additionalFailureDescription protected function Return additional failure description where needed. 7
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
StringContains::$ignoreCase private property
StringContains::$ignoreLineEndings private property
StringContains::$needle private property
StringContains::failureDescription public function Returns the description of the failure. Overrides Constraint::failureDescription
StringContains::getDetectedEncoding private function
StringContains::getHaystackLength private function
StringContains::matches protected function Evaluates the constraint for parameter $other. Returns true if the
constraint is met, false otherwise.
Overrides Constraint::matches
StringContains::normalizeLineEndings private function
StringContains::toString public function Returns a string representation of the constraint. Overrides SelfDescribing::toString
StringContains::__construct public function

API Navigation

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