@sun-asterisk/sunlint 1.3.47 → 1.3.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/rules/rules-registry-generated.json +1717 -282
- package/core/architecture-integration.js +57 -15
- package/core/cli-action-handler.js +51 -36
- package/core/config-manager.js +6 -0
- package/core/config-merger.js +33 -0
- package/core/config-validator.js +37 -2
- package/core/file-targeting-service.js +148 -15
- package/core/init-command.js +118 -70
- package/core/output-service.js +12 -3
- package/core/project-detector.js +517 -0
- package/core/scoring-service.js +12 -6
- package/core/summary-report-service.js +9 -4
- package/core/tui-select.js +245 -0
- package/engines/arch-detect/rules/layered/l001-presentation-layer.js +7 -15
- package/engines/arch-detect/rules/layered/l002-business-layer.js +7 -15
- package/engines/arch-detect/rules/layered/l003-data-layer.js +7 -15
- package/engines/arch-detect/rules/layered/l004-model-layer.js +7 -15
- package/engines/arch-detect/rules/layered/l005-layer-separation.js +22 -2
- package/engines/arch-detect/rules/layered/l006-dependency-direction.js +8 -5
- package/engines/arch-detect/rules/modular/m005-no-deep-imports.js +67 -29
- package/engines/arch-detect/rules/presentation/pr001-view-layer.js +16 -9
- package/engines/arch-detect/rules/presentation/pr006-router-layer.js +33 -8
- package/engines/arch-detect/rules/presentation/pr007-interactor-layer.js +35 -6
- package/engines/arch-detect/rules/project-scanner/ps003-framework-detection.js +56 -10
- package/engines/impact/cli.js +54 -39
- package/engines/impact/config/default-config.js +105 -5
- package/engines/impact/core/impact-analyzer.js +12 -15
- package/engines/impact/core/utils/gitignore-parser.js +123 -0
- package/engines/impact/core/utils/method-call-graph.js +272 -87
- package/origin-rules/dart-en.md +1 -1
- package/origin-rules/go-en.md +231 -0
- package/origin-rules/php-en.md +107 -0
- package/origin-rules/python-en.md +113 -0
- package/origin-rules/ruby-en.md +607 -0
- package/package.json +1 -1
- package/scripts/copy-arch-detect.js +5 -1
- package/scripts/copy-impact-analyzer.js +5 -1
- package/scripts/generate-rules-registry.js +30 -14
- package/skill-assets/sunlint-code-quality/SKILL.md +3 -2
- package/skill-assets/sunlint-code-quality/rules/dart/C006-verb-noun-functions.md +45 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C013-no-dead-code.md +53 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C014-dependency-injection.md +92 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C017-no-constructor-logic.md +62 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C018-generic-errors.md +57 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C019-error-log-level.md +50 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C020-no-unused-imports.md +46 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C022-no-unused-variables.md +50 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C023-no-duplicate-names.md +56 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C024-centralize-constants.md +75 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C029-catch-log-root-cause.md +53 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C030-custom-error-classes.md +86 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C033-separate-data-access.md +90 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C035-error-context-logging.md +62 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C041-no-hardcoded-secrets.md +75 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C042-boolean-naming.md +73 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C052-widget-parsing.md +84 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C060-superclass-logic.md +91 -0
- package/skill-assets/sunlint-code-quality/rules/dart/C067-no-hardcoded-config.md +108 -0
- package/skill-assets/sunlint-code-quality/rules/go/G001-explicit-error-handling.md +53 -0
- package/skill-assets/sunlint-code-quality/rules/go/G002-context-first-argument.md +44 -0
- package/skill-assets/sunlint-code-quality/rules/go/G003-receiver-consistency.md +38 -0
- package/skill-assets/sunlint-code-quality/rules/go/G004-avoid-panic.md +49 -0
- package/skill-assets/sunlint-code-quality/rules/go/G005-goroutine-leak-prevention.md +49 -0
- package/skill-assets/sunlint-code-quality/rules/go/G006-interface-consumer-definition.md +45 -0
- package/skill-assets/sunlint-code-quality/rules/go/GN001-gin-binding-validation.md +57 -0
- package/skill-assets/sunlint-code-quality/rules/go/GN002-gin-error-response.md +48 -0
- package/skill-assets/sunlint-code-quality/rules/go/GN003-graceful-shutdown.md +57 -0
- package/skill-assets/sunlint-code-quality/rules/go/GN004-gin-route-logical-grouping.md +54 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/AGENTS.md +149 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN001-abort-after-response.md +75 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN002-request-context.md +64 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN003-bind-error-handling.md +70 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN004-dependency-injection.md +78 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN005-route-groups-middleware.md +71 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN006-http-status-codes.md +91 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN007-release-mode.md +64 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN008-struct-validation-tags.md +90 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN009-recovery-middleware.md +68 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN010-context-scope.md +68 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN011-middleware-concerns.md +92 -0
- package/skill-assets/sunlint-code-quality/rules/go-gin/GN012-no-log-sensitive.md +84 -0
- package/skill-assets/sunlint-code-quality/rules/java/J001-try-with-resources.md +86 -0
- package/skill-assets/sunlint-code-quality/rules/java/J002-equals-and-hashcode.md +88 -0
- package/skill-assets/sunlint-code-quality/rules/java/J003-string-comparison.md +72 -0
- package/skill-assets/sunlint-code-quality/rules/java/J004-use-java-time.md +91 -0
- package/skill-assets/sunlint-code-quality/rules/java/J005-no-print-stack-trace.md +80 -0
- package/skill-assets/sunlint-code-quality/rules/java/J006-no-system-println.md +89 -0
- package/skill-assets/sunlint-code-quality/rules/java/J007-proper-logger.md +91 -0
- package/skill-assets/sunlint-code-quality/rules/java/J008-thread-safe-singleton.md +119 -0
- package/skill-assets/sunlint-code-quality/rules/java/J009-utility-class-constructor.md +82 -0
- package/skill-assets/sunlint-code-quality/rules/java/J010-preserve-stack-trace.md +119 -0
- package/skill-assets/sunlint-code-quality/rules/java/J011-null-safe-compare.md +88 -0
- package/skill-assets/sunlint-code-quality/rules/java/J012-use-enum-collections.md +104 -0
- package/skill-assets/sunlint-code-quality/rules/java/J013-return-empty-not-null.md +102 -0
- package/skill-assets/sunlint-code-quality/rules/java/J014-hardcoded-crypto-key.md +108 -0
- package/skill-assets/sunlint-code-quality/rules/java/J015-optional-instead-of-null.md +109 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/AGENTS.md +124 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV001-form-request-validation.md +64 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV002-eager-load-no-n-plus-1.md +58 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV003-config-not-env.md +54 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV004-fillable-mass-assignment.md +51 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV005-policies-gates-authorization.md +71 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV006-queue-heavy-tasks.md +68 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV007-hash-passwords.md +51 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV008-route-model-binding.md +67 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV009-api-resources.md +72 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV010-chunk-large-datasets.md +58 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV011-db-transactions.md +73 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV012-service-layer.md +78 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV013-testing-factories.md +75 -0
- package/skill-assets/sunlint-code-quality/rules/php-laravel/LV014-service-container.md +61 -0
- package/skill-assets/sunlint-code-quality/rules/python/P001-mutable-default-argument.md +55 -0
- package/skill-assets/sunlint-code-quality/rules/python/P002-specify-file-encoding.md +45 -0
- package/skill-assets/sunlint-code-quality/rules/python/P003-context-manager-for-resources.md +54 -0
- package/skill-assets/sunlint-code-quality/rules/python/P004-no-bare-except.md +65 -0
- package/skill-assets/sunlint-code-quality/rules/python/P005-use-isinstance.md +60 -0
- package/skill-assets/sunlint-code-quality/rules/python/P006-timezone-aware-datetime.md +58 -0
- package/skill-assets/sunlint-code-quality/rules/python/P007-use-pathlib.md +62 -0
- package/skill-assets/sunlint-code-quality/rules/python/P008-no-wildcard-import.md +52 -0
- package/skill-assets/sunlint-code-quality/rules/python/P009-logging-lazy-format.md +50 -0
- package/skill-assets/sunlint-code-quality/rules/python/P010-exception-chaining.md +57 -0
- package/skill-assets/sunlint-code-quality/rules/python/P011-subprocess-check.md +59 -0
- package/skill-assets/sunlint-code-quality/rules/python/P012-requests-timeout.md +70 -0
- package/skill-assets/sunlint-code-quality/rules/python/P013-no-global-statement.md +73 -0
- package/skill-assets/sunlint-code-quality/rules/python/P014-no-modify-collection-while-iterating.md +66 -0
- package/skill-assets/sunlint-code-quality/rules/python/P015-prefer-fstrings.md +61 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/AGENTS.md +121 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR001-strong-parameters.md +55 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR002-eager-load-includes.md +51 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR003-service-objects.md +99 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR004-active-job-background.md +67 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR005-pagination.md +53 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR006-find-each-batches.md +53 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR007-http-status-codes.md +76 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR008-before-action-auth.md +77 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR009-rails-credentials.md +61 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR010-scopes.md +57 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR011-counter-cache.md +59 -0
- package/skill-assets/sunlint-code-quality/rules/ruby-rails/RR012-render-json-status.md +42 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C006-verb-noun-functions.md +37 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C013-no-dead-code.md +55 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C014-dependency-injection.md +69 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C017-no-constructor-logic.md +66 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C018-generic-errors.md +64 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C019-error-log-level.md +64 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C020-no-unused-imports.md +47 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C022-no-unused-variables.md +46 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C023-no-duplicate-names.md +55 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C024-centralize-constants.md +68 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C029-catch-log-root-cause.md +69 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C030-custom-error-classes.md +77 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C033-separate-data-access.md +89 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C035-error-context-logging.md +66 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C041-no-hardcoded-secrets.md +65 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C042-boolean-naming.md +60 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C052-controller-parsing.md +67 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C060-superclass-logic.md +95 -0
- package/skill-assets/sunlint-code-quality/rules/swift/C067-no-hardcoded-config.md +80 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S003-sql-injection.md +65 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S004-no-log-credentials.md +67 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S005-server-authorization.md +73 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S006-default-credentials.md +76 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S007-output-encoding.md +96 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S009-approved-crypto.md +86 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S010-csprng.md +71 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S011-insecure-deserialization.md +74 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S012-secrets-management.md +81 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S013-tls-connections.md +67 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S017-parameterized-queries.md +86 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S019-session-management.md +131 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S020-kvc-injection.md +91 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S025-input-validation.md +125 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S029-brute-force-protection.md +120 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S036-path-traversal.md +102 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S039-tls-certificate-validation.md +109 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S041-logout-invalidation.md +103 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S043-password-hashing.md +116 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S044-critical-changes-reauth.md +145 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S045-debug-info-exposure.md +116 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S046-unvalidated-redirect.md +140 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S051-token-expiry.md +134 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S053-jwt-validation.md +139 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S059-background-snapshot-protection.md +113 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S060-data-protection-api.md +106 -0
- package/skill-assets/sunlint-code-quality/rules/swift/S061-jailbreak-detection.md +132 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Put Literals First in String Comparisons to Avoid NullPointerException
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: variable.equals("literal") throws NPE if variable is null; reversing operands makes comparisons null-safe without extra null checks
|
|
5
|
+
tags: null-safety, best-practice, java, error-prone
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Put Literals First in String Comparisons to Avoid NullPointerException
|
|
9
|
+
|
|
10
|
+
When comparing a string variable to a known literal value, placing the literal on the **left** side of `.equals()` prevents a `NullPointerException` if the variable is `null`. Since a string literal is never `null`, calling `.equals()` on it is always safe.
|
|
11
|
+
|
|
12
|
+
**Incorrect (variable first — may throw NPE):**
|
|
13
|
+
|
|
14
|
+
```java
|
|
15
|
+
public boolean isActive(String status) {
|
|
16
|
+
return status.equals("ACTIVE"); // NPE if status is null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public void handleRequest(HttpServletRequest request) {
|
|
20
|
+
String method = request.getMethod();
|
|
21
|
+
if (method.equals("POST")) { // NPE if method is null (unlikely but possible)
|
|
22
|
+
processPost();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public boolean checkRole(User user) {
|
|
27
|
+
String role = user.getRole(); // getRole() might return null
|
|
28
|
+
return role.equals("ADMIN"); // NPE
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**Correct (literal first — null-safe):**
|
|
33
|
+
|
|
34
|
+
```java
|
|
35
|
+
public boolean isActive(String status) {
|
|
36
|
+
return "ACTIVE".equals(status); // returns false if status is null — no NPE
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public void handleRequest(HttpServletRequest request) {
|
|
40
|
+
String method = request.getMethod();
|
|
41
|
+
if ("POST".equals(method)) { // safe even if method is null
|
|
42
|
+
processPost();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
public boolean checkRole(User user) {
|
|
47
|
+
String role = user.getRole();
|
|
48
|
+
return "ADMIN".equals(role); // null-safe
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Also applies to equalsIgnoreCase:
|
|
52
|
+
public boolean isAdminIgnoreCase(String role) {
|
|
53
|
+
return "admin".equalsIgnoreCase(role); // null-safe
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Constant comparison:
|
|
57
|
+
public static final String DEFAULT_LANG = "en";
|
|
58
|
+
|
|
59
|
+
public boolean isDefaultLanguage(String lang) {
|
|
60
|
+
return DEFAULT_LANG.equals(lang); // constant on left — same principle
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
**When to use Objects.equals() instead:**
|
|
65
|
+
|
|
66
|
+
Use `Objects.equals(a, b)` when **both** values may be `null` and you want a symmetric comparison:
|
|
67
|
+
|
|
68
|
+
```java
|
|
69
|
+
// Objects.equals handles both nulls:
|
|
70
|
+
boolean same = Objects.equals(user.getRole(), adminRole); // null-safe on both sides
|
|
71
|
+
|
|
72
|
+
// Equivalent to:
|
|
73
|
+
// user.getRole() == adminRole || (user.getRole() != null && user.getRole().equals(adminRole))
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**compareTo / compareToIgnoreCase:**
|
|
77
|
+
|
|
78
|
+
Note that reversing the literal changes the sign of the result:
|
|
79
|
+
|
|
80
|
+
```java
|
|
81
|
+
// Original: x.compareTo("bar") > 0
|
|
82
|
+
// Reversed: "bar".compareTo(x) < 0 ← sign flipped!
|
|
83
|
+
|
|
84
|
+
// Be careful when converting compareTo usage
|
|
85
|
+
if ("bar".compareTo(x) < 0) { ... } // equivalent to x.compareTo("bar") > 0
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
**Tools:** PMD (`LiteralsFirstInComparisons`), SonarQube (`S1132`), IntelliJ Inspections
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use EnumSet and EnumMap for Enum Keys
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: EnumSet and EnumMap use array-backed implementations that are significantly faster and more memory-efficient than HashSet/HashMap for enum keys
|
|
5
|
+
tags: performance, best-practice, java, collections
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use EnumSet and EnumMap for Enum Keys
|
|
9
|
+
|
|
10
|
+
When working with collections whose elements or keys are enum values, `EnumSet` and `EnumMap` should be preferred over `HashSet` and `HashMap`. They use a compact array-backed representation internally — bit vectors for `EnumSet` and an array indexed by ordinal for `EnumMap` — providing better performance and less memory usage.
|
|
11
|
+
|
|
12
|
+
**Incorrect (using general-purpose collections with enum keys):**
|
|
13
|
+
|
|
14
|
+
```java
|
|
15
|
+
public enum Permission { READ, WRITE, DELETE, ADMIN }
|
|
16
|
+
|
|
17
|
+
public class User {
|
|
18
|
+
// Using HashSet for enum values — wastes memory, slower
|
|
19
|
+
private Set<Permission> permissions = new HashSet<>();
|
|
20
|
+
|
|
21
|
+
public boolean hasPermission(Permission p) {
|
|
22
|
+
return permissions.contains(p); // HashSet lookup — unnecessary overhead
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public class RolePolicy {
|
|
27
|
+
// Using HashMap for enum keys — suboptimal
|
|
28
|
+
private Map<Permission, String> descriptions = new HashMap<>();
|
|
29
|
+
|
|
30
|
+
public void setupDescriptions() {
|
|
31
|
+
descriptions.put(Permission.READ, "Can read resources");
|
|
32
|
+
descriptions.put(Permission.WRITE, "Can write resources");
|
|
33
|
+
descriptions.put(Permission.ADMIN, "Full access");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**Correct (using EnumSet / EnumMap):**
|
|
39
|
+
|
|
40
|
+
```java
|
|
41
|
+
import java.util.EnumSet;
|
|
42
|
+
import java.util.EnumMap;
|
|
43
|
+
|
|
44
|
+
public enum Permission { READ, WRITE, DELETE, ADMIN }
|
|
45
|
+
|
|
46
|
+
public class User {
|
|
47
|
+
// EnumSet: compact bit-vector implementation, O(1) operations
|
|
48
|
+
private Set<Permission> permissions = EnumSet.noneOf(Permission.class);
|
|
49
|
+
|
|
50
|
+
public void grantPermission(Permission p) {
|
|
51
|
+
permissions.add(p);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
public boolean hasPermission(Permission p) {
|
|
55
|
+
return permissions.contains(p);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public class RolePolicy {
|
|
60
|
+
// EnumMap: array-backed, indexed by enum ordinal
|
|
61
|
+
private Map<Permission, String> descriptions = new EnumMap<>(Permission.class);
|
|
62
|
+
|
|
63
|
+
public void setupDescriptions() {
|
|
64
|
+
descriptions.put(Permission.READ, "Can read resources");
|
|
65
|
+
descriptions.put(Permission.WRITE, "Can write resources");
|
|
66
|
+
descriptions.put(Permission.ADMIN, "Full access");
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**EnumSet factory methods:**
|
|
72
|
+
|
|
73
|
+
```java
|
|
74
|
+
// Empty set
|
|
75
|
+
Set<Permission> none = EnumSet.noneOf(Permission.class);
|
|
76
|
+
|
|
77
|
+
// All values
|
|
78
|
+
Set<Permission> all = EnumSet.allOf(Permission.class);
|
|
79
|
+
|
|
80
|
+
// Specific values
|
|
81
|
+
Set<Permission> readOnly = EnumSet.of(Permission.READ);
|
|
82
|
+
|
|
83
|
+
// Range (by ordinal order)
|
|
84
|
+
Set<Permission> basic = EnumSet.range(Permission.READ, Permission.WRITE);
|
|
85
|
+
|
|
86
|
+
// Complement
|
|
87
|
+
Set<Permission> nonAdmin = EnumSet.complementOf(EnumSet.of(Permission.ADMIN));
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
**Performance comparison:**
|
|
91
|
+
|
|
92
|
+
| Operation | HashSet | EnumSet |
|
|
93
|
+
|-----------|---------|---------|
|
|
94
|
+
| `add` | O(1) avg | O(1) bit op |
|
|
95
|
+
| `contains` | O(1) avg | O(1) bit op |
|
|
96
|
+
| Memory | ~40 bytes/entry | 1 bit/entry |
|
|
97
|
+
| Iteration | Hash order | Enum ordinal order |
|
|
98
|
+
|
|
99
|
+
**When to apply:**
|
|
100
|
+
- Use `EnumSet` whenever you have a `Set<SomeEnum>`
|
|
101
|
+
- Use `EnumMap` whenever you have a `Map<SomeEnum, V>`
|
|
102
|
+
- Both are drop-in replacements that implement `Set` and `Map` respectively
|
|
103
|
+
|
|
104
|
+
**Tools:** PMD (`UseEnumCollections`), IntelliJ Inspections
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Return Empty Collection Instead of null
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: returning null for collections forces every caller to perform a null check, causing NullPointerExceptions when they forget
|
|
5
|
+
tags: null-safety, design, java, clean-code
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Return Empty Collection Instead of null
|
|
9
|
+
|
|
10
|
+
Methods that return collections (`List`, `Set`, `Map`, arrays) should **never** return `null` to indicate "no results." Instead, return an empty collection. This eliminates the need for null checks on every call site, reduces potential `NullPointerException` bugs, and makes the API safer and more predictable.
|
|
11
|
+
|
|
12
|
+
This principle is described in *Effective Java* by Joshua Bloch: *"Never return null in place of an empty array or collection."*
|
|
13
|
+
|
|
14
|
+
**Incorrect (returning null):**
|
|
15
|
+
|
|
16
|
+
```java
|
|
17
|
+
public List<Order> getOrdersByUser(Long userId) {
|
|
18
|
+
List<Order> orders = orderRepository.findByUserId(userId);
|
|
19
|
+
if (orders.isEmpty()) {
|
|
20
|
+
return null; // forces callers to null-check
|
|
21
|
+
}
|
|
22
|
+
return orders;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public String[] getTagsForPost(Long postId) {
|
|
26
|
+
String[] tags = tagRepository.findByPost(postId);
|
|
27
|
+
if (tags == null || tags.length == 0) {
|
|
28
|
+
return null; // callers must check for null before iterating
|
|
29
|
+
}
|
|
30
|
+
return tags;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Callers must remember to null-check — easy to forget:
|
|
34
|
+
List<Order> orders = orderService.getOrdersByUser(userId);
|
|
35
|
+
for (Order order : orders) { // NPE if orders is null
|
|
36
|
+
processOrder(order);
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Correct (returning empty collections):**
|
|
41
|
+
|
|
42
|
+
```java
|
|
43
|
+
import java.util.Collections;
|
|
44
|
+
import java.util.List;
|
|
45
|
+
|
|
46
|
+
public List<Order> getOrdersByUser(Long userId) {
|
|
47
|
+
List<Order> orders = orderRepository.findByUserId(userId);
|
|
48
|
+
if (orders == null || orders.isEmpty()) {
|
|
49
|
+
return Collections.emptyList(); // immutable, no allocation overhead
|
|
50
|
+
}
|
|
51
|
+
return orders;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Or with Stream API:
|
|
55
|
+
public List<Order> getOrdersByUser(Long userId) {
|
|
56
|
+
return orderRepository.findByUserId(userId) != null
|
|
57
|
+
? orderRepository.findByUserId(userId)
|
|
58
|
+
: Collections.emptyList();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Using List.of() (Java 9+):
|
|
62
|
+
public List<String> getTagsForPost(Long postId) {
|
|
63
|
+
List<String> tags = tagRepository.findByPost(postId);
|
|
64
|
+
return tags != null ? tags : List.of(); // immutable empty list
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Arrays:
|
|
68
|
+
public String[] getPermissionsForRole(String role) {
|
|
69
|
+
String[] perms = permissionRepo.findByRole(role);
|
|
70
|
+
return perms != null ? perms : new String[0]; // empty array, not null
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Callers now iterate safely without null checks:
|
|
74
|
+
List<Order> orders = orderService.getOrdersByUser(userId);
|
|
75
|
+
for (Order order : orders) { // safe — at worst iterates 0 times
|
|
76
|
+
processOrder(order);
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Preferred empty collection factories:**
|
|
81
|
+
|
|
82
|
+
| Type | Factory |
|
|
83
|
+
|------|---------|
|
|
84
|
+
| `List<T>` | `Collections.emptyList()` or `List.of()` (Java 9+) |
|
|
85
|
+
| `Set<T>` | `Collections.emptySet()` or `Set.of()` |
|
|
86
|
+
| `Map<K,V>` | `Collections.emptyMap()` or `Map.of()` |
|
|
87
|
+
| `T[]` | `new T[0]` |
|
|
88
|
+
|
|
89
|
+
**Note:** `Collections.emptyList()`, `emptySet()`, and `emptyMap()` return immutable, singleton instances — no memory allocation per call. They are the most efficient choice.
|
|
90
|
+
|
|
91
|
+
**Exception — when null has semantic meaning:**
|
|
92
|
+
If `null` and "empty" are **intentionally different states** (e.g., "not loaded yet" vs "loaded but empty"), `Optional<List<T>>` or a domain wrapper class should be used instead of `null`.
|
|
93
|
+
|
|
94
|
+
```java
|
|
95
|
+
// Distinguish "not configured" from "configured but empty"
|
|
96
|
+
public Optional<List<String>> getWhitelist(String service) {
|
|
97
|
+
if (!config.hasWhitelist(service)) return Optional.empty();
|
|
98
|
+
return Optional.of(config.getWhitelist(service));
|
|
99
|
+
}
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
**Tools:** PMD (`ReturnEmptyCollectionRatherThanNull`), SonarQube (`S1168`), IntelliJ Inspections
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Never Hardcode Cryptographic Keys or Initialization Vectors
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: hardcoded crypto keys or IVs give attackers permanent access to all encrypted data — compromised keys cannot be rotated without code changes
|
|
5
|
+
tags: security, cryptography, java, secrets
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Never Hardcode Cryptographic Keys or Initialization Vectors
|
|
9
|
+
|
|
10
|
+
Hardcoding cryptographic keys, passwords, secrets, or initialization vectors (IVs) directly in source code is a critical security vulnerability (OWASP A02: Cryptographic Failures). Once the code is in source control or compiled into a binary, the key is permanently exposed. Attackers who access the binary, source code, or logs can decrypt all data protected by that key.
|
|
11
|
+
|
|
12
|
+
**Incorrect (hardcoded key/IV):**
|
|
13
|
+
|
|
14
|
+
```java
|
|
15
|
+
import javax.crypto.SecretKey;
|
|
16
|
+
import javax.crypto.spec.SecretKeySpec;
|
|
17
|
+
import javax.crypto.spec.IvParameterSpec;
|
|
18
|
+
|
|
19
|
+
public class EncryptionService {
|
|
20
|
+
// CRITICAL VULNERABILITY: key is hardcoded in source code
|
|
21
|
+
private static final byte[] SECRET_KEY = "MySuperSecretKey".getBytes(); // 16 bytes = AES-128
|
|
22
|
+
private static final byte[] IV = "InitVector123456".getBytes(); // hardcoded IV
|
|
23
|
+
|
|
24
|
+
public byte[] encrypt(byte[] data) throws Exception {
|
|
25
|
+
SecretKey key = new SecretKeySpec(SECRET_KEY, "AES");
|
|
26
|
+
IvParameterSpec ivSpec = new IvParameterSpec(IV);
|
|
27
|
+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
28
|
+
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
|
|
29
|
+
return cipher.doFinal(data);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Also bad: hardcoded passwords for key derivation
|
|
34
|
+
public class KeyDerivation {
|
|
35
|
+
private static final String PASSWORD = "hardcoded_password_123"; // exposed in binary
|
|
36
|
+
private static final byte[] SALT = "fixed_salt".getBytes(); // static salt defeats PBKDF2
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Correct (keys from secure storage):**
|
|
41
|
+
|
|
42
|
+
```java
|
|
43
|
+
import javax.crypto.*;
|
|
44
|
+
import javax.crypto.spec.*;
|
|
45
|
+
import java.security.SecureRandom;
|
|
46
|
+
|
|
47
|
+
public class EncryptionService {
|
|
48
|
+
private final SecretKey key;
|
|
49
|
+
|
|
50
|
+
// Inject key via constructor — loaded from secure storage, not hardcoded
|
|
51
|
+
public EncryptionService(SecretKey key) {
|
|
52
|
+
this.key = key;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public EncryptedData encrypt(byte[] data) throws Exception {
|
|
56
|
+
// Always generate a random IV per encryption operation
|
|
57
|
+
byte[] iv = new byte[16];
|
|
58
|
+
new SecureRandom().nextBytes(iv); // cryptographically random
|
|
59
|
+
IvParameterSpec ivSpec = new IvParameterSpec(iv);
|
|
60
|
+
|
|
61
|
+
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
|
62
|
+
cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec);
|
|
63
|
+
byte[] ciphertext = cipher.doFinal(data);
|
|
64
|
+
|
|
65
|
+
return new EncryptedData(iv, ciphertext); // store IV alongside ciphertext
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Key loading from environment or secrets manager
|
|
70
|
+
public class KeyLoader {
|
|
71
|
+
public SecretKey loadKeyFromEnv() {
|
|
72
|
+
String base64Key = System.getenv("AES_SECRET_KEY"); // from environment
|
|
73
|
+
if (base64Key == null) {
|
|
74
|
+
throw new IllegalStateException("AES_SECRET_KEY environment variable not set");
|
|
75
|
+
}
|
|
76
|
+
byte[] keyBytes = Base64.getDecoder().decode(base64Key);
|
|
77
|
+
return new SecretKeySpec(keyBytes, "AES");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Or use Java KeyStore (JKS/PKCS12):
|
|
81
|
+
public SecretKey loadKeyFromKeystore(String keystorePath, char[] storePassword,
|
|
82
|
+
String alias, char[] keyPassword) throws Exception {
|
|
83
|
+
KeyStore ks = KeyStore.getInstance("PKCS12");
|
|
84
|
+
try (InputStream fis = new FileInputStream(keystorePath)) {
|
|
85
|
+
ks.load(fis, storePassword);
|
|
86
|
+
}
|
|
87
|
+
return (SecretKey) ks.getKey(alias, keyPassword);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Secure key management options:**
|
|
93
|
+
|
|
94
|
+
| Method | Description |
|
|
95
|
+
|--------|-------------|
|
|
96
|
+
| Environment variables | Simple, good for containers (`AES_SECRET_KEY`) |
|
|
97
|
+
| Java KeyStore (JKS/PKCS12) | Standard Java key storage |
|
|
98
|
+
| AWS Secrets Manager / KMS | Cloud-managed, auditable |
|
|
99
|
+
| HashiCorp Vault | Enterprise secret management |
|
|
100
|
+
| Google Cloud Secret Manager | GCP-native secrets |
|
|
101
|
+
|
|
102
|
+
**Additional rules:**
|
|
103
|
+
- Always use a **random IV** per encryption — never a fixed IV.
|
|
104
|
+
- IVs need not be secret but must be unique per encryption.
|
|
105
|
+
- Use AES-GCM instead of AES-CBC where possible (provides authentication).
|
|
106
|
+
- Key length: AES-128 minimum, AES-256 recommended.
|
|
107
|
+
|
|
108
|
+
**Tools:** PMD (`HardCodedCryptoKey`, `InsecureCryptoIv`), SonarQube (`S2068`, `S3329`), SpotBugs (`HardCodedKey`)
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Optional Instead of Returning null for Absent Values
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: returning null from methods forces callers to guess whether null is possible, leading to unchecked NullPointerExceptions
|
|
5
|
+
tags: null-safety, design, java, clean-code
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Optional Instead of Returning null for Absent Values
|
|
9
|
+
|
|
10
|
+
`null` is the most common source of `NullPointerException` in Java. When a method may or may not return a value (as opposed to returning a collection), use `java.util.Optional<T>` to make the "absence" explicit in the API contract. `Optional` forces callers to consciously handle the absent case.
|
|
11
|
+
|
|
12
|
+
This is described in *Effective Java* Item 55: *"Return Optionals Judiciously."*
|
|
13
|
+
|
|
14
|
+
**Incorrect (returning null):**
|
|
15
|
+
|
|
16
|
+
```java
|
|
17
|
+
// Caller cannot tell from the signature whether null is possible
|
|
18
|
+
public User findUserByEmail(String email) {
|
|
19
|
+
return userRepository.findByEmail(email); // may return null — not obvious
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public String getPreferredLanguage(Long userId) {
|
|
23
|
+
UserPreferences prefs = preferencesRepo.findByUser(userId);
|
|
24
|
+
if (prefs == null) return null; // unclear API contract
|
|
25
|
+
return prefs.getLanguage();
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Callers must remember to null-check:
|
|
29
|
+
User user = userService.findUserByEmail("a@b.com");
|
|
30
|
+
user.getName(); // NPE if forgotten
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**Correct (using Optional):**
|
|
34
|
+
|
|
35
|
+
```java
|
|
36
|
+
import java.util.Optional;
|
|
37
|
+
|
|
38
|
+
// Method signature clearly states: "this might not exist"
|
|
39
|
+
public Optional<User> findUserByEmail(String email) {
|
|
40
|
+
User user = userRepository.findByEmail(email);
|
|
41
|
+
return Optional.ofNullable(user); // wraps null into empty Optional
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public Optional<String> getPreferredLanguage(Long userId) {
|
|
45
|
+
return Optional.ofNullable(preferencesRepo.findByUser(userId))
|
|
46
|
+
.map(UserPreferences::getLanguage);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**Handling Optional at call sites:**
|
|
51
|
+
|
|
52
|
+
```java
|
|
53
|
+
// Option 1: provide a default value
|
|
54
|
+
String name = userService.findUserByEmail("a@b.com")
|
|
55
|
+
.map(User::getName)
|
|
56
|
+
.orElse("Guest");
|
|
57
|
+
|
|
58
|
+
// Option 2: throw a meaningful exception if absent
|
|
59
|
+
User user = userService.findUserByEmail("a@b.com")
|
|
60
|
+
.orElseThrow(() -> new UserNotFoundException("User not found: " + email));
|
|
61
|
+
|
|
62
|
+
// Option 3: only proceed if present
|
|
63
|
+
userService.findUserByEmail("a@b.com")
|
|
64
|
+
.ifPresent(user -> sendWelcomeEmail(user));
|
|
65
|
+
|
|
66
|
+
// Option 4: ifPresentOrElse (Java 9+)
|
|
67
|
+
userService.findUserByEmail("a@b.com")
|
|
68
|
+
.ifPresentOrElse(
|
|
69
|
+
user -> logger.info("Found user: {}", user.getId()),
|
|
70
|
+
() -> logger.info("User not found")
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
// Option 5: transform and fall back
|
|
74
|
+
String language = getPreferredLanguage(userId)
|
|
75
|
+
.filter(lang -> supportedLanguages.contains(lang))
|
|
76
|
+
.orElse("en");
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
**When NOT to use Optional:**
|
|
80
|
+
|
|
81
|
+
```java
|
|
82
|
+
// 1. Do NOT use Optional for fields — use null or default values
|
|
83
|
+
public class User {
|
|
84
|
+
private Optional<String> middleName; // bad: not serializable, adds overhead
|
|
85
|
+
private String middleName; // good: use null or ""
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 2. Do NOT use Optional for method parameters — use overloading or @Nullable
|
|
89
|
+
public void createUser(Optional<String> role) { ... } // bad
|
|
90
|
+
public void createUser(String role) { ... } // good: role can be nullable
|
|
91
|
+
public void createUser() { ... } // good: overload for absent role
|
|
92
|
+
|
|
93
|
+
// 3. Do NOT use Optional for collections — return empty collection instead
|
|
94
|
+
public Optional<List<Order>> getOrders() { ... } // bad: double-wrap
|
|
95
|
+
public List<Order> getOrders() { ... } // good: return empty list
|
|
96
|
+
|
|
97
|
+
// 4. Do NOT use Optional.get() without checking — defeats the purpose
|
|
98
|
+
user.get().getName(); // same as null, throws NoSuchElementException
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Optional factory methods:**
|
|
102
|
+
|
|
103
|
+
```java
|
|
104
|
+
Optional.of(value) // value must be non-null; throws NPE if null
|
|
105
|
+
Optional.ofNullable(value) // safe: wraps null into empty Optional
|
|
106
|
+
Optional.empty() // explicitly empty
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**Tools:** IntelliJ Inspections (`OptionalUsedAsFieldOrParameterType`), SonarQube (`S3553`, `S2789`), PMD
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# Laravel Framework — SunLint Agent Guide
|
|
2
|
+
|
|
3
|
+
> Priority directives for AI agents working on Laravel projects.
|
|
4
|
+
> Rule files: `.agent/skills/sunlint-code-quality/rules/`
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Critical Patterns — Apply Every Time
|
|
9
|
+
|
|
10
|
+
### Validation
|
|
11
|
+
- **Always** use `php artisan make:request` → `FormRequest` class, never `$request->validate()` in controller
|
|
12
|
+
- In the controller use `$request->validated()` — never `$request->all()` or `$request->input()` wholesale
|
|
13
|
+
- See: `LV001-form-request-validation.md`
|
|
14
|
+
|
|
15
|
+
### N+1 Queries
|
|
16
|
+
- **Always** check: does this controller/service access a relation in a loop or collection?
|
|
17
|
+
- If yes → add `->with(['relation'])` to the query **before** the loop
|
|
18
|
+
- Use `->withCount('relation')` instead of `->relation->count()` in loops
|
|
19
|
+
- See: `LV002-eager-load-no-n-plus-1.md`
|
|
20
|
+
|
|
21
|
+
### Mass Assignment
|
|
22
|
+
- Every new Model must define explicit `$fillable = [...]`
|
|
23
|
+
- **Never**: `protected $guarded = []`
|
|
24
|
+
- **Never**: `Model::create($request->all())` — use `$request->validated()`
|
|
25
|
+
- See: `LV004-fillable-mass-assignment.md`
|
|
26
|
+
|
|
27
|
+
### Passwords
|
|
28
|
+
- `Hash::make($password)` to store, `Hash::check($plain, $hash)` to verify
|
|
29
|
+
- **Never** `md5()`, `sha1()`, or raw `password_hash()` for passwords
|
|
30
|
+
- See: `LV007-hash-passwords.md`
|
|
31
|
+
|
|
32
|
+
### Authorization
|
|
33
|
+
- **Never** `if ($user->role === 'admin')` scattered in controllers
|
|
34
|
+
- Generate a Policy: `php artisan make:policy ModelPolicy --model=Model`
|
|
35
|
+
- Use `$this->authorize('action', $model)` or Form Request `authorize()`
|
|
36
|
+
- See: `LV005-policies-gates-authorization.md`
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## Architecture Patterns
|
|
41
|
+
|
|
42
|
+
### Controller responsibility
|
|
43
|
+
Controllers do exactly 3 things: validate input → call service → return response.
|
|
44
|
+
No business logic, no DB queries, no calculations in controllers.
|
|
45
|
+
```php
|
|
46
|
+
// Controller
|
|
47
|
+
public function store(StoreOrderRequest $request): JsonResponse
|
|
48
|
+
{
|
|
49
|
+
$order = $this->orderService->placeOrder($request->user(), $request->validated());
|
|
50
|
+
return OrderResource::make($order)->response()->setStatusCode(201);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
See: `LV012-service-layer.md`
|
|
54
|
+
|
|
55
|
+
### API Responses
|
|
56
|
+
- Always use `php artisan make:resource` → never `->toArray()`, `->toJson()` or raw `response()->json($model)`
|
|
57
|
+
- Sensitive columns (`password`, `remember_token`) must be in `$hidden` on the model
|
|
58
|
+
- See: `LV009-api-resources.md`
|
|
59
|
+
|
|
60
|
+
### Configuration
|
|
61
|
+
- `env()` appears **only** in `config/*.php` files — never in app code (breaks after `config:cache`)
|
|
62
|
+
- App code always uses `config('key.sub_key')` with optional default
|
|
63
|
+
- See: `LV003-config-not-env.md`
|
|
64
|
+
|
|
65
|
+
### Heavy Operations (email, API calls, reports)
|
|
66
|
+
- Any external call or operation > ~200ms → `Job::dispatch()->onQueue('name')`
|
|
67
|
+
- Job class implements `ShouldQueue`, handles `failed()` method
|
|
68
|
+
- See: `LV006-queue-heavy-tasks.md`
|
|
69
|
+
|
|
70
|
+
### Large Data Sets
|
|
71
|
+
- No `Model::all()` or unbounded `->get()` in commands/jobs
|
|
72
|
+
- Use `->chunkById(500, fn($batch) => ...)` or `->cursor()` for iteration
|
|
73
|
+
- See: `LV010-chunk-large-datasets.md`
|
|
74
|
+
|
|
75
|
+
### Multi-step Writes
|
|
76
|
+
- Any sequence of 2+ DB writes that must be atomic → wrap in `DB::transaction(fn() => { ... })`
|
|
77
|
+
- dispatch jobs inside transactions: `Job::dispatch($model)->afterCommit()`
|
|
78
|
+
- See: `LV011-db-transactions.md`
|
|
79
|
+
|
|
80
|
+
### Route Model Binding
|
|
81
|
+
- Route params matching a Model name auto-resolve — no manual `findOrFail()`
|
|
82
|
+
- `Route::get('/posts/{post}', ...)` → controller receives `Post $post` directly
|
|
83
|
+
- See: `LV008-route-model-binding.md`
|
|
84
|
+
|
|
85
|
+
### Dependency Injection
|
|
86
|
+
- Register services in `AppServiceProvider::register()` as singletons/bindings
|
|
87
|
+
- Controllers receive via constructor — never `new Service()` inside a method
|
|
88
|
+
- See: `LV014-service-container.md`
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## Run Commands
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
php artisan make:request StoreXxxRequest # validation
|
|
96
|
+
php artisan make:policy XxxPolicy --model=Xxx
|
|
97
|
+
php artisan make:resource XxxResource
|
|
98
|
+
php artisan make:job ProcessXxx
|
|
99
|
+
php artisan make:service XxxService # (if using stubs)
|
|
100
|
+
|
|
101
|
+
php artisan test --filter ClassName # run specific test
|
|
102
|
+
php artisan test --coverage # coverage report
|
|
103
|
+
php artisan config:cache # validate no env() in app code
|
|
104
|
+
php artisan route:list --path=api # audit routes
|
|
105
|
+
php artisan telescope:install # install query/request debugger
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## What NOT to do (quick reference)
|
|
111
|
+
|
|
112
|
+
| ❌ Wrong | ✅ Correct |
|
|
113
|
+
|---|---|
|
|
114
|
+
| `$request->validate([...])` in controller | `FormRequest` class |
|
|
115
|
+
| `$request->all()` | `$request->validated()` |
|
|
116
|
+
| `Post::all()` | `Post::with([...])->paginate(20)` |
|
|
117
|
+
| `$post->comments->count()` in loop | `Post::withCount('comments')` |
|
|
118
|
+
| `env('KEY')` in service | `config('prefix.key')` |
|
|
119
|
+
| `$guarded = []` | `$fillable = ['col1', 'col2']` |
|
|
120
|
+
| `if ($user->role === 'admin')` | `$this->authorize('action', $model)` |
|
|
121
|
+
| `md5($password)` | `Hash::make($password)` |
|
|
122
|
+
| `response()->json($model)` | `ModelResource::make($model)` |
|
|
123
|
+
| `Mail::send()` in controller | `SendMailJob::dispatch()->onQueue(...)` |
|
|
124
|
+
| `new OrderService()` in controller | Constructor DI auto-resolved by container |
|