Skip to content

Commit 17c382e

Browse files
authored
One pull request to (hopefully) clear the board on the problems Matt and his team found (#36)
Close #33 Close #32 Close #31 Close #28 Ready for Laravel 8 once dependencies I don't control are updated.
1 parent 2f2f4ee commit 17c382e

11 files changed

+276
-52
lines changed

.php_cs

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->in(__DIR__.DIRECTORY_SEPARATOR.'tests')
5+
->in(__DIR__.DIRECTORY_SEPARATOR.'src')
6+
->append(['.php_cs']);
7+
8+
$rules = [
9+
'array_syntax' => ['syntax' => 'short'],
10+
'binary_operator_spaces' => [
11+
'default' => 'single_space',
12+
'operators' => ['=>' => null],
13+
],
14+
'blank_line_after_namespace' => true,
15+
'blank_line_after_opening_tag' => true,
16+
'blank_line_before_statement' => [
17+
'statements' => ['return'],
18+
],
19+
'braces' => true,
20+
'cast_spaces' => true,
21+
'class_attributes_separation' => [
22+
'elements' => ['method'],
23+
],
24+
'class_definition' => true,
25+
'concat_space' => [
26+
'spacing' => 'none',
27+
],
28+
'declare_equal_normalize' => true,
29+
'elseif' => true,
30+
'encoding' => true,
31+
'full_opening_tag' => true,
32+
'fully_qualified_strict_types' => true, // added by Shift
33+
'function_declaration' => true,
34+
'function_typehint_space' => true,
35+
'heredoc_to_nowdoc' => true,
36+
'include' => true,
37+
'increment_style' => ['style' => 'post'],
38+
'indentation_type' => true,
39+
'linebreak_after_opening_tag' => true,
40+
'line_ending' => true,
41+
'lowercase_cast' => true,
42+
'lowercase_constants' => true,
43+
'lowercase_keywords' => true,
44+
'lowercase_static_reference' => true, // added from Symfony
45+
'magic_method_casing' => true, // added from Symfony
46+
'magic_constant_casing' => true,
47+
'method_argument_space' => true,
48+
'native_function_casing' => true,
49+
'no_alias_functions' => true,
50+
'no_extra_blank_lines' => [
51+
'tokens' => [
52+
'extra',
53+
'throw',
54+
'use',
55+
'use_trait',
56+
],
57+
],
58+
'no_blank_lines_after_class_opening' => true,
59+
'no_blank_lines_after_phpdoc' => true,
60+
'no_closing_tag' => true,
61+
'no_empty_phpdoc' => true,
62+
'no_empty_statement' => true,
63+
'no_leading_import_slash' => true,
64+
'no_leading_namespace_whitespace' => true,
65+
'no_mixed_echo_print' => [
66+
'use' => 'echo',
67+
],
68+
'no_multiline_whitespace_around_double_arrow' => true,
69+
'multiline_whitespace_before_semicolons' => [
70+
'strategy' => 'no_multi_line',
71+
],
72+
'no_short_bool_cast' => true,
73+
'no_singleline_whitespace_before_semicolons' => true,
74+
'no_spaces_after_function_name' => true,
75+
'no_spaces_around_offset' => true,
76+
'no_spaces_inside_parenthesis' => true,
77+
'no_trailing_comma_in_list_call' => true,
78+
'no_trailing_comma_in_singleline_array' => true,
79+
'no_trailing_whitespace' => true,
80+
'no_trailing_whitespace_in_comment' => true,
81+
'no_unneeded_control_parentheses' => true,
82+
'no_unreachable_default_argument_value' => true,
83+
'no_useless_return' => true,
84+
'no_whitespace_before_comma_in_array' => true,
85+
'no_whitespace_in_blank_line' => true,
86+
'normalize_index_brace' => true,
87+
'not_operator_with_successor_space' => true,
88+
'object_operator_without_whitespace' => true,
89+
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
90+
'phpdoc_indent' => true,
91+
'phpdoc_inline_tag' => true,
92+
'phpdoc_no_access' => true,
93+
'phpdoc_no_package' => true,
94+
'phpdoc_no_useless_inheritdoc' => true,
95+
'phpdoc_scalar' => true,
96+
'phpdoc_single_line_var_spacing' => true,
97+
'phpdoc_summary' => true,
98+
'phpdoc_to_comment' => true,
99+
'phpdoc_trim' => true,
100+
'phpdoc_types' => true,
101+
'phpdoc_var_without_name' => true,
102+
'psr4' => true,
103+
'self_accessor' => true,
104+
'short_scalar_cast' => true,
105+
'simplified_null_return' => false, // disabled by Shift
106+
'single_blank_line_at_eof' => true,
107+
'single_blank_line_before_namespace' => true,
108+
'single_class_element_per_statement' => true,
109+
'single_import_per_statement' => true,
110+
'single_line_after_imports' => true,
111+
'single_line_comment_style' => [
112+
'comment_types' => ['hash'],
113+
],
114+
'single_quote' => true,
115+
'space_after_semicolon' => true,
116+
'standardize_not_equals' => true,
117+
'switch_case_semicolon_to_colon' => true,
118+
'switch_case_space' => true,
119+
'ternary_operator_spaces' => true,
120+
'trailing_comma_in_multiline_array' => true,
121+
'trim_array_spaces' => true,
122+
'unary_operator_spaces' => true,
123+
'visibility_required' => [
124+
'elements' => ['method', 'property'],
125+
],
126+
'whitespace_after_comma_in_array' => true,
127+
];
128+
129+
return PhpCsFixer\Config::create()
130+
->setUsingCache(true)
131+
->setRules($rules)
132+
->setRiskyAllowed(true)
133+
->setFinder($finder);

.phpunit.result.cache

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
C:37:"PHPUnit\Runner\DefaultTestResultCache":1332:{a:2:{s:7:"defects";a:9:{s:87:"Tests\MakeSheetModelCommandTest::it_creates_a_sheet_model_without_committing_any_crimes";i:3;s:49:"Tests\SheetModelTest::can_read_from_google_sheets";i:4;s:64:"Tests\SheetModelTest::does_not_hit_google_sheets_if_cache_exists";i:4;s:49:"Tests\SheetModelTest::can_do_basic_eloquent_stuff";i:4;s:43:"Tests\SheetModelTest::can_infer_id_from_row";i:3;s:45:"Tests\SheetModelTest::can_use_defined_headers";i:4;s:42:"Tests\SheetModelTest::can_invalidate_cache";i:3;s:53:"Tests\SheetModelTest::can_invalidate_cache_by_request";i:3;s:86:"Tests\SheetModelTest::will_bail_out_without_creating_cache_file_if_error_reading_sheet";i:4;}s:5:"times";a:9:{s:87:"Tests\MakeSheetModelCommandTest::it_creates_a_sheet_model_without_committing_any_crimes";d:1.032;s:49:"Tests\SheetModelTest::can_read_from_google_sheets";d:1.425;s:64:"Tests\SheetModelTest::does_not_hit_google_sheets_if_cache_exists";d:0.608;s:49:"Tests\SheetModelTest::can_do_basic_eloquent_stuff";d:0.665;s:43:"Tests\SheetModelTest::can_infer_id_from_row";d:0.794;s:45:"Tests\SheetModelTest::can_use_defined_headers";d:0.838;s:42:"Tests\SheetModelTest::can_invalidate_cache";d:0.635;s:53:"Tests\SheetModelTest::can_invalidate_cache_by_request";d:0.922;s:86:"Tests\SheetModelTest::will_bail_out_without_creating_cache_file_if_error_reading_sheet";d:0.269;}}}

.styleci.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
risky: true
2+
version: 7
3+
preset: laravel
4+
finder:
5+
exclude:
6+
- "config"
7+
- "vendor"
8+
name: "*.php"
9+
not-name:
10+
- "*.blade.php"
11+
- "_ide_helper.php"

composer.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
"prefer-stable": true,
1414
"require": {
1515
"php": "^7.3",
16-
"illuminate/support": "^6.0|^7.0",
16+
"illuminate/support": ">=6.0",
1717
"revolution/laravel-google-sheets": "^5.0",
1818
"calebporzio/sushi": "^2.0",
1919
"ext-json": "*"
2020
},
2121
"require-dev": {
22-
"orchestra/testbench": "^4.6"
22+
"orchestra/testbench": "^5.0",
23+
"friendsofphp/php-cs-fixer": "^2.16"
2324
},
2425
"autoload": {
2526
"psr-4": {
@@ -36,5 +37,13 @@
3637
"laravel": {
3738
"providers": "Grosv\\EloquentSheets\\EloquentSheetsProvider"
3839
}
40+
},
41+
"scripts": {
42+
"lint": "php-cs-fixer fix -v",
43+
"test:unit": "phpunit",
44+
"test": [
45+
"@lint",
46+
"@test:unit"
47+
]
3948
}
4049
}

phpunit.xml.dist

+16-25
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
verbose="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false">
12-
<testsuites>
13-
<testsuite name="Test Suite">
14-
<directory>tests</directory>
15-
</testsuite>
16-
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
22-
<php>
23-
<env name="APP_ENV" value="testing"/>
24-
<env name="APP_DEBUG" value="true"/>
25-
</php>
26-
</phpunit>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
3+
<coverage>
4+
<include>
5+
<directory suffix=".php">src/</directory>
6+
</include>
7+
</coverage>
8+
<testsuites>
9+
<testsuite name="Test Suite">
10+
<directory>tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
<php>
14+
<env name="APP_ENV" value="testing"/>
15+
<env name="APP_DEBUG" value="true"/>
16+
</php>
17+
</phpunit>

phpunit.xml.dist.bak

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="true">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<php>
23+
<env name="APP_ENV" value="testing"/>
24+
<env name="APP_DEBUG" value="true"/>
25+
</php>
26+
</phpunit>

src/Commands/MakeSheetModelCommand.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function handle()
4444
$this->modelPath .= '/'.$this->modelName.'.php';
4545
$this->calculateForgetUri();
4646

47-
if (!$this->confirm('Ready to write model '.$this->fullyQualfiedModelName.' at '.$this->modelPath.'?')) {
47+
if (! $this->confirm('Ready to write model '.$this->fullyQualfiedModelName.' at '.$this->modelPath.'?')) {
4848
return;
4949
}
5050
File::put($this->modelPath, $this->makeSubstitutions());

src/ForgetSheet.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,34 @@
33
namespace Grosv\EloquentSheets;
44

55
use Illuminate\Support\Facades\File;
6+
use Illuminate\Support\Str;
67

78
class ForgetSheet
89
{
910
public function execute($id)
1011
{
11-
File::delete(config('sushi.cache-path').'/'.$id.'.sqlite');
12+
if (File::isFile(config('sushi.cache-path').'/'.$id.'.sqlite')) {
13+
File::delete(config('sushi.cache-path').'/'.$id.'.sqlite');
14+
}
15+
if (File::isFile(storage_path('framework/cache/'.$id.'.sqlite'))) {
16+
File::delete(storage_path('framework/cache/'.$id.'.sqlite'));
17+
}
18+
19+
$this->triggerCallback($id);
1220

1321
return response()->noContent();
1422
}
23+
24+
protected function triggerCallback($id)
25+
{
26+
$class = 'App\\'.Str::studly(str_replace('sushi-', '', $id));
27+
if (! class_exists($class)) {
28+
return;
29+
}
30+
$model = new $class();
31+
32+
if (method_exists($model, 'onForget')) {
33+
$model->onForget();
34+
}
35+
}
1536
}

src/SheetModel.php

+25-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Google_Client;
66
use Google_Service_Sheets;
77
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Support\Facades\File;
89
use Illuminate\Support\Str;
910
use Revolution\Google\Sheets\Sheets;
1011
use Sushi\Sushi;
@@ -19,29 +20,33 @@ class SheetModel extends Model
1920
protected $headerRow;
2021
public $primaryKey = 'id';
2122
public $cacheName;
23+
private $cacheDirectory;
2224
protected $headers;
2325

2426
public function __construct()
2527
{
2628
parent::__construct();
27-
$this->cacheDirectory = realpath(config('sushi.cache-path', storage_path('framework/cache')));
29+
$this->cacheDirectory = File::isDirectory(config('sushi.cache-path', storage_path('framework/cache'))) ?
30+
config('sushi.cache-path', storage_path('framework/cache')) :
31+
File::makeDirectory(config('sushi.cache-path', storage_path('framework/cache')));
32+
2833
$this->cacheName = $this->getCacheName();
2934
}
3035

3136
public function getRows()
3237
{
33-
return !empty($this->rows) ? $this->rows : $this->loadFromSheet();
38+
return ! empty($this->rows) ? $this->rows : $this->loadFromSheet();
3439
}
3540

3641
public function invalidateCache()
3742
{
38-
if (!file_exists(config('sushi.cache-path').'/'.config('sushi.cache-prefix', 'sushi').'-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite')) {
43+
if (! file_exists($this->cacheDirectory.'/'.config('sushi.cache-prefix', 'sushi').'-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite')) {
3944
return;
4045
}
41-
unlink(config('sushi.cache-path').'/'.config('sushi.cache-prefix', 'sushi').'-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite');
46+
unlink($this->cacheDirectory.'/'.config('sushi.cache-prefix', 'sushi').'-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite');
4247
}
4348

44-
public function loadFromSheet(): array
49+
public function loadFromSheet(): ?array
4550
{
4651
$sheets = new Sheets();
4752
$client = new Google_Client(config('google'));
@@ -50,30 +55,35 @@ public function loadFromSheet(): array
5055
$sheets->setService($service);
5156
$inferId = 0;
5257

53-
$sheet = $sheets->spreadsheet($this->spreadsheetId)->sheetById($this->sheetId)->get();
58+
try {
59+
$sheet = $sheets->spreadsheet($this->spreadsheetId)->sheetById($this->sheetId)->get();
60+
} catch (\Exception $e) {
61+
$this->invalidateCache();
62+
throw $e;
63+
}
5464

5565
$headers = is_array($this->headers) ? collect($this->headers) : collect($sheet->pull($this->headerRow - 1));
5666

57-
if (!$headers->contains($this->primaryKey)) {
67+
if (! $headers->contains($this->primaryKey)) {
5868
$headers->push($this->primaryKey);
5969
$inferId = 1;
6070
}
6171

6272
$rows = collect([]);
6373

6474
$sheet->each(function ($row) use ($headers, $rows, &$inferId) {
65-
if ($inferId) {
66-
$row[] = $inferId++;
67-
}
75+
$record = [];
6876

6977
// append empty cols inside the row to match the number of cols in header
7078
foreach ($headers as $index => $header) {
71-
if (!isset($row[$index])) {
72-
$row[$index] = '';
73-
}
79+
$record[$header] = $row[$index] ?? '';
80+
}
81+
82+
if ($inferId) {
83+
$record[$this->primaryKey] = $inferId++;
7484
}
7585

76-
$rows->push($headers->combine($row));
86+
$rows->push($headers->combine($record));
7787
});
7888

7989
return $rows->toArray();
@@ -91,6 +101,6 @@ public function getSpreadsheetId()
91101

92102
public function getCacheName()
93103
{
94-
return !is_null($this->getConnection()) ? explode('.', basename($this->getConnection()->getDatabaseName()))[0] : null;
104+
return ! is_null($this->getConnection()) ? explode('.', basename($this->getConnection()->getDatabaseName()))[0] : null;
95105
}
96106
}

0 commit comments

Comments
 (0)