Skip to content

Commit

Permalink
Added inital phpstan/larastan setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddanbrown committed Nov 5, 2021
1 parent d0aa10a commit bc291be
Show file tree
Hide file tree
Showing 20 changed files with 1,113 additions and 151 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/phpstan.yml
@@ -0,0 +1,41 @@
name: phpstan

on:
push:
branches-ignore:
- l10n_master
pull_request:
branches-ignore:
- l10n_master

jobs:
build:
runs-on: ubuntu-20.04
strategy:
matrix:
php: ['7.3']
steps:
- uses: actions/checkout@v1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: gd, mbstring, json, curl, xml, mysql, ldap

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer packages
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ matrix.php }}

- name: Install composer dependencies & Test
run: composer install --prefer-dist --no-interaction --ansi

- name: Run PHPStan
run: php${{ matrix.php }} ./vendor/bin/phpstan analyse --memory-limit=2G
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -23,4 +23,5 @@ nbproject
.settings/
webpack-stats.json
.phpunit.result.cache
.DS_Store
.DS_Store
phpstan.neon
2 changes: 1 addition & 1 deletion app/Api/ApiDocsGenerator.php
Expand Up @@ -28,7 +28,7 @@ public static function generateConsideringCache(): Collection
if (Cache::has($cacheKey) && config('app.env') === 'production') {
$docs = Cache::get($cacheKey);
} else {
$docs = (new static())->generate();
$docs = (new ApiDocsGenerator)->generate();
Cache::put($cacheKey, $docs, 60 * 24);
}

Expand Down
1 change: 0 additions & 1 deletion app/Auth/User.php
Expand Up @@ -178,7 +178,6 @@ protected function permissions(): Collection
->leftJoin('permission_role', 'ru.role_id', '=', 'permission_role.role_id')
->leftJoin('role_permissions', 'permission_role.permission_id', '=', 'role_permissions.id')
->where('ru.user_id', '=', $this->id)
->get()
->pluck('name');

return $this->permissions;
Expand Down
88 changes: 30 additions & 58 deletions app/Config/broadcasting.php
@@ -1,79 +1,51 @@
<?php

/**
* Caching configuration options.
* Broadcasting configuration options.
*
* Changes to these config files are not supported by BookStack and may break upon updates.
* Configuration should be altered via the `.env` file or environment variables.
* Do not edit this file unless you're happy to maintain any changes yourself.
*/

// MEMCACHED - Split out configuration into an array
if (env('CACHE_DRIVER') === 'memcached') {
$memcachedServerKeys = ['host', 'port', 'weight'];
$memcachedServers = explode(',', trim(env('MEMCACHED_SERVERS', '127.0.0.1:11211:100'), ','));
foreach ($memcachedServers as $index => $memcachedServer) {
$memcachedServerDetails = explode(':', $memcachedServer);
if (count($memcachedServerDetails) < 2) {
$memcachedServerDetails[] = '11211';
}
if (count($memcachedServerDetails) < 3) {
$memcachedServerDetails[] = '100';
}
$memcachedServers[$index] = array_combine($memcachedServerKeys, $memcachedServerDetails);
}
}

