From 672721abbfccf54e25bfe9b6b5f19c1e5ea54aeb Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Sat, 6 May 2017 21:36:17 +0700 Subject: [PATCH] Apply fixes from StyleCI (#4) --- app/Helpers/helpers.php | 35 +++++++------------ app/Http/Controllers/BackupsController.php | 23 +++++++------ app/Http/Controllers/ProductsController.php | 29 +++++++++------- app/Http/Controllers/TransactionsController.php | 3 +- app/Http/Controllers/UnitsController.php | 9 +++-- app/Http/Requests/BackupUploadRequest.php | 7 ++-- app/Product.php | 2 +- app/Providers/AppServiceProvider.php | 3 +- app/Transaction.php | 3 +- database/factories/ModelFactory.php | 2 +- ...017_05_02_211915_create_product_units_table.php | 4 +-- resources/lang/id/backup.php | 2 +- resources/lang/id/cart.php | 2 +- resources/lang/id/master.php | 2 +- resources/lang/id/nav_menu.php | 2 +- resources/lang/id/product.php | 2 +- resources/lang/id/unit.php | 2 +- routes/web.php | 18 +++++----- tests/Feature/ManageProductsTest.php | 40 +++++++++++----------- tests/Feature/ManageUnitsTest.php | 20 +++++------ tests/Unit/Integration/TransactionTest.php | 4 +-- 21 files changed, 106 insertions(+), 108 deletions(-) diff --git a/app/Helpers/helpers.php b/app/Helpers/helpers.php index 5cc0924..d2a0006 100755 --- a/app/Helpers/helpers.php +++ b/app/Helpers/helpers.php @@ -33,30 +33,19 @@ function html_link_to_route($name, $title = null, $parameters = [], $attributes function formatSizeUnits($bytes) { - if ($bytes >= 1073741824) - { - $bytes = number_format($bytes / 1073741824, 2) . ' GB'; - } - elseif ($bytes >= 1048576) - { - $bytes = number_format($bytes / 1048576, 2) . ' MB'; - } - elseif ($bytes >= 1024) - { - $bytes = number_format($bytes / 1024, 2) . ' KB'; - } - elseif ($bytes > 1) - { - $bytes = $bytes . ' bytes'; - } - elseif ($bytes == 1) - { - $bytes = $bytes . ' byte'; - } - else - { + if ($bytes >= 1073741824) { + $bytes = number_format($bytes / 1073741824, 2).' GB'; + } elseif ($bytes >= 1048576) { + $bytes = number_format($bytes / 1048576, 2).' MB'; + } elseif ($bytes >= 1024) { + $bytes = number_format($bytes / 1024, 2).' KB'; + } elseif ($bytes > 1) { + $bytes = $bytes.' bytes'; + } elseif ($bytes == 1) { + $bytes = $bytes.' byte'; + } else { $bytes = '0 bytes'; } return $bytes; -} \ No newline at end of file +} diff --git a/app/Http/Controllers/BackupsController.php b/app/Http/Controllers/BackupsController.php index 9f93e83..63d5644 100644 --- a/app/Http/Controllers/BackupsController.php +++ b/app/Http/Controllers/BackupsController.php @@ -19,18 +19,18 @@ class BackupsController extends Controller $backups = \File::allFiles(storage_path('app/backup/db')); // Sort files by modified time DESC - usort($backups, function($a, $b) { + usort($backups, function ($a, $b) { return -1 * strcmp($a->getMTime(), $b->getMTime()); }); } - return view('backups.index',compact('backups')); + return view('backups.index', compact('backups')); } public function store(Request $request) { $this->validate($request, [ - 'file_name' => 'nullable|max:30|regex:/^[\w._-]+$/' + 'file_name' => 'nullable|max:30|regex:/^[\w._-]+$/', ]); try { @@ -38,7 +38,7 @@ class BackupsController extends Controller $fileName = $request->get('file_name') ?: date('Y-m-d_Hi'); $manager->makeBackup()->run('mysql', [ - new Destination('local', 'backup/db/' . $fileName) + new Destination('local', 'backup/db/'.$fileName), ], 'gzip'); return redirect()->route('backups.index'); @@ -49,23 +49,25 @@ class BackupsController extends Controller public function destroy($fileName) { - if (file_exists(storage_path('app/backup/db/') . $fileName)) { - unlink(storage_path('app/backup/db/') . $fileName); + if (file_exists(storage_path('app/backup/db/').$fileName)) { + unlink(storage_path('app/backup/db/').$fileName); } + return redirect()->route('backups.index'); } public function download($fileName) { - return response()->download(storage_path('app/backup/db/') . $fileName); + return response()->download(storage_path('app/backup/db/').$fileName); } public function restore($fileName) { try { $manager = app()->make(Manager::class); - $manager->makeRestore()->run('local', 'backup/db/' . $fileName, 'mysql', 'gzip'); - } catch (FileNotFoundException $e) {} + $manager->makeRestore()->run('local', 'backup/db/'.$fileName, 'mysql', 'gzip'); + } catch (FileNotFoundException $e) { + } return redirect()->route('backups.index'); } @@ -74,11 +76,10 @@ class BackupsController extends Controller { $file = $request->file('backup_file'); - if (file_exists(storage_path('app/backup/db/') . $file->getClientOriginalName()) == false) { + if (file_exists(storage_path('app/backup/db/').$file->getClientOriginalName()) == false) { $file->storeAs('backup/db', $file->getClientOriginalName()); } return redirect()->route('backups.index'); } - } diff --git a/app/Http/Controllers/ProductsController.php b/app/Http/Controllers/ProductsController.php index d5c986a..d48b611 100644 --- a/app/Http/Controllers/ProductsController.php +++ b/app/Http/Controllers/ProductsController.php @@ -11,31 +11,32 @@ class ProductsController extends Controller { $editableProduct = null; $q = $request->get('q'); - $products = Product::where(function($query) use ($q) { + $products = Product::where(function ($query) use ($q) { if ($q) { - $query->where('name', 'like', '%' . $q . '%'); + $query->where('name', 'like', '%'.$q.'%'); } }) ->orderBy('name') ->with('unit') ->paginate(25); - if (in_array($request->get('action'), ['edit','delete']) && $request->has('id')) + if (in_array($request->get('action'), ['edit', 'delete']) && $request->has('id')) { $editableProduct = Product::find($request->get('id')); + } - return view('products.index', compact('products','editableProduct')); + return view('products.index', compact('products', 'editableProduct')); } public function store(Request $request) { $this->validate($request, [ - 'name' => 'required|max:20', - 'cash_price' => 'required|numeric', + 'name' => 'required|max:20', + 'cash_price' => 'required|numeric', 'credit_price' => 'nullable|numeric', - 'unit_id' => 'required|numeric', + 'unit_id' => 'required|numeric', ]); - Product::create($request->only('name','cash_price','credit_price','unit_id')); + Product::create($request->only('name', 'cash_price', 'credit_price', 'unit_id')); flash(trans('product.created'), 'success'); @@ -45,14 +46,14 @@ class ProductsController extends Controller public function update(Request $request, $productId) { $this->validate($request, [ - 'name' => 'required|max:20', - 'cash_price' => 'required|numeric', + 'name' => 'required|max:20', + 'cash_price' => 'required|numeric', 'credit_price' => 'nullable|numeric', ]); - $routeParam = $request->only('page','q'); + $routeParam = $request->only('page', 'q'); - $product = Product::findOrFail($productId)->update($request->only('name','cash_price','credit_price','unit_id')); + $product = Product::findOrFail($productId)->update($request->only('name', 'cash_price', 'credit_price', 'unit_id')); flash(trans('product.updated'), 'success'); @@ -65,14 +66,16 @@ class ProductsController extends Controller 'product_id' => 'required|exists:products,id', ]); - $routeParam = $request->only('page','q'); + $routeParam = $request->only('page', 'q'); if ($request->get('product_id') == $productId && Product::findOrFail($productId)->delete()) { flash(trans('product.deleted'), 'success'); + return redirect()->route('products.index', $routeParam); } flash(trans('product.undeleted'), 'error'); + return back(); } } diff --git a/app/Http/Controllers/TransactionsController.php b/app/Http/Controllers/TransactionsController.php index af5558d..29d9628 100644 --- a/app/Http/Controllers/TransactionsController.php +++ b/app/Http/Controllers/TransactionsController.php @@ -9,7 +9,8 @@ class TransactionsController extends Controller { public function index(Request $request) { - $transactions = Transaction::orderBy('invoice_no','desc')->paginate(24); + $transactions = Transaction::orderBy('invoice_no', 'desc')->paginate(24); + return view('transactions.index', compact('transactions')); } diff --git a/app/Http/Controllers/UnitsController.php b/app/Http/Controllers/UnitsController.php index d7b9576..cc4eb2c 100644 --- a/app/Http/Controllers/UnitsController.php +++ b/app/Http/Controllers/UnitsController.php @@ -12,10 +12,11 @@ class UnitsController extends Controller $editableUnit = null; $units = Unit::withCount('products')->get(); - if (in_array($request->get('action'), ['edit','delete']) && $request->has('id')) + if (in_array($request->get('action'), ['edit', 'delete']) && $request->has('id')) { $editableUnit = Unit::find($request->get('id')); + } - return view('units.index', compact('units','editableUnit')); + return view('units.index', compact('units', 'editableUnit')); } public function store(Request $request) @@ -49,15 +50,17 @@ class UnitsController extends Controller $this->validate($request, [ 'unit_id' => 'required|exists:product_units,id|not_exists:products,unit_id', ], [ - 'unit_id.not_exists' => trans('unit.undeleted') + 'unit_id.not_exists' => trans('unit.undeleted'), ]); if ($request->get('unit_id') == $unit->id && $unit->delete()) { flash(trans('unit.deleted'), 'success'); + return redirect()->route('units.index'); } flash(trans('unit.undeleted'), 'error'); + return back(); } } diff --git a/app/Http/Requests/BackupUploadRequest.php b/app/Http/Requests/BackupUploadRequest.php index 2ce785b..51b6e90 100644 --- a/app/Http/Requests/BackupUploadRequest.php +++ b/app/Http/Requests/BackupUploadRequest.php @@ -24,7 +24,7 @@ class BackupUploadRequest extends FormRequest public function rules() { return [ - 'backup_file' => 'required|sql_gz' + 'backup_file' => 'required|sql_gz', ]; } @@ -39,9 +39,10 @@ class BackupUploadRequest extends FormRequest { $validator = parent::getValidatorInstance(); - $validator->addImplicitExtension('sql_gz', function($attribute, $value, $parameters) { - if ($value) + $validator->addImplicitExtension('sql_gz', function ($attribute, $value, $parameters) { + if ($value) { return $value->getClientOriginalExtension() == 'gz'; + } return false; }); diff --git a/app/Product.php b/app/Product.php index 2e05749..076e9c7 100644 --- a/app/Product.php +++ b/app/Product.php @@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model; class Product extends Model { - protected $fillable = ['name','cash_price','credit_price','unit_id']; + protected $fillable = ['name', 'cash_price', 'credit_price', 'unit_id']; public function getPrice($type = 'cash') { diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 332188f..bb8ffb7 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -14,8 +14,7 @@ class AppServiceProvider extends ServiceProvider public function boot() { require_once app_path().'/Helpers/helpers.php'; - \Validator::extend('not_exists', function($attribute, $value, $parameters) - { + \Validator::extend('not_exists', function ($attribute, $value, $parameters) { return \DB::table($parameters[0]) ->where($parameters[1], $value) ->count() < 1; diff --git a/app/Transaction.php b/app/Transaction.php index ecafd02..24c7e31 100644 --- a/app/Transaction.php +++ b/app/Transaction.php @@ -22,7 +22,8 @@ class Transaction extends Model foreach ($this->items as $item) { $pcsCount += $item['qty']; } - return count($this->items) . ' Item, ' . $pcsCount . ' Pcs'; + + return count($this->items).' Item, '.$pcsCount.' Pcs'; } public function getExchange() diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index ac9a68e..db8fc55 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -14,7 +14,7 @@ $factory->define(App\User::class, function (Faker\Generator $faker) { $factory->define(App\Product::class, function (Faker\Generator $faker) { return [ 'name' => $faker->name, - 'unit_id' => function() { + 'unit_id' => function () { return factory(App\Unit::class)->create()->id; }, 'cash_price' => 2000, diff --git a/database/migrations/2017_05_02_211915_create_product_units_table.php b/database/migrations/2017_05_02_211915_create_product_units_table.php index ff7747c..8d44324 100644 --- a/database/migrations/2017_05_02_211915_create_product_units_table.php +++ b/database/migrations/2017_05_02_211915_create_product_units_table.php @@ -1,8 +1,8 @@ 'Cancel Restore', 'confirm_restore' => 'YES, Restore Database!', 'upload' => 'Upload Backup File', -]; \ No newline at end of file +]; diff --git a/resources/lang/id/cart.php b/resources/lang/id/cart.php index 3dd47a2..62cbe40 100644 --- a/resources/lang/id/cart.php +++ b/resources/lang/id/cart.php @@ -3,5 +3,5 @@ return [ // Labels 'product_search' => 'Cari Produk', - 'list' => 'Transaksi', + 'list' => 'Transaksi', ]; diff --git a/resources/lang/id/master.php b/resources/lang/id/master.php index 1a924e9..517a03c 100644 --- a/resources/lang/id/master.php +++ b/resources/lang/id/master.php @@ -25,4 +25,4 @@ return [ // Attributes 'name' => 'Nama Master', 'description' => 'Deskripsi Master', -]; \ No newline at end of file +]; diff --git a/resources/lang/id/nav_menu.php b/resources/lang/id/nav_menu.php index a2296ea..8f5798d 100644 --- a/resources/lang/id/nav_menu.php +++ b/resources/lang/id/nav_menu.php @@ -2,4 +2,4 @@ return [ 'draft_list' => 'Draft Transaksi', -]; \ No newline at end of file +]; diff --git a/resources/lang/id/product.php b/resources/lang/id/product.php index 33bb148..1592d95 100644 --- a/resources/lang/id/product.php +++ b/resources/lang/id/product.php @@ -32,4 +32,4 @@ return [ 'name' => 'Nama Produk', 'cash_price' => 'Harga Tunai', 'credit_price' => 'Harga Kredit', -]; \ No newline at end of file +]; diff --git a/resources/lang/id/unit.php b/resources/lang/id/unit.php index 3368d7a..c4ca25c 100644 --- a/resources/lang/id/unit.php +++ b/resources/lang/id/unit.php @@ -23,4 +23,4 @@ return [ // Attributes 'name' => 'Nama Satuan', -]; \ No newline at end of file +]; diff --git a/routes/web.php b/routes/web.php index e8e511b..2bc02de 100644 --- a/routes/web.php +++ b/routes/web.php @@ -18,12 +18,12 @@ Route::get('/', function () { Auth::routes(); Route::group(['middleware' => 'auth'], function () { - /** + /* * Pages Routes */ Route::get('/home', 'CartController@index')->name('home'); - /** + /* * Cart / Trasanction Draft Routes */ Route::get('drafts', 'CartController@index')->name('cart.index'); @@ -38,28 +38,28 @@ Route::group(['middleware' => 'auth'], function () { Route::delete('cart/remove', 'CartController@remove')->name('cart.remove'); Route::delete('cart/destroy', 'CartController@destroy')->name('cart.destroy'); - /** + /* * Products Routes */ - Route::resource('products', 'ProductsController', ['except' => ['create','show','edit']]); + Route::resource('products', 'ProductsController', ['except' => ['create', 'show', 'edit']]); - /** + /* * Units Routes */ - Route::resource('units', 'UnitsController', ['except' => ['create','show','edit']]); + Route::resource('units', 'UnitsController', ['except' => ['create', 'show', 'edit']]); - /** + /* * Transactions Routes */ Route::get('transactions', ['as' => 'transactions.index', 'uses' => 'TransactionsController@index']); Route::get('transactions/{transaction}', ['as' => 'transactions.show', 'uses' => 'TransactionsController@show']); Route::get('transactions/{transaction}/pdf', ['as' => 'transactions.pdf', 'uses' => 'TransactionsController@pdf']); - /** + /* * Backup Restore Database Routes */ Route::post('backups/upload', ['as'=>'backups.upload', 'uses'=>'BackupsController@upload']); Route::post('backups/{fileName}/restore', ['as'=>'backups.restore', 'uses'=>'BackupsController@restore']); Route::get('backups/{fileName}/dl', ['as'=>'backups.download', 'uses'=>'BackupsController@download']); - Route::resource('backups','BackupsController', ['except' => ['create', 'show', 'edit']]); + Route::resource('backups', 'BackupsController', ['except' => ['create', 'show', 'edit']]); }); diff --git a/tests/Feature/ManageProductsTest.php b/tests/Feature/ManageProductsTest.php index e606be4..0ac763b 100644 --- a/tests/Feature/ManageProductsTest.php +++ b/tests/Feature/ManageProductsTest.php @@ -58,8 +58,8 @@ class ManageProductsTest extends BrowserKitTestCase $this->see(trans('product.created')); $this->seeInDatabase('products', [ - 'name' => 'Product 1', - 'cash_price' => 1000, + 'name' => 'Product 1', + 'cash_price' => 1000, 'credit_price' => 1200, ]); } @@ -72,8 +72,8 @@ class ManageProductsTest extends BrowserKitTestCase $product = factory(Product::class)->create(['name' => 'Testing 123']); $this->visit(route('products.index', ['q' => '123'])); - $this->click('edit-product-' . $product->id); - $this->seePageIs(route('products.index', ['action' => 'edit','id' => $product->id, 'q' => '123'])); + $this->click('edit-product-'.$product->id); + $this->seePageIs(route('products.index', ['action' => 'edit', 'id' => $product->id, 'q' => '123'])); $this->type('Product 1', 'name'); $this->type('1000', 'cash_price'); @@ -84,8 +84,8 @@ class ManageProductsTest extends BrowserKitTestCase $this->seePageIs(route('products.index', ['q' => '123'])); $this->seeInDatabase('products', [ - 'name' => 'Product 1', - 'cash_price' => 1000, + 'name' => 'Product 1', + 'cash_price' => 1000, 'credit_price' => 1200, ]); } @@ -110,8 +110,8 @@ class ManageProductsTest extends BrowserKitTestCase $this->see(trans('product.created')); $this->seeInDatabase('products', [ - 'name' => 'Product 1', - 'cash_price' => 1000, + 'name' => 'Product 1', + 'cash_price' => 1000, 'credit_price' => null, ]); } @@ -124,8 +124,8 @@ class ManageProductsTest extends BrowserKitTestCase $product = factory(Product::class)->create(); $this->visit(route('products.index')); - $this->click('edit-product-' . $product->id); - $this->seePageIs(route('products.index', ['action' => 'edit','id' => $product->id])); + $this->click('edit-product-'.$product->id); + $this->seePageIs(route('products.index', ['action' => 'edit', 'id' => $product->id])); $this->type('Product 1', 'name'); $this->type('1000', 'cash_price'); @@ -134,8 +134,8 @@ class ManageProductsTest extends BrowserKitTestCase $this->press(trans('product.update')); $this->seeInDatabase('products', [ - 'name' => 'Product 1', - 'cash_price' => 1000, + 'name' => 'Product 1', + 'cash_price' => 1000, 'credit_price' => 1200, ]); } @@ -147,17 +147,17 @@ class ManageProductsTest extends BrowserKitTestCase $product = factory(Product::class)->create(); $this->visit(route('products.index')); - $this->click('del-product-' . $product->id); - $this->seePageIs(route('products.index', ['action' => 'delete','id' => $product->id])); + $this->click('del-product-'.$product->id); + $this->seePageIs(route('products.index', ['action' => 'delete', 'id' => $product->id])); $this->seeInDatabase('products', [ - 'id' => $product->id + 'id' => $product->id, ]); $this->press(trans('app.delete_confirm_button')); $this->dontSeeInDatabase('products', [ - 'id' => $product->id + 'id' => $product->id, ]); } @@ -168,18 +168,18 @@ class ManageProductsTest extends BrowserKitTestCase $product = factory(Product::class)->create(['name' => 'Product 123']); $this->visit(route('products.index', ['q' => '123'])); - $this->click('del-product-' . $product->id); + $this->click('del-product-'.$product->id); - $this->seePageIs(route('products.index', ['action' => 'delete','id' => $product->id, 'q' => '123'])); + $this->seePageIs(route('products.index', ['action' => 'delete', 'id' => $product->id, 'q' => '123'])); $this->seeInDatabase('products', [ - 'id' => $product->id + 'id' => $product->id, ]); $this->press(trans('app.delete_confirm_button')); $this->seePageIs(route('products.index', ['q' => '123'])); $this->dontSeeInDatabase('products', [ - 'id' => $product->id + 'id' => $product->id, ]); } } diff --git a/tests/Feature/ManageUnitsTest.php b/tests/Feature/ManageUnitsTest.php index c7a142e..9e7e9dc 100644 --- a/tests/Feature/ManageUnitsTest.php +++ b/tests/Feature/ManageUnitsTest.php @@ -50,8 +50,8 @@ class ManageUnitsTest extends BrowserKitTestCase $unit = factory(Unit::class)->create(); $this->visit(route('units.index')); - $this->click('edit-unit-' . $unit->id); - $this->seePageIs(route('units.index', ['action' => 'edit','id' => $unit->id])); + $this->click('edit-unit-'.$unit->id); + $this->seePageIs(route('units.index', ['action' => 'edit', 'id' => $unit->id])); $this->type('Unit 1', 'name'); $this->press(trans('unit.update')); @@ -71,17 +71,17 @@ class ManageUnitsTest extends BrowserKitTestCase $unit = factory(Unit::class)->create(); $this->visit(route('units.index')); - $this->click('del-unit-' . $unit->id); - $this->seePageIs(route('units.index', ['action' => 'delete','id' => $unit->id])); + $this->click('del-unit-'.$unit->id); + $this->seePageIs(route('units.index', ['action' => 'delete', 'id' => $unit->id])); $this->seeInDatabase('product_units', [ - 'id' => $unit->id + 'id' => $unit->id, ]); $this->press(trans('app.delete_confirm_button')); $this->dontSeeInDatabase('product_units', [ - 'id' => $unit->id + 'id' => $unit->id, ]); } @@ -93,16 +93,16 @@ class ManageUnitsTest extends BrowserKitTestCase $unitId = $product->unit_id; $this->visit(route('units.index')); - $this->click('del-unit-' . $unitId); - $this->seePageIs(route('units.index', ['action' => 'delete','id' => $unitId])); + $this->click('del-unit-'.$unitId); + $this->seePageIs(route('units.index', ['action' => 'delete', 'id' => $unitId])); $this->press(trans('app.delete_confirm_button')); $this->see(trans('unit.undeleted')); - $this->seePageIs(route('units.index', ['action' => 'delete','id' => $unitId])); + $this->seePageIs(route('units.index', ['action' => 'delete', 'id' => $unitId])); $this->seeInDatabase('product_units', [ - 'id' => $unitId + 'id' => $unitId, ]); } } diff --git a/tests/Unit/Integration/TransactionTest.php b/tests/Unit/Integration/TransactionTest.php index d5bf630..580c5b4 100644 --- a/tests/Unit/Integration/TransactionTest.php +++ b/tests/Unit/Integration/TransactionTest.php @@ -13,7 +13,7 @@ class TransactionTest extends TestCase /** @test */ public function it_has_items_count_attribute() { - $transaction = new Transaction; + $transaction = new Transaction(); $items = [ [ @@ -45,7 +45,7 @@ class TransactionTest extends TestCase /** @test */ public function it_has_get_exchange_method() { - $transaction = new Transaction; + $transaction = new Transaction(); $transaction->payment = 100000; $transaction->total = 90000;