Skip to content

Commit

Permalink
experimenting with psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
SignpostMarv committed Jan 19, 2019
1 parent 9199553 commit 210ac3c
Show file tree
Hide file tree
Showing 18 changed files with 236 additions and 28 deletions.
18 changes: 17 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,32 @@ matrix:
- php: 5.5
env: deps=low
- php: 5.6
env: deps=high
env:
- deps=high
- psalm=yes
- php: 7.0
env:
- psalm=yes
- php: 7.1
env:
- psalm=yes
- php: 7.2
env:
- psalm=yes
- php: 7.3
env:
- psalm=yes

install:
- if [ "$deps" = "no" ]; then composer install; fi
- if [ "$deps" = "low" ]; then composer update --prefer-lowest; fi
- if [ "$deps" = "high" ]; then composer update; fi
- if [ "$psalm" = "yes" ]; then composer require --dev vimeo/psalm; fi

script:
- mkdir -p build/logs
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- if [ "$psalm" = "yes" ]; then vendor/bin/psalm; fi

after_script:
- php vendor/bin/coveralls
26 changes: 21 additions & 5 deletions EmailValidator/EmailLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,36 @@ class EmailLexer extends AbstractLexer
'\0' => self::C_NUL,
);

/**
* @var bool
*/
protected $hasInvalidTokens = false;

protected $previous;
/**
* @var array
*/
protected $previous = [];

public function reset()
{
$this->hasInvalidTokens = false;
parent::reset();
}

/**
* @return bool
*/
public function hasInvalidTokens()
{
return $this->hasInvalidTokens;
}

/**
* @param $type
* @param int $type
* @throws \UnexpectedValueException
* @return boolean
*
* @psalm-suppress InvalidScalarArgument
*/
public function find($type)
{
Expand All @@ -107,7 +118,7 @@ public function find($type)
/**
* getPrevious
*
* @return array token
* @return array
*/
public function getPrevious()
{
Expand Down Expand Up @@ -179,6 +190,11 @@ protected function getType(&$value)
return self::GENERIC;
}

/**
* @param string $value
*
* @return bool
*/
protected function isValid($value)
{
if (isset($this->charValue[$value])) {
Expand All @@ -189,7 +205,7 @@ protected function isValid($value)
}

/**
* @param $value
* @param string $value
* @return bool
*/
protected function isNullType($value)
Expand All @@ -202,7 +218,7 @@ protected function isNullType($value)
}

/**
* @param $value
* @param string $value
* @return bool
*/
protected function isUTF8Invalid($value)
Expand Down
36 changes: 35 additions & 1 deletion EmailValidator/EmailParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,33 @@ class EmailParser
{
const EMAIL_MAX_LENGTH = 254;

/**
* @var \SplObjectStorage|array
*/
protected $warnings;

/**
* @var string
*/
protected $domainPart = '';

/**
* @var string
*/
protected $localPart = '';
/**
* @var EmailLexer
*/
protected $lexer;

/**
* @var LocalPart
*/
protected $localPartParser;

/**
* @var DomainPart
*/
protected $domainPartParser;

public function __construct(EmailLexer $lexer)
Expand All @@ -33,7 +55,7 @@ public function __construct(EmailLexer $lexer)
}

/**
* @param $str
* @param string $str
* @return array
*/
public function parse($str)
Expand All @@ -57,6 +79,9 @@ public function parse($str)
return array('local' => $this->localPart, 'domain' => $this->domainPart);
}

/**
* @return Warning\Warning[]
*/
public function getWarnings()
{
$localPartWarnings = $this->localPartParser->getWarnings();
Expand All @@ -68,18 +93,27 @@ public function getWarnings()
return $this->warnings;
}

/**
* @return string
*/
public function getParsedDomainPart()
{
return $this->domainPart;
}

/**
* @param string $email
*/
protected function setParts($email)
{
$parts = explode('@', $email);
$this->domainPart = $this->domainPartParser->getDomainPart();
$this->localPart = $parts[0];
}

/**
* @return bool
*/
protected function hasAtToken()
{
$this->lexer->moveNext();
Expand Down
10 changes: 5 additions & 5 deletions EmailValidator/EmailValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class EmailValidator
private $lexer;

/**
* @var array
* @var Warning\Warning[]
*/
protected $warnings;
protected $warnings = [];

/**
* @var InvalidEmail
* @var InvalidEmail|null
*/
protected $error;

Expand All @@ -28,7 +28,7 @@ public function __construct()
}

