Skip to content

Commit

Permalink
Merge pull request #34 from mavimo/add-phpstan
Browse files Browse the repository at this point in the history
Add phpstan for static analysis and fixes - WIP
  • Loading branch information
leeqvip committed Feb 17, 2020
2 parents 7373f46 + 18eab00 commit f21fedd
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 22 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -13,6 +13,7 @@ install:
script:
- mkdir -p build/logs
- vendor/bin/phpunit
- vendor/bin/phpstan

after_script:
- travis_retry vendor/bin/php-coveralls -v
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -29,7 +29,8 @@
},
"require-dev": {
"phpunit/phpunit": "~7.0|~8.0",
"php-coveralls/php-coveralls": "^2.1"
"php-coveralls/php-coveralls": "^2.1",
"phpstan/phpstan": "^0.12"
},
"autoload-dev": {
"psr-4": {
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
@@ -0,0 +1,8 @@
parameters:
reportUnmatchedIgnoredErrors: false
checkMissingIterableValueType: false
tipsOfTheDay: false
level: max
paths:
- src
- tests
5 changes: 0 additions & 5 deletions src/CachedEnforcer.php
Expand Up @@ -22,11 +22,6 @@ class CachedEnforcer extends Enforcer
*/
protected $enableCache;

/**
* @var
*/
protected static $locker;

/**
* CachedEnforcer constructor.
*
Expand Down
5 changes: 4 additions & 1 deletion src/Config/Config.php
Expand Up @@ -11,7 +11,7 @@
*
* @author techlee@qq.com
*/
class Config implements ConfigContract
final class Config implements ConfigContract
{
const DEFAULT_SECTION = 'default';

Expand All @@ -21,6 +21,9 @@ class Config implements ConfigContract

const DEFAULT_MULTI_LINE_SEPARATOR = '\\';

/**
* @var array<string, array<string, string>>
*/
public $data = [];

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Config/ConfigContract.php
Expand Up @@ -4,6 +4,8 @@

namespace Casbin\Config;

use Casbin\Exceptions\CasbinException;

/**
* Interface ConfigContract.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Enforcer.php
Expand Up @@ -63,14 +63,14 @@ class Enforcer
/**
* Adapter.
*
* @var Adapter
* @var Adapter|null
*/
protected $adapter;

/**
* Watcher.
*
* @var Watcher
* @var Watcher|null
*/
protected $watcher;

Expand Down
3 changes: 3 additions & 0 deletions src/Model/FunctionMap.php
Expand Up @@ -13,6 +13,9 @@
*/
class FunctionMap
{
/**
* @var array<string, \Closure>
*/
private $functions = [];

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Model/Model.php
Expand Up @@ -13,6 +13,8 @@
/**
* Class Model.
*
* @implements ArrayAccess<string|int, mixed>
*
* @author techlee@qq.com
*/
class Model implements ArrayAccess
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Policy.php
Expand Up @@ -176,7 +176,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): bool
* @param string $sec
* @param string $ptype
* @param int $fieldIndex
* @param mixed ...$fieldValues
* @param string ...$fieldValues
*
* @return bool
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Persist/Adapters/FileAdapter.php
Expand Up @@ -98,6 +98,11 @@ public function savePolicy(Model $model): void
protected function loadPolicyFile(Model $model): void
{
$file = fopen($this->filePath, 'rb');

if (false === $file) {
throw new InvalidFilePathException(sprintf('Unable to access to the specified path "%s"', $this->filePath));
}

while ($line = fgets($file)) {
$this->loadPolicyLine(trim($line), $model);
}
Expand Down
22 changes: 10 additions & 12 deletions src/Persist/Adapters/FileFilteredAdapter.php
Expand Up @@ -22,7 +22,7 @@ class FileFilteredAdapter extends FileAdapter implements FilteredAdapter
/**
* filtered variable.
*
* @var boolean
* @var bool
*/
protected $filtered;

Expand Down Expand Up @@ -92,15 +92,14 @@ public function isFiltered(): bool
* SavePolicy saves all policy rules to the storage.
*
* @param Model $model
*
* @return void
*/
public function savePolicy(Model $model): void
{
if ($this->filtered) {
throw new CannotSaveFilteredPolicy('cannot save a filtered policy');
}
parent.savePolicy($model);

parent::savePolicy($model);
}

/**
Expand All @@ -109,12 +108,15 @@ public function savePolicy(Model $model): void
* @param Model $model
* @param Filter $filter
* @param callable $handler
*
* @return void
*/
protected function loadFilteredPolicyFile(Model $model, Filter $filter, callable $handler): void
{
$file = fopen($this->filePath, 'rb');

if (false === $file) {
throw new InvalidFilePathException(sprintf('Unable to access to the specified path "%s"', $this->filePath));
}

while ($line = fgets($file)) {
$line = trim($line);
if (self::filterLine($line, $filter)) {
Expand All @@ -130,14 +132,10 @@ protected function loadFilteredPolicyFile(Model $model, Filter $filter, callable
* @param string $line
* @param Filter $filter
*
* @return boolean
* @return bool
*/
protected static function filterLine(string $line, Filter $filter): bool
{
if (is_null($filter)) {
return false;
}

$p = explode(',', $line);
if (0 == \count($p)) {
return true;
Expand All @@ -164,7 +162,7 @@ protected static function filterLine(string $line, Filter $filter): bool
* @param array $line
* @param array $filter
*
* @return boolean
* @return bool
*/
protected static function filterWords(array $line, array $filter): bool
{
Expand Down
6 changes: 6 additions & 0 deletions src/Util/Util.php
Expand Up @@ -4,6 +4,8 @@

namespace Casbin\Util;

use Casbin\Exceptions\CasbinException;

/**
* Class Util.
*
Expand Down Expand Up @@ -31,6 +33,10 @@ public static function escapeAssertion(string $s): string
return $m[1].$m[2].'_';
}, $s);

if (null === $s) {
throw new CasbinException(sprintf('Unable to escape assertion "%s"', $s));
}

return $s;
}

Expand Down

0 comments on commit f21fedd

Please sign in to comment.