Skip to content

Commit 5b1d6f1

Browse files
authored
Merge pull request #281 from php-school/phpstan-and-cleanup
Fix phpstan and remove lots of unused code
2 parents e8c2ec5 + 65b03f4 commit 5b1d6f1

File tree

80 files changed

+651
-1087
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+651
-1087
lines changed

.env.dist

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ SEND_GRID_API_KEY=sendgripapikey
66
77
REDIS_HOST=redis
88
CACHE.ENABLE=false
9-
CACHE.FPC.ENABLE=false
109
DISPLAY_ERRORS=true
1110
GITHUB_CLIENT_ID=
1211
GITHUB_CLIENT_SECRET=

.github/workflows/php.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ jobs:
3333

3434
- name: Run phpcs
3535
run: composer cs
36-
#
37-
# - name: Run psalm
38-
# run: composer static
36+
37+
- name: Run PHPStan
38+
run: composer static

README.md

-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ docker compose exec php composer app:gen:blog
3232

3333
Then navigate to `http://localhost` !
3434

35-
Pages are cached on first view.
36-
If you need to clear the cache, run `docker compose exec php composer app:cc`.
37-
38-
You can disable the cache by setting `CACHE.FPC.ENABLE` to `false` in your `.env` file.
39-
4035
## Build CSS & JS
4136

4237
This needs to be done for the main website (non cloud) to run in development mode.

app/config.php