return [

// Default cache store to use
// Can be overridden at cache call-time
'default' => env('CACHE_DRIVER', 'file'),

// Available caches stores
'stores' => [

'apc' => [
'driver' => 'apc',
],

'array' => [
'driver' => 'array',
'serialize' => false,
],

'database' => [
'driver' => 'database',
'table' => 'cache',
'connection' => null,
'lock_connection' => null,
],

'file' => [
'driver' => 'file',
'path' => storage_path('framework/cache'),
// Default Broadcaster
// This option controls the default broadcaster that will be used by the
// framework when an event needs to be broadcast. This can be set to
// any of the connections defined in the "connections" array below.
'default' => env('BROADCAST_DRIVER', 'pusher'),

// Broadcast Connections
// Here you may define all of the broadcast connections that will be used
// to broadcast events to other systems or over websockets. Samples of
// each available type of connection are provided inside this array.
'connections' => [

'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => true,
],
],

'memcached' => [
'driver' => 'memcached',
'servers' => env('CACHE_DRIVER') === 'memcached' ? $memcachedServers : [],
'options' => [],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],

'redis' => [
'driver' => 'redis',
'connection' => 'default',
'lock_connection' => 'default',
'log' => [
'driver' => 'log',
],

'octane' => [
'driver' => 'octane',
'null' => [
'driver' => 'null',
],

],

// Cache key prefix
// Used to prevent collisions in shared cache systems.
'prefix' => env('CACHE_PREFIX', 'bookstack_cache'),

];
];
68 changes: 28 additions & 40 deletions app/Config/cache.php
Expand Up @@ -2,35 +2,37 @@

use Illuminate\Support\Str;

return [
/**
* Caching configuration options.
*
* Changes to these config files are not supported by BookStack and may break upon updates.
* Configuration should be altered via the `.env` file or environment variables.
* Do not edit this file unless you're happy to maintain any changes yourself.
*/

// MEMCACHED - Split out configuration into an array
if (env('CACHE_DRIVER') === 'memcached') {
$memcachedServerKeys = ['host', 'port', 'weight'];
$memcachedServers = explode(',', trim(env('MEMCACHED_SERVERS', '127.0.0.1:11211:100'), ','));
foreach ($memcachedServers as $index => $memcachedServer) {
$memcachedServerDetails = explode(':', $memcachedServer);
if (count($memcachedServerDetails) < 2) {
$memcachedServerDetails[] = '11211';
}
if (count($memcachedServerDetails) < 3) {
$memcachedServerDetails[] = '100';
}
$memcachedServers[$index] = array_combine($memcachedServerKeys, $memcachedServerDetails);
}
}

/*
|--------------------------------------------------------------------------
| Default Cache Store
|--------------------------------------------------------------------------
|
| This option controls the default cache connection that gets used while
| using this caching library. This connection is used when another is
| not explicitly specified when executing a given caching function.
|
*/
return [

// Default cache store to use
// Can be overridden at cache call-time
'default' => env('CACHE_DRIVER', 'file'),

/*
|--------------------------------------------------------------------------
| Cache Stores
|--------------------------------------------------------------------------
|
| Here you may define all of the cache "stores" for your application as
| well as their drivers. You may even define multiple stores for the
| same cache driver to group types of items stored in your caches.
|
| Supported drivers: "apc", "array", "database", "file",
| "memcached", "redis", "dynamodb", "octane", "null"
|
*/

// Available caches stores
'stores' => [

'apc' => [
Expand All @@ -56,15 +58,10 @@

'memcached' => [
'driver' => 'memcached',
'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
'sasl' => [
env('MEMCACHED_USERNAME'),
env('MEMCACHED_PASSWORD'),
],
'options' => [
// Memcached::OPT_CONNECT_TIMEOUT => 2000,
],
'servers' => env('CACHE_DRIVER') === 'memcached' ? $memcachedServers : [],
'servers' => $memcachedServers ?? [],
],

'redis' => [
Expand All @@ -73,15 +70,6 @@
'lock_connection' => 'default',
],

'dynamodb' => [
'driver' => 'dynamodb',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
'table' => env('DYNAMODB_CACHE_TABLE', 'cache'),
'endpoint' => env('DYNAMODB_ENDPOINT'),
],

'octane' => [
'driver' => 'octane',
],
Expand Down
2 changes: 1 addition & 1 deletion app/Config/database.php
Expand Up @@ -105,6 +105,6 @@
'migrations' => 'migrations',

// Redis configuration to use if set
'redis' => env('REDIS_SERVERS', false) ? $redisConfig : [],
'redis' => $redisConfig ?? [],

];
14 changes: 10 additions & 4 deletions app/Console/Commands/CreateAdmin.php
Expand Up @@ -4,6 +4,7 @@

use BookStack\Auth\UserRepo;
use Illuminate\Console\Command;
use Symfony\Component\Console\Command\Command as SymfonyCommand;

class CreateAdmin extends Command
{
Expand Down Expand Up @@ -49,27 +50,31 @@ public function handle()
$email = $this->ask('Please specify an email address for the new admin user');
}
if (mb_strlen($email) < 5 || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
return $this->error('Invalid email address provided');
$this->error('Invalid email address provided');
return SymfonyCommand::FAILURE;
}

if ($this->userRepo->getByEmail($email) !== null) {
return $this->error('A user with the provided email already exists!');
$this->error('A user with the provided email already exists!');
return SymfonyCommand::FAILURE;
}

$name = trim($this->option('name'));
if (empty($name)) {
$name = $this->ask('Please specify an name for the new admin user');
}
if (mb_strlen($name) < 2) {
return $this->error('Invalid name provided');
$this->error('Invalid name provided');
return SymfonyCommand::FAILURE;
}

$password = trim($this->option('password'));
if (empty($password)) {
$password = $this->secret('Please specify a password for the new admin user');
}
if (mb_strlen($password) < 5) {
return $this->error('Invalid password provided, Must be at least 5 characters');
$this->error('Invalid password provided, Must be at least 5 characters');
return SymfonyCommand::FAILURE;
}

$user = $this->userRepo->create(['email' => $email, 'name' => $name, 'password' => $password]);
Expand All @@ -79,5 +84,6 @@ public function handle()
$user->save();

$this->info("Admin account with email \"{$user->email}\" successfully created!");
return SymfonyCommand::SUCCESS;
}
}
3 changes: 2 additions & 1 deletion app/Entities/Repos/BookshelfRepo.php
Expand Up @@ -124,7 +124,8 @@ protected function updateBooks(Bookshelf $shelf, array $bookIds)

$syncData = Book::visible()
->whereIn('id', $bookIds)
->get(['id'])->pluck('id')->mapWithKeys(function ($bookId) use ($numericIDs) {
->pluck('id')
->mapWithKeys(function ($bookId) use ($numericIDs) {
return [$bookId => ['order' => $numericIDs->search($bookId)]];
});

Expand Down
4 changes: 2 additions & 2 deletions app/Entities/Tools/SearchOptions.php
Expand Up @@ -32,7 +32,7 @@ class SearchOptions
public static function fromString(string $search): self
{
$decoded = static::decode($search);
$instance = new static();
$instance = new SearchOptions;
foreach ($decoded as $type => $value) {
$instance->$type = $value;
}
Expand All @@ -55,7 +55,7 @@ public static function fromRequest(Request $request): self
return static::fromString($request->get('term'));
}

$instance = new static();
$instance = new SearchOptions;
$inputs = $request->only(['search', 'types', 'filters', 'exact', 'tags']);
$instance->searches = explode(' ', $inputs['search'] ?? []);
$instance->exacts = array_filter($inputs['exact'] ?? []);
Expand Down
2 changes: 2 additions & 0 deletions app/Entities/Tools/TrashCan.php
Expand Up @@ -323,6 +323,8 @@ protected function destroyEntity(Entity $entity): int
if ($entity instanceof Bookshelf) {
return $this->destroyShelf($entity);
}

return 0;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/StatusController.php
Expand Up @@ -20,9 +20,9 @@ public function show()
}),
'cache' => $this->trueWithoutError(function () {
$rand = Str::random();
Cache::set('status_test', $rand);
Cache::add('status_test', $rand);

return Cache::get('status_test') === $rand;
return Cache::pull('status_test') === $rand;
}),
'session' => $this->trueWithoutError(function () {
$rand = Str::random();
Expand Down

0 comments on commit bc291be

Please sign in to comment.