Skip to content

Commit

Permalink
fixed starts with letter (webmozarts#138)
Browse files Browse the repository at this point in the history
    Added psalm assert
    Added string assertion
    Added test
    Added static-analysis class.
  • Loading branch information
FrontEndCoffee authored and BackEndTea committed Oct 15, 2019
1 parent db2f01f commit a296d8a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -1045,13 +1045,17 @@ public static function startsWith($value, $prefix, $message = '')
}

/**
* @psalm-assert string $value
*
* @param mixed $value
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function startsWithLetter($value, $message = '')
{
static::string($value);

$valid = isset($value[0]);

if ($valid) {
Expand Down
3 changes: 3 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,14 @@ public function getTests()
array('startsWith', array('😄😑☹️', '😑'), false),
array('startsWith', array('', '😑'), false),
array('startsWithLetter', array('abcd'), true),
array('startsWithLetter', array(array(66)), false),
array('startsWithLetter', array('a'), true),
array('startsWithLetter', array('a1'), true),
array('startsWithLetter', array('1abcd'), false),
array('startsWithLetter', array('1'), false),
array('startsWithLetter', array(''), false),
array('startsWithLetter', array(null), false),
array('startsWithLetter', array(66), false),
array('endsWith', array('abcd', 'cd'), true),
array('endsWith', array('abcd', 'bc'), false),
array('endsWith', array('', 'bc'), false),
Expand Down
15 changes: 15 additions & 0 deletions tests/static-analysis/assert-startsWithLetter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @param mixed $value
* @return string
*/
function consume($value) : string
{
Assert::startsWithLetter($value);
return $value;
}

0 comments on commit a296d8a

Please sign in to comment.