+7-55
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@
7171
use PhpSchool\Website\InputFilter\Login as LoginInputFilter;
7272
use PhpSchool\Website\InputFilter\SubmitWorkshop as SubmitWorkshopInputFilter;
7373
use PhpSchool\Website\InputFilter\WorkshopComposerJson as WorkshopComposerJsonInputFilter;
74-
use PhpSchool\Website\Middleware\FpcCache;
7574
use PhpSchool\Website\Middleware\Session as SessionMiddleware;
76-
use PhpSchool\Website\PhpRenderer;
7775
use PhpSchool\Website\Repository\DoctrineORMBlogRepository;
7876
use PhpSchool\Website\Repository\EventRepository;
7977
use PhpSchool\Website\Repository\WorkshopInstallRepository;
@@ -122,47 +120,23 @@
122120
'app' => factory(function (ContainerInterface $c): App {
123121
$app = Bridge::create($c);
124122
$app->addRoutingMiddleware();
125-
$app->add($c->get(FpcCache::class));
126123

127124
$app->add(function (Request $request, RequestHandler $handler) use($c) : Response {
128-
$renderer = $this->get(PhpRenderer::class);
129125
/** @var Session $session */
130126
$session = $this->get(Session::class);
131127

132128
$student = $session->get('student');
133129

134130
$request = $request->withAttribute('student', $student);
135-
$renderer->addAttribute('student', $student);
136-
$renderer->addAttribute(
137-
'totalExerciseCount',
138-
$c->get(CloudWorkshopRepository::class)->totalExerciseCount()
139-
);
140131

141132
return $handler->handle($request)
142133
->withHeader('cache-control', 'no-cache');
143134
});
144135
$app->add(StudentRefresher::class);
145136
$app->add(new SessionMiddleware(['name' => 'phpschool']));
146137

147-
$app->add(function (Request $request, RequestHandler $handler) use ($c){
148-
$renderer = $c->get(PhpRenderer::class);
149-
$renderer->addAttribute('userAgent', new Agent);
150-
$renderer->addAttribute('route', $request->getUri()->getPath());
151-
152-
return $handler->handle($request);
153-
});
154-
155138
return $app;
156139
}),
157-
FpcCache::class => factory(function (ContainerInterface $c): FpcCache {
158-
return new FpcCache($c->get('cache.fpc'));
159-
}),
160-
'cache.fpc' => factory(function (ContainerInterface $c): CacheInterface {
161-
if (!$c->get('config')['enablePageCache']) {
162-
return new NullAdapter;
163-
}
164-
return new RedisAdapter(new Predis\Client(['host' => $c->get('config')['redisHost']]), 'fpc');
165-
}),
166140
'cache' => factory(function (ContainerInterface $c): CacheInterface {
167141
if (!$c->get('config')['enableCache']) {
168142
return new NullAdapter;
@@ -183,18 +157,6 @@
183157

184158
return new RedisAdapter($redisConnection, 'default');
185159
}),
186-
PhpRenderer::class => factory(function (ContainerInterface $c): PhpRenderer {
187-
188-
$settings = $c->get('config')['renderer'];
189-
$renderer = new PhpRenderer(
190-
$settings['template_path'],
191-
[
192-
'links' => $c->get('config')['links'],
193-
],
194-
);
195-
196-
return $renderer;
197-
}),
198160
LoggerInterface::class => factory(function (ContainerInterface $c): LoggerInterface{
199161
$settings = $c->get('config')['logger'];
200162
$logger = new Logger($settings['name']);
@@ -215,7 +177,7 @@
215177

216178
//commands
217179
ClearCache::class => factory(function (ContainerInterface $c): ClearCache {
218-
return new ClearCache($c->get('cache.fpc'));
180+
return new ClearCache($c->get('cache'));
219181
}),
220182
CreateAdminUser::class => factory(function (ContainerInterface $c): CreateAdminUser {
221183
return new CreateAdminUser($c->get(EntityManagerInterface::class));
@@ -275,30 +237,28 @@
275237

276238
ClearCacheAction::class => function (ContainerInterface $c): ClearCacheAction {
277239
return new ClearCacheAction(
278-
$c->get('cache.fpc'),
240+
$c->get('cache'),
279241
);
280242
},
281243

282244
Requests::class => \DI\factory(function (ContainerInterface $c): Requests {
283245
return new Requests(
284246
$c->get(WorkshopRepository::class),
285-
$c->get(PhpRenderer::class)
286247
);
287248
}),
288249

289250
All::class => \DI\factory(function (ContainerInterface $c): All {
290251
return new All(
291252
$c->get(WorkshopRepository::class),
292253
$c->get(WorkshopInstallRepository::class),
293-
$c->get(PhpRenderer::class)
294254
);
295255
}),
296256

297257
Approve::class => \DI\factory(function (ContainerInterface $c): Approve {
298258
return new Approve(
299259
$c->get(WorkshopRepository::class),
300260
$c->get(WorkshopFeed::class),
301-
$c->get('cache.fpc'),
261+
$c->get('cache'),
302262
$c->get(EmailNotifier::class),
303263
$c->get(LoggerInterface::class)
304264
);
@@ -308,7 +268,7 @@
308268
return new Promote(
309269
$c->get(WorkshopRepository::class),
310270
$c->get(WorkshopFeed::class),
311-
$c->get('cache.fpc'),
271+
$c->get('cache'),
312272
);
313273
}),
314274

@@ -317,15 +277,14 @@
317277
$c->get(WorkshopRepository::class),
318278
$c->get(WorkshopInstallRepository::class),
319279
$c->get(WorkshopFeed::class),
320-
$c->get('cache.fpc'),
280+
$c->get('cache'),
321281
);
322282
}),
323283

324284
View::class => function (ContainerInterface $c): View {
325285
return new View(
326286
$c->get(WorkshopRepository::class),
327287
$c->get(WorkshopInstallRepository::class),
328-
$c->get(PhpRenderer::class)
329288
);
330289
},
331290

@@ -410,7 +369,7 @@
410369
},
411370

412371
EventAll::class => function (ContainerInterface $c): EventAll {
413-
return new EventAll($c->get(EventRepository::class), $c->get(PhpRenderer::class));
372+
return new EventAll($c->get(EventRepository::class));
414373
},
415374

416375
EventCreate::class => function (ContainerInterface $c): EventCreate {
@@ -424,14 +383,13 @@
424383
return new EventUpdate(
425384
$c->get(EventRepository::class),
426385
$c->get('form.event'),
427-
$c->get(PhpRenderer::class),
428386
);
429387
},
430388

431389
EventDelete::class => function (ContainerInterface $c): EventDelete {
432390
return new EventDelete(
433391
$c->get(EventRepository::class),
434-
$c->get('cache.fpc'),
392+
$c->get('cache'),
435393
);
436394
},
437395

@@ -473,7 +431,6 @@
473431
StudentAuthenticator::class => function (ContainerInterface $c): StudentAuthenticator {
474432
return new StudentAuthenticator(
475433
$c->get(Session::class),
476-
$c->get(StudentRepository::class)
477434
);
478435
},
479436

@@ -528,10 +485,6 @@ public function parse($markdown): string
528485
);
529486
},
530487