/**
* @param $email
* @param string $email
* @param EmailValidation $emailValidation
* @return bool
*/
Expand Down Expand Up @@ -58,7 +58,7 @@ public function getWarnings()
}

/**
* @return InvalidEmail
* @return InvalidEmail|null
*/
public function getError()
{
Expand Down
36 changes: 32 additions & 4 deletions EmailValidator/Parser/DomainPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
class DomainPart extends Parser
{
const DOMAIN_MAX_LENGTH = 254;

/**
* @var string
*/
protected $domainPart = '';

public function parse($domainPart)
Expand Down Expand Up @@ -77,11 +81,18 @@ public function parse($domainPart)
$this->domainPart = $domain;
}

/**
* @return string
*/
public function getDomainPart()
{
return $this->domainPart;
}

/**
* @param string $addressLiteral
* @param int $maxGroups
*/
public function checkIPV6Tag($addressLiteral, $maxGroups = 8)
{
$prev = $this->lexer->getPrevious();
Expand Down Expand Up @@ -125,6 +136,9 @@ public function checkIPV6Tag($addressLiteral, $maxGroups = 8)
}
}

/**
* @return string
*/
protected function doParseDomainPart()
{
$domain = '';
Expand Down Expand Up @@ -171,14 +185,17 @@ protected function doParseDomainPart()
return $domain;
}

private function checkNotAllowedChars($token)
private function checkNotAllowedChars(array $token)
{
$notAllowed = [EmailLexer::S_BACKSLASH => true, EmailLexer::S_SLASH=> true];
if (isset($notAllowed[$token['type']])) {
throw new CharNotAllowed();
}
}

/**
* @return string|false
*/
protected function parseDomainLiteral()
{
if ($this->lexer->isNextToken(EmailLexer::S_COLON)) {
Expand All @@ -195,6 +212,9 @@ protected function parseDomainLiteral()
return $this->doParseDomainLiteral();
}

/**
* @return string|false
*/
protected function doParseDomainLiteral()
{
$IPv6TAG = false;
Expand Down Expand Up @@ -262,6 +282,11 @@ protected function doParseDomainLiteral()
return $addressLiteral;
}

/**
* @param string $addressLiteral
*
* @return string|false
*/
protected function checkIPV4Tag($addressLiteral)
{
$matchesIP = array();
Expand All @@ -279,13 +304,13 @@ protected function checkIPV4Tag($addressLiteral)
return false;
}
// Convert IPv4 part to IPv6 format for further testing
$addressLiteral = substr($addressLiteral, 0, $index) . '0:0';
$addressLiteral = substr($addressLiteral, 0, (int) $index) . '0:0';
}

return $addressLiteral;
}

protected function checkDomainPartExceptions($prev)
protected function checkDomainPartExceptions(array $prev)
{
$invalidDomainTokens = array(
EmailLexer::S_DQUOTE => true,
Expand Down Expand Up @@ -320,6 +345,9 @@ protected function checkDomainPartExceptions($prev)
}
}

/**
* @return bool
*/
protected function hasBrackets()
{
if ($this->lexer->token['type'] !== EmailLexer::S_OPENBRACKET) {
Expand All @@ -335,7 +363,7 @@ protected function hasBrackets()
return true;
}

protected function checkLabelLength($prev)
protected function checkLabelLength(array $prev)
{
if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
$prev['type'] === EmailLexer::GENERIC &&
Expand Down
8 changes: 7 additions & 1 deletion EmailValidator/Parser/LocalPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function parse($localPart)
}
}

/**
* @return bool
*/
protected function parseDoubleQuote()
{
$parseAgain = true;
Expand Down Expand Up @@ -118,7 +121,10 @@ protected function parseDoubleQuote()
return $parseAgain;
}

protected function isInvalidToken($token, $closingQuote)
/**
* @param bool $closingQuote
*/
protected function isInvalidToken(array $token, $closingQuote)
{
$forbidden = array(
EmailLexer::S_COMMA,
Expand Down
Loading

0 comments on commit 210ac3c

Please sign in to comment.