Skip to content

Commit

Permalink
Static code analyzer is not part of the testing framework
Browse files Browse the repository at this point in the history
Fixed the found issues
  • Loading branch information
hisorange committed Feb 26, 2020
1 parent 6687d14 commit 8c41169
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ install: composer update --prefer-source --no-interaction
script: vendor/bin/phpunit --coverage-clover ./tests/logs/clover.xml

after_success:
- "if [ $LARAVEL_VERSION = '6.5' ]; then php vendor/bin/coveralls -v; fi"
- "if [ $LARAVEL_VERSION = '6.5' ]; then vendor/bin/phpstan analyse -c phpstan.neon ./src/; fi"
- "if [ $LARAVEL_VERSION = '6.5' ]; then php vendor/bin/phpcs --standard=PSR12 ./src/; fi"
- "if [ $LARAVEL_VERSION = '6.5' ]; then php vendor/bin/coveralls -v; fi"

notifications:
email:
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"phpunit/phpunit": "~5.0 || ~6.0 || ~7.0 || ~8.0",
"orchestra/testbench": "~4.0",
"satooshi/php-coveralls": "^1.0",
"squizlabs/php_codesniffer": "^3.5"
"squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^0.12.11"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

parameters:
level: max
checkMissingIterableValueType: false
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- '%Call to an undefined method Illuminate\\\Cache\\\CacheManager::remember\(\).%'
- '%Parameter #1 \$function of function call_user_func_array expects callable\(\)\: mixed, array\(hisorange\\BrowserDetect\\Contracts\\ResultInterface, string\) given\.%'
2 changes: 1 addition & 1 deletion src/Contracts/PayloadInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function setValue(string $key, $value): void;
* Get a value if exists in the data schema,
* returns null if the key does not exists.
*
* @param sting $key Temporary data key.
* @param string $key Temporary data key.
*
* @return mixed
*/
Expand Down
11 changes: 10 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,16 @@ public function __call(string $method, array $params)
*/
public function detect(): ResultInterface
{
return $this->parse($this->request->server('HTTP_USER_AGENT'));
// Cuts the agent string at 2048 byte, anything longer will be a DoS attack.
$userAgentStringRaw = $this->request->server('HTTP_USER_AGENT');

if (is_string($userAgentStringRaw)) {
$userAgentString = substr($userAgentStringRaw, 0, 2048);
} else {
$userAgentString = 'Unknown';
}

return $this->parse($userAgentString);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public function isIE(): bool
*/
public function isIEVersion(int $version, string $operator = '='): bool
{
return ($this->isIE && version_compare($this->browserVersion, $version, $operator));
return ($this->isIE && version_compare($this->browserVersion, (string) $version, $operator));
}

/**
Expand Down Expand Up @@ -386,7 +386,7 @@ public function mobileGrade(): string
/**
* @inheritdoc
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->toArray();
}
Expand Down
8 changes: 5 additions & 3 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ class ServiceProvider extends BaseServiceProvider
* Register the custom blade directives.
*
* @inheritDoc
*
* @return void
*/
public function boot()
public function boot(): void
{
$this->registerDirectives();
}

/**
* Register the blade directives.
*/
protected function registerDirectives()
protected function registerDirectives(): void
{
Blade::if(
'desktop',
Expand Down Expand Up @@ -62,7 +64,7 @@ function ($fn) {
*
* @inheritdoc
*/
public function register()
public function register(): void
{
$this->app->singleton('browser-detect', Parser::class);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Stages/BrowserDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ public function __invoke(PayloadInterface $payload): ResultInterface
*/
protected function trimVersion(string $version): string
{
return trim(preg_replace('%(^0.0.0$|\.0\.0$|\.0$)%', '', $version), '.');
return trim((string) preg_replace('%(^0.0.0$|\.0\.0$|\.0$)%', '', $version), '.');
}
}
4 changes: 2 additions & 2 deletions src/Stages/DeviceDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __invoke(PayloadInterface $payload): PayloadInterface
'model' => $detector->getModel(),
];

if ($platform !== null) {
if ($platform !== null && is_array($platform)) {
if (! empty($platform['name'])) {
$payload->setValue('platformFamily', $platform['name']);
}
Expand All @@ -46,7 +46,7 @@ public function __invoke(PayloadInterface $payload): PayloadInterface
}
}

if ($browser !== null) {
if ($browser !== null && is_array($browser)) {
if (! empty($browser['name'])) {
$payload->setValue('browserFamily', $browser['name']);
}
Expand Down
19 changes: 9 additions & 10 deletions src/Stages/MobileDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace hisorange\BrowserDetect\Stages;

use Mobile_Detect;
use hisorange\BrowserDetect\Contracts\StageInterface;
use hisorange\BrowserDetect\Contracts\PayloadInterface;

Expand All @@ -18,38 +19,36 @@ class MobileDetect implements StageInterface
*/
public function __invoke(PayloadInterface $payload): PayloadInterface
{
$class = class_exists('Mobile_Detect') ? 'Mobile_Detect' : 'MobileDetect';

$result = new $class();
$result = new Mobile_Detect();
$result->setHttpHeaders(['HTTP_FAKE_HEADER' => 'Mobile\Detect\Header']);
$result->setUserAgent($payload->getAgent());

// Need to test for tablet first, because most of the tablet are mobile too.
if ($result->isTablet()) {
$payload->setValue('isTablet', true);
$payload->setValue('mobileGrade', (string) $result->mobileGrade());
$payload->setValue('deviceModel', (string) $this->filter($result, $class::getTabletDevices()));
$payload->setValue('deviceModel', (string) $this->filter($result, Mobile_Detect::getTabletDevices()));
} elseif ($result->isMobile()) {
$payload->setValue('isMobile', true);
$payload->setValue('mobileGrade', (string) $result->mobileGrade());
$payload->setValue('deviceModel', (string) $this->filter($result, $class::getPhoneDevices()));
$payload->setValue('deviceModel', (string) $this->filter($result, Mobile_Detect::getPhoneDevices()));
}

$payload->setValue('platformFamily', $this->filter($result, $class::getOperatingSystems()));
$payload->setValue('browserFamily', $this->filter($result, $class::getBrowsers()));
$payload->setValue('platformFamily', $this->filter($result, Mobile_Detect::getOperatingSystems()));
$payload->setValue('browserFamily', $this->filter($result, Mobile_Detect::getBrowsers()));

return $payload;
}

/**
* Filter through the choices to find the matching one.
*
* @param Mobile_Detect|MobileDetect $result
* @param array $choices
* @param Mobile_Detect $result
* @param array $choices
*
* @return string|null
*/
protected function filter($result, array $choices): ?string
protected function filter(Mobile_Detect $result, array $choices): ?string
{
foreach ($choices as $key => $regex) {
if ($result->is($key) and stripos($key, 'generic') === false) {
Expand Down

0 comments on commit 8c41169

Please sign in to comment.