Skip to content

Commit eed7ef1

Browse files
authored
Merge pull request #1 from CloCkWeRX/phpcs
Add PHPCS and correct app/Importer/ for PSR-12 (errors only)
2 parents b0d183c + 906e30d commit eed7ef1

11 files changed

+104
-117
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ Add codespell as a linter:
1111
`pip install codespell`
1212

1313
Add a precommit hook:
14-
`cp contrib/hooks/pre-commit .git/hooks/`
14+
`cp contrib/hooks/pre-commit .git/hooks/`
15+
16+
Add phpcs:
17+
`curl -OL https://phars.phpcodesniffer.com/phpcs.phar`
18+
19+
Check against PSR12:
20+
`php phpcs.phar -n --standard=PSR12 app/`

app/Importer/AccessoryImporter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function createAccessoryIfNotExists($row)
2828
$accessory = Accessory::where('name', $this->item['name'])->first();
2929
if ($accessory) {
3030
if (! $this->updating) {
31-
$this->log('A matching Accessory '.$this->item['name'].' already exists. ');
31+
$this->log('A matching Accessory ' . $this->item['name'] . ' already exists. ');
3232

3333
return;
3434
}
@@ -50,7 +50,7 @@ public function createAccessoryIfNotExists($row)
5050
// This sets an attribute on the Loggable trait for the action log
5151
$accessory->setImported(true);
5252
if ($accessory->save()) {
53-
$this->log('Accessory '.$this->item['name'].' was created');
53+
$this->log('Accessory ' . $this->item['name'] . ' was created');
5454

5555
return;
5656
}

app/Importer/AssetImporter.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ protected function handle($row)
4747
if ($customFieldValue) {
4848
if ($customField->field_encrypted == 1) {
4949
$this->item['custom_fields'][$customField->db_column_name()] = Crypt::encrypt($customFieldValue);
50-
$this->log('Custom Field '.$customField->name.': '.Crypt::encrypt($customFieldValue));
50+
$this->log('Custom Field ' . $customField->name . ': ' . Crypt::encrypt($customFieldValue));
5151
} else {
5252
$this->item['custom_fields'][$customField->db_column_name()] = $customFieldValue;
53-
$this->log('Custom Field '.$customField->name.': '.$customFieldValue);
53+
$this->log('Custom Field ' . $customField->name . ': ' . $customFieldValue);
5454
}
5555
} else {
5656
// Clear out previous data.
@@ -76,11 +76,11 @@ public function createAssetIfNotExists(array $row)
7676
$editingAsset = false;
7777
$asset_tag = $this->findCsvMatch($row, 'asset_tag');
7878

79-
if (empty($asset_tag)){
79+
if (empty($asset_tag)) {
8080
$asset_tag = Asset::autoincrement_asset();
8181
}
8282

83-
$asset = Asset::where(['asset_tag'=> (string) $asset_tag])->first();
83+
$asset = Asset::where(['asset_tag' => (string) $asset_tag])->first();
8484
if ($asset) {
8585
if (! $this->updating) {
8686
$exists_error = trans('general.import_asset_tag_exists', ['asset_tag' => $asset_tag]);
@@ -93,7 +93,7 @@ public function createAssetIfNotExists(array $row)
9393
$editingAsset = true;
9494
} else {
9595
$this->log('No Matching Asset, Creating a new one');
96-
$asset = new Asset;
96+
$asset = new Asset();
9797
}
9898

9999
// If no status ID is found
@@ -137,7 +137,7 @@ public function createAssetIfNotExists(array $row)
137137
* We use this to backdate the checkin action further down
138138
*/
139139
$checkin_date = date('Y-m-d H:i:s');
140-
if ($this->item['last_checkin']!='') {
140+
if ($this->item['last_checkin'] != '') {
141141
$item['last_checkin'] = $this->parseOrNullDate('last_checkin', 'datetime');
142142
$checkout_date = $this->item['last_checkin'];
143143
}
@@ -146,24 +146,24 @@ public function createAssetIfNotExists(array $row)
146146
* We use this to backdate the checkout action further down
147147
*/
148148
$checkout_date = date('Y-m-d H:i:s');
149-
if ($this->item['last_checkout']!='') {
149+
if ($this->item['last_checkout'] != '') {
150150
$item['last_checkout'] = $this->parseOrNullDate('last_checkout', 'datetime');
151151
$checkout_date = $this->item['last_checkout'];
152152
}
153153

154-
if ($this->item['expected_checkin']!='') {
154+
if ($this->item['expected_checkin'] != '') {
155155
$item['expected_checkin'] = $this->parseOrNullDate('expected_checkin');
156156
}
157157

158-
if ($this->item['last_audit_date']!='') {
158+
if ($this->item['last_audit_date'] != '') {
159159
$item['last_audit_date'] = $this->parseOrNullDate('last_audit_date');
160160
}
161161

162-
if ($this->item['next_audit_date']!='') {
162+
if ($this->item['next_audit_date'] != '') {
163163
$item['next_audit_date'] = $this->parseOrNullDate('next_audit_date');
164164
}
165165

166-
if ($this->item['asset_eol_date']!='') {
166+
if ($this->item['asset_eol_date'] != '') {
167167
$item['asset_eol_date'] = $this->parseOrNullDate('asset_eol_date');
168168
}
169169

@@ -185,26 +185,23 @@ public function createAssetIfNotExists(array $row)
185185
$asset->setImported(true);
186186

187187
if ($asset->save()) {
188-
189-
$this->log('Asset '.$this->item['name'].' with serial number '.$this->item['serial'].' was created');
188+
$this->log('Asset ' . $this->item['name'] . ' with serial number ' . $this->item['serial'] . ' was created');
190189

191190
// If we have a target to checkout to, lets do so.
192191
//-- created_by is a property of the abstract class Importer, which this class inherits from and it's set by
193192
//-- the class that needs to use it (command importer or GUI importer inside the project).
194193
if (isset($target) && ($target !== false)) {
195-
if (!is_null($asset->assigned_to)){
194+
if (!is_null($asset->assigned_to)) {
196195
if ($asset->assigned_to != $target->id) {
197196
event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), auth()->user(), 'Checkin from CSV Importer', $checkin_date));
198197
}
199198
}
200199

201-
$asset->fresh()->checkOut($target, $this->created_by, $checkout_date, null, 'Checkout from CSV Importer', $asset->name);
200+
$asset->fresh()->checkOut($target, $this->created_by, $checkout_date, null, 'Checkout from CSV Importer', $asset->name);
202201
}
203202

204203
return;
205204
}
206-
$this->logError($asset, 'Asset "'.$this->item['name'].'"');
205+
$this->logError($asset, 'Asset "' . $this->item['name'] . '"');
207206
}
208-
209-
210207
}

app/Importer/AssetModelImporter.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function createAssetModelIfNotExists(array $row)
4444

4545
if ($assetModel) {
4646
if (! $this->updating) {
47-
$this->log('A matching Model '.$this->item['name'].' already exists');
47+
$this->log('A matching Model ' . $this->item['name'] . ' already exists');
4848
return;
4949
}
5050

@@ -104,15 +104,13 @@ public function createAssetModelIfNotExists(array $row)
104104
}
105105

106106
if ($assetModel->save()) {
107-
$this->log('AssetModel '.$assetModel->name.' created or updated from CSV import');
107+
$this->log('AssetModel ' . $assetModel->name . ' created or updated from CSV import');
108108
return $assetModel;
109-
110109
} else {
111110
$this->log($assetModel->getErrors()->first());
112-
$this->addErrorToBag($assetModel, $assetModel->getErrors()->keys()[0], $assetModel->getErrors()->first());
111+
$this->addErrorToBag($assetModel, $assetModel->getErrors()->keys()[0], $assetModel->getErrors()->first());
113112
return $assetModel->getErrors();
114113
}
115-
116114
}
117115

118116

@@ -130,9 +128,8 @@ public function createAssetModelIfNotExists(array $row)
130128
public function fetchDepreciation($depreciation_name) : ?int
131129
{
132130
if ($depreciation_name != '') {
133-
134131
if ($depreciation = Depreciation::where('name', '=', $depreciation_name)->first()) {
135-
$this->log('A matching Depreciation '.$depreciation_name.' already exists');
132+
$this->log('A matching Depreciation ' . $depreciation_name . ' already exists');
136133
return $depreciation->id;
137134
}
138135
}
@@ -154,15 +151,15 @@ public function createOrFetchCustomFieldset($fieldset_name) : ?int
154151
$fieldset = CustomFieldset::where('name', '=', $fieldset_name)->first();
155152

156153
if ($fieldset) {
157-
$this->log('A matching fieldset '.$fieldset_name.' already exists');
154+
$this->log('A matching fieldset ' . $fieldset_name . ' already exists');
158155
return $fieldset->id;
159156
}
160157

161158
$fieldset = new CustomFieldset();
162159
$fieldset->name = $fieldset_name;
163160

164161
if ($fieldset->save()) {
165-
$this->log('Fieldset '.$fieldset_name.' was created');
162+
$this->log('Fieldset ' . $fieldset_name . ' was created');
166163

167164
return $fieldset->id;
168165
}
@@ -171,4 +168,4 @@ public function createOrFetchCustomFieldset($fieldset_name) : ?int
171168

172169
return null;
173170
}
174-
}
171+
}

app/Importer/ComponentImporter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ protected function handle($row)
2222
* Create a component if a duplicate does not exist
2323
*
2424
* @author Daniel Melzter
25-
* @since 3.0
25+
* @since 3.0
2626
*/
2727
public function createComponentIfNotExists()
2828
{
@@ -33,7 +33,7 @@ public function createComponentIfNotExists()
3333
->first();
3434

3535
if ($component) {
36-
$this->log('A matching Component '.$this->item['name'].' with serial '.$this->item['serial'].' already exists. ');
36+
$this->log('A matching Component ' . $this->item['name'] . ' with serial ' . $this->item['serial'] . ' already exists. ');
3737
if (! $this->updating) {
3838
$this->log('Skipping Component');
3939

@@ -46,14 +46,14 @@ public function createComponentIfNotExists()
4646
return;
4747
}
4848
$this->log('No matching component, creating one');
49-
$component = new Component;
49+
$component = new Component();
5050
$component->created_by = auth()->id();
5151
$component->fill($this->sanitizeItemForStoring($component));
5252

5353
// This sets an attribute on the Loggable trait for the action log
5454
$component->setImported(true);
5555
if ($component->save()) {
56-
$this->log('Component '.$this->item['name'].' was created');
56+
$this->log('Component ' . $this->item['name'] . ' was created');
5757

5858
// If we have an asset tag, checkout to that asset.
5959
if (isset($this->item['asset_tag']) && ($asset = Asset::where('asset_tag', $this->item['asset_tag'])->first())) {

app/Importer/ConsumableImporter.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ public function createConsumableIfNotExists($row)
2828
{
2929
$consumable = Consumable::where('name', trim($this->item['name']))->first();
3030
if ($consumable) {
31-
3231
if (! $this->updating) {
33-
$this->log('A matching Consumable '.$this->item['name'].' already exists. ');
32+
$this->log('A matching Consumable ' . $this->item['name'] . ' already exists. ');
3433
return;
3534
}
3635
$this->log('Updating Consumable');
@@ -48,7 +47,7 @@ public function createConsumableIfNotExists($row)
4847
// This sets an attribute on the Loggable trait for the action log
4948
$consumable->setImported(true);
5049
if ($consumable->save()) {
51-
$this->log('Consumable '.$this->item['name'].' was created');
50+
$this->log('Consumable ' . $this->item['name'] . ' was created');
5251

5352
return;
5453
}

app/Importer/Importer.php

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ protected function logError($item, $field)
272272
}
273273
}
274274

275-
protected function addErrorToBag($item, $field, $error_message)
275+
protected function addErrorToBag($item, $field, $error_message)
276276
{
277277
if ($this->errorCallback) {
278278
call_user_func($this->errorCallback, $item, $field, [$field => [$error_message]]);
@@ -299,8 +299,8 @@ protected function createOrFetchUser($row, $type = 'user')
299299
'first_name' => $this->findCsvMatch($row, 'first_name'),
300300
'last_name' => $this->findCsvMatch($row, 'last_name'),
301301
'email' => $this->findCsvMatch($row, 'email'),
302-
'manager_id'=> '',
303-
'department_id' => '',
302+
'manager_id' => '',
303+
'department_id' => '',
304304
'username' => $this->findCsvMatch($row, 'username'),
305305
'activated' => $this->fetchHumanBoolean($this->findCsvMatch($row, 'activated')),
306306
'remote' => $this->fetchHumanBoolean(($this->findCsvMatch($row, 'remote'))),
@@ -314,7 +314,7 @@ protected function createOrFetchUser($row, $type = 'user')
314314
// Maybe we're lucky and the username was passed and it already exists.
315315
if (!empty($user_array['username'])) {
316316
if ($user = User::where('username', $user_array['username'])->first()) {
317-
$this->log('User '.$user_array['username'].' already exists');
317+
$this->log('User ' . $user_array['username'] . ' already exists');
318318
return $user;
319319
}
320320
}
@@ -352,7 +352,7 @@ protected function createOrFetchUser($row, $type = 'user')
352352

353353
// Check for a matching username one more time after trying to guess username.
354354
if ($user = User::where('username', $user_array['username'])->first()) {
355-
$this->log('User '.$user_array['username'].' already exists');
355+
$this->log('User ' . $user_array['username'] . ' already exists');
356356
return $user;
357357
}
358358
}
@@ -363,7 +363,7 @@ protected function createOrFetchUser($row, $type = 'user')
363363
}
364364

365365
// No luck finding a user on username or first name, let's create one.
366-
$user = new User;
366+
$user = new User();
367367

368368
$user->first_name = $user_array['first_name'];
369369
$user->last_name = $user_array['last_name'];
@@ -374,14 +374,14 @@ protected function createOrFetchUser($row, $type = 'user')
374374
$user->activated = 1;
375375
$user->password = $this->tempPassword;
376376

377-
Log::debug('Creating a user with the following attributes: '.print_r($user_array, true));
377+
Log::debug('Creating a user with the following attributes: ' . print_r($user_array, true));
378378

379379
if ($user->save()) {
380-
$this->log('User '.$user_array['username'].' created');
380+
$this->log('User ' . $user_array['username'] . ' created');
381381
return $user;
382382
}
383383

384-
$this->logError($user, 'User "'.$user_array['username'].'" was not able to be created.');
384+
$this->logError($user, 'User "' . $user_array['username'] . '" was not able to be created.');
385385

386386
return false;
387387
}
@@ -395,7 +395,7 @@ protected function findUserByNumber($user_name)
395395
{
396396
// A number was given instead of a name
397397
if (is_numeric($user_name)) {
398-
$this->log('User '.$user_name.' is a number - lets see if it matches a user id');
398+
$this->log('User ' . $user_name . ' is a number - lets see if it matches a user id');
399399

400400
return User::find($user_name);
401401
}
@@ -521,7 +521,7 @@ public function createOrFetchDepartment($user_department_name)
521521
$department = Department::where('name', '=', $user_department_name)->first();
522522

523523
if ($department) {
524-
$this->log('A matching Department '.$user_department_name.' already exists');
524+
$this->log('A matching Department ' . $user_department_name . ' already exists');
525525

526526
return $department->id;
527527
}
@@ -530,7 +530,7 @@ public function createOrFetchDepartment($user_department_name)
530530
$department->name = $user_department_name;
531531

532532
if ($department->save()) {
533-
$this->log('Department '.$user_department_name.' was created');
533+
$this->log('Department ' . $user_department_name . ' was created');
534534

535535
return $department->id;
536536
}
@@ -554,11 +554,11 @@ public function fetchManager($user_manager_first_name, $user_manager_last_name)
554554
$manager = User::where('first_name', '=', $user_manager_first_name)
555555
->where('last_name', '=', $user_manager_last_name)->first();
556556
if ($manager) {
557-
$this->log('A matching Manager '.$user_manager_first_name.' '.$user_manager_last_name.' already exists');
557+
$this->log('A matching Manager ' . $user_manager_first_name . ' ' . $user_manager_last_name . ' already exists');
558558

559559
return $manager->id;
560560
}
561-
$this->log('No matching Manager '.$user_manager_first_name.' '.$user_manager_last_name.' found. If their user account is being created through this import, you should re-process this file again. ');
561+
$this->log('No matching Manager ' . $user_manager_first_name . ' ' . $user_manager_last_name . ' found. If their user account is being created through this import, you should re-process this file again. ');
562562

563563
return null;
564564
}
@@ -573,16 +573,15 @@ public function fetchManager($user_manager_first_name, $user_manager_last_name)
573573
* @return string|null
574574
575575
*/
576-
public function parseOrNullDate($field, $format = 'date') {
577-
576+
public function parseOrNullDate($field, $format = 'date')
577+
{
578578
$date_format = 'Y-m-d';
579579

580580
if ($format == 'datetime') {
581581
$date_format = 'Y-m-d H:i:s';
582582
}
583583

584584
if (array_key_exists($field, $this->item) && $this->item[$field] != '') {
585-
586585
try {
587586
$value = CarbonImmutable::parse($this->item[$field])->format($date_format);
588587
return $value;

0 commit comments

Comments
 (0)