531-
Styles::class => function (ContainerInterface $c) {
532-
return new Styles($c->get(PhpRenderer::class));
533-
},
534-
535488
CloudWorkshopRepository::class => function (ContainerInterface $c): CloudWorkshopRepository {
536489
return new CloudWorkshopRepository($c->get(WorkshopRepository::class));
537490
},
@@ -602,7 +555,6 @@ public function parse($markdown): string
602555
'github-website' => 'https://github.com/php-school/phpschool.io',
603556
],
604557

605-
'enablePageCache' => filter_var($_ENV['CACHE.FPC.ENABLE'], FILTER_VALIDATE_BOOLEAN),
606558
'enableCache' => filter_var($_ENV['CACHE.ENABLE'], FILTER_VALIDATE_BOOLEAN),
607559
'redisHost' => $_ENV['REDIS_HOST'],
608560
'devMode' => filter_var($_ENV['DEV_MODE'], FILTER_VALIDATE_BOOLEAN),

composer.json

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"jenssegers/agent": "^2.3",
2525
"sendgrid/sendgrid": "^7.9",
2626
"vlucas/phpdotenv": "^5.3",
27-
"adamwathan/bootforms": "^0.9",
2827
"mnapoli/front-yaml": "^1.5",
2928
"laminas/laminas-inputfilter": "^2.28",
3029
"laminas/laminas-uri": "^2.11",

composer.lock

+1-95
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@
128128
$group->post('/tour/complete', TourComplete::class);
129129
$group->post('/reset', ResetState::class);
130130
})
131-
->add($container->get(StudentAuthenticator::class))
132-
->add(Styles::class);
131+
->add($container->get(StudentAuthenticator::class));
133132

134133
// Run app
135134
$app->run();

src/Action/Admin/Event/Create.php

+23-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,23 @@
1212
use Psr\Http\Message\ResponseInterface as Response;
1313
use Laminas\Filter\Exception\RuntimeException;
1414

15+
/**
16+
* @phpstan-import-type EventData from \PhpSchool\Website\InputFilter\Event
17+
*/
1518
class Create
1619
{
1720
use JsonUtils;
1821

1922
private EventRepository $repository;
23+
24+
/**
25+
* @var FormHandler<EventData>
26+
*/
2027
private FormHandler $formHandler;
2128

29+
/**
30+
* @param FormHandler<EventData> $formHandler
31+
*/
2232
public function __construct(
2333
EventRepository $repository,
2434
FormHandler $formHandler,
@@ -47,11 +57,23 @@ public function __invoke(Request $request, Response $response): MessageInterface
4757
);
4858
}
4959

60+
$date = \DateTime::createFromFormat('Y-m-d\TH:i', $values['date']);
61+
62+
if (false === $date) {
63+
return $this->withJson(
64+
[
65+
'success' => false,
66+
'form_errors' => ['date' => 'Invalid date format']
67+
],
68+
$response
69+
);
70+
}
71+
5072
$event = new Event(
5173
$values['name'],
5274
$values['description'],
5375
$values['link'] ?? null,
54-
\DateTime::createFromFormat('Y-m-d\TH:i', $values['date']),
76+
$date,
5577
$values['venue'],
5678
isset($values['poster']['tmp_name']) ? basename($values['poster']['tmp_name']) : null
5779
);

0 commit comments

Comments
 (0)