Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit c6a60da

Browse files
Housekeeping fixes (#7)
Apply fixes reported by static analysis. Ensure global functions are not overloaded. Decouple controller from ContainerAware.
1 parent 0757d4e commit c6a60da

File tree

13 files changed

+128
-155
lines changed

13 files changed

+128
-155
lines changed

composer.json

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
"require": {
3131
"php": ">=7.1",
32+
"ext-json": "*",
3233
"doctrine/doctrine-bundle": "~1.6",
3334
"doctrine/orm": "~2.1",
3435
"stof/doctrine-extensions-bundle": "*",
@@ -41,38 +42,39 @@
4142
"behat/symfony2-extension": "^2.1",
4243
"friendsofphp/php-cs-fixer": "*",
4344
"jakub-onderka/php-parallel-lint": "^1.0",
44-
"phpstan/phpstan": "^0.10.2",
45-
"phpunit/phpunit": "^5.7",
46-
"satooshi/php-coveralls": "^2.0",
45+
"php-coveralls/php-coveralls": "^2.1",
46+
"phpstan/phpstan": "^0.10.5",
47+
"phpstan/phpstan-phpunit": "^0.10.0",
48+
"phpunit/phpunit": "^7.4",
4749
"sensiolabs/security-checker": "^4.1",
4850
"symfony/phpunit-bridge": "^3.0|^4.0"
4951
},
5052

5153
"scripts": {
5254
"check-code-style": [
53-
"bin/php-cs-fixer fix --config='./.php_cs' --show-progress=none --dry-run --no-interaction --diff -v"
55+
"php-cs-fixer fix --config='./.php_cs' --show-progress=none --dry-run --no-interaction --diff -v"
5456
],
5557
"check-security": [
56-
"bin/security-checker security:check"
58+
"security-checker security:check"
5759
],
5860
"fix-code-style": [
59-
"bin/php-cs-fixer fix --config='./.php_cs' --show-progress=none --no-interaction --diff -v"
61+
"php-cs-fixer fix --config='./.php_cs' --show-progress=none --no-interaction --diff -v"
6062
],
6163
"run-static-analysis": [
62-
"bin/phpstan analyse --level=1 src/"
64+
"phpstan analyse --level=7 src/"
6365
],
6466
"run-static-analysis-including-tests": [
6567
"@run-static-analysis",
66-
"bin/phpstan analyse --level=1 tests/"
68+
"phpstan analyse --level=3 tests/"
6769
],
6870
"run-tests": [
69-
"bin/phpunit"
71+
"phpunit"
7072
],
7173
"run-tests-with-clover": [
72-
"bin/phpunit --coverage-clover build/logs/clover.xml"
74+
"phpunit --coverage-clover build/logs/clover.xml"
7375
],
7476
"validate-files": [
75-
"bin/parallel-lint --exclude vendor --exclude bin ."
77+
"parallel-lint --exclude vendor --exclude bin ."
7678
]
7779
},
7880

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
includes:
2+
- vendor/phpstan/phpstan-phpunit/extension.neon

phpunit.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
convertWarningsToExceptions="true"
77
stopOnFailure="false"
88
bootstrap="vendor/autoload.php"
9-
verbose="true"
10-
syntaxCheck="true">
9+
verbose="true">
1110

1211
<testsuites>
13-
<testsuite>
12+
<testsuite name="VisitorBundle Full Test Suite">
1413
<directory>./tests</directory>
1514
</testsuite>
1615
</testsuites>

src/Controller/DeviceController.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,38 @@
66

77
use Alpha\VisitorTrackingBundle\Entity\Device;
88
use Alpha\VisitorTrackingBundle\Entity\Session;
9-
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9+
use Alpha\VisitorTrackingBundle\Manager\DeviceFingerprintManager;
10+
use Alpha\VisitorTrackingBundle\Storage\SessionStore;
11+
use Doctrine\ORM\EntityManager;
12+
use Psr\Log\LoggerInterface;
1013
use Symfony\Component\HttpFoundation\Request;
1114
use Symfony\Component\HttpFoundation\Response;
1215

13-
class DeviceController extends Controller
16+
class DeviceController
1417
{
18+
private $entityManager;
19+
20+
private $logger;
21+
22+
private $sessionStore;
23+
24+
private $deviceFingerprintManager;
25+
26+
public function __construct(
27+
EntityManager $entityManager,
28+
LoggerInterface $logger,
29+
SessionStore $sessionStore,
30+
DeviceFingerprintManager $deviceFingerprintManager
31+
) {
32+
$this->entityManager = $entityManager;
33+
$this->logger = $logger;
34+
$this->sessionStore = $sessionStore;
35+
$this->deviceFingerprintManager = $deviceFingerprintManager;
36+
}
37+
1538
public function fingerprintAction(Request $request): Response
1639
{
17-
$em = $this->getDoctrine()->getManager();
18-
19-
$session = $this->get('alpha.visitor_tracking.storage.session')->getSession();
40+
$session = $this->sessionStore->getSession();
2041
$device = null;
2142

2243
if ($session instanceof Session) {
@@ -30,12 +51,12 @@ public function fingerprintAction(Request $request): Response
3051
$device->setFingerprint($request->getContent());
3152
$device->setSession($session);
3253

33-
$this->get('alpha.visitor_tracking.manager.device_fingerprint')->generateHashes($device);
54+
$this->deviceFingerprintManager->generateHashes($device);
3455

35-
$em->persist($device);
36-
$em->flush($device);
56+
$this->entityManager->persist($device);
57+
$this->entityManager->flush($device);
3758

38-
$this->get('logger')->debug(sprintf('A new device fingerprint was added with the id %d.', $device->getId()));
59+
$this->logger->debug(\sprintf('A new device fingerprint was added with the id %d.', $device->getId()));
3960

4061
return new Response('', 201);
4162
}

src/DependencyInjection/AlphaVisitorTrackingExtension.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public function load(array $configs, ContainerBuilder $container): void
1616
$configuration = new Configuration();
1717
$config = $this->processConfiguration($configuration, $configs);
1818

19-
2019
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
2120
$loader->load('services.yml');
2221

src/DependencyInjection/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function getConfigTreeBuilder(): TreeBuilder
1515
$treeBuilder = new TreeBuilder();
1616
$rootNode = $treeBuilder->root('alpha_visitor_tracking');
1717

18+
\assert($rootNode instanceof ArrayNodeDefinition);
1819
$rootNode->append($this->createSubscriberNode());
1920

2021
return $treeBuilder;
@@ -23,6 +24,7 @@ public function getConfigTreeBuilder(): TreeBuilder
2324
private function createSubscriberNode(): ArrayNodeDefinition
2425
{
2526
$root = (new TreeBuilder())->root('session_subscriber');
27+
\assert($root instanceof ArrayNodeDefinition);
2628
$root->addDefaultsIfNotSet();
2729

2830
$root->children()->arrayNode('firewall_blacklist')

src/Entity/Seed.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,11 @@ class Seed
3636

3737
/**
3838
* @var string
39+
*
3940
* @ORM\Column(type="string")
4041
*/
4142
protected $value;
4243

43-
/**
44-
* @param string $name
45-
* @param int $numberOfValues
46-
* @param array $weights
47-
*/
4844
public function __construct(string $name, int $numberOfValues, ?array $weights = null)
4945
{
5046
$this->name = $name;
@@ -102,7 +98,7 @@ public function getValue()
10298
private function setValue(int $numberOfValues, ?array $weights = null): void
10399
{
104100
if ($weights === null) {
105-
$weights = array_fill(1, $numberOfValues, 1);
101+
$weights = \array_fill(1, $numberOfValues, 1);
106102
}
107103

108104
if (\count($weights) !== $numberOfValues) {
@@ -111,7 +107,7 @@ private function setValue(int $numberOfValues, ?array $weights = null): void
111107

112108
// This does not need to be cryptographically secure, mt_rand() is roughly 2x faster than random_int().
113109
/** @noinspection RandomApiMigrationInspection */
114-
$random = mt_rand(1, array_sum($weights));
110+
$random = \mt_rand(1, (int) \array_sum($weights));
115111
$total = 0;
116112

117113
foreach ($weights as $seed => $weight) {

0 commit comments

Comments
 (0)