Skip to content

Commit 8d7695f

Browse files
committed
Improve PMD analysed code
1 parent 3d19418 commit 8d7695f

File tree

4 files changed

+31
-45
lines changed

4 files changed

+31
-45
lines changed

testcontainers-advanced-imagebuilder/src/main/java/software/xdev/testcontainers/imagebuilder/jgit/ignore/internal/NameMatcher.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ protected NameMatcher(
5151
}
5252
}
5353

54+
@SuppressWarnings({"java:S3776", "PMD.CognitiveComplexity"})
5455
@Override
5556
public boolean matches(
56-
final String path, final boolean assumeDirectory,
57+
final String path,
58+
final boolean assumeDirectory,
5759
final boolean pathMatch)
5860
{
5961
// A NameMatcher's pattern does not contain a slash.

testcontainers-advanced-imagebuilder/src/main/java/software/xdev/testcontainers/imagebuilder/jgit/ignore/internal/PathMatcher.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ public boolean matches(final String segment, final int startIncl, final int endE
232232
"Path matcher works only on entire paths");
233233
}
234234

235+
@SuppressWarnings({"java:S3776", "PMD.CognitiveComplexity"})
235236
protected boolean iterate(
236237
final String path,
237238
final int startIncl,

testcontainers-advanced-imagebuilder/src/main/java/software/xdev/testcontainers/imagebuilder/jgit/ignore/internal/Strings.java

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public static List<String> split(final String pattern, final char slash)
142142
throw new IllegalStateException(
143143
"Pattern must have at least two segments: " + pattern);
144144
}
145+
145146
final List<String> segments = new ArrayList<>(count);
146147
int right = 0;
147148
while(true)
@@ -281,32 +282,32 @@ enum PatternState
281282
// [:word:]
282283
);
283284

284-
// Collating symbols [[.a.]] or equivalence class expressions [[=a=]] are
285-
// not supported by CLI git (at least not by 1.9.1)
286-
static final Pattern UNSUPPORTED = Pattern
287-
.compile("\\[\\[[.=]\\w+[.=]\\]\\]");
288-
289285
/**
290286
* Conversion from glob to Java regex following two sources:
291287
* <ul>
292-
* <li>http://man7.org/linux/man-pages/man7/glob.7.html
288+
* <li><a href="http://man7.org/linux/man-pages/man7/glob.7.html">Git Glob Man Page</a></li>
293289
* <li>org.eclipse.jgit.fnmatch.FileNameMatcher.java Seems that there are
294-
* various ways to define what "glob" can be.
290+
* various ways to define what "glob" can be.</li>
295291
* </ul>
296292
*
297293
* @param pattern non null pattern
298294
* @return Java regex pattern corresponding to given glob pattern
299295
* @throws InvalidPatternException if pattern is invalid
300296
*/
301-
@SuppressWarnings({"checkstyle:MethodLength", "checkstyle:MagicNumber"})
302-
public static Pattern convertGlob(final String pattern) throws InvalidPatternException
297+
@SuppressWarnings({
298+
"checkstyle:MethodLength",
299+
"checkstyle:MagicNumber",
300+
"java:S3776",
301+
"PMD.CognitiveComplexity",
302+
"PMD.CyclomaticComplexity",
303+
"java:S6541"}) // Eclipse code = Big brain required
304+
public static Pattern convertGlob(final String pattern)
305+
throws InvalidPatternException
303306
{
304-
if(UNSUPPORTED.matcher(pattern).find())
305-
{
306-
throw new InvalidPatternException(
307-
"Collating symbols [[.a.]] or equivalence class expressions [[=a=]] are not supported",
308-
pattern);
309-
}
307+
// We don't check for unsupported patterns to improve performance
308+
// Original comment:
309+
// Collating symbols [[.a.]] or equivalence class expressions [[=a=]] are
310+
// not supported by CLI git (at least not by 1.9.1)
310311

311312
final StringBuilder sb = new StringBuilder(pattern.length());
312313

@@ -322,7 +323,6 @@ public static Pattern convertGlob(final String pattern) throws InvalidPatternExc
322323
final char c = pattern.charAt(i);
323324
switch(c)
324325
{
325-
326326
case '*':
327327
if(seenEscape || inBrackets > 0)
328328
{
@@ -333,15 +333,7 @@ public static Pattern convertGlob(final String pattern) throws InvalidPatternExc
333333
sb.append('.').append(c);
334334
}
335335
break;
336-
337-
case '(': // fall-through
338-
case ')': // fall-through
339-
case '{': // fall-through
340-
case '}': // fall-through
341-
case '+': // fall-through
342-
case '$': // fall-through
343-
case '^': // fall-through
344-
case '|':
336+
case '(', ')', '{', '}', '+', '$', '^', '|':
345337
if(seenEscape || inBrackets > 0)
346338
{
347339
sb.append(c);
@@ -351,18 +343,16 @@ public static Pattern convertGlob(final String pattern) throws InvalidPatternExc
351343
sb.append('\\').append(c);
352344
}
353345
break;
354-
355346
case '.':
356347
if(seenEscape)
357348
{
358349
sb.append(c);
359350
}
360351
else
361352
{
362-
sb.append('\\').append('.');
353+
sb.append("\\.");
363354
}
364355
break;
365-
366356
case '?':
367357
if(seenEscape || inBrackets > 0)
368358
{
@@ -373,18 +363,15 @@ public static Pattern convertGlob(final String pattern) throws InvalidPatternExc
373363
sb.append('.');
374364
}
375365
break;
376-
377366
case ':':
378367
if(inBrackets > 0
379368
&& lookBehind(sb) == '['
380369
&& isLetter(lookAhead(pattern, i)))
381370
{
382371
inCharClass = true;
383372
}
384-
385373
sb.append(':');
386374
break;
387-
388375
case '-':
389376
if(inBrackets > 0)
390377
{
@@ -402,20 +389,17 @@ && isLetter(lookAhead(pattern, i)))
402389
sb.append('-');
403390
}
404391
break;
405-
406392
case '\\':
393+
final char lookAhead = lookAhead(pattern, i);
407394
if(inBrackets > 0)
408395
{
409-
final char lookAhead = lookAhead(pattern, i);
410396
if(lookAhead == ']' || lookAhead == '[')
411397
{
412398
ignoreLastBracket = true;
413399
}
414400
}
415401
else
416402
{
417-
//
418-
final char lookAhead = lookAhead(pattern, i);
419403
if(lookAhead != '\\' && lookAhead != '['
420404
&& lookAhead != '?' && lookAhead != '*'
421405
&& lookAhead != ' ' && lookBehind(sb) != '\\')
@@ -425,7 +409,6 @@ && isLetter(lookAhead(pattern, i)))
425409
}
426410
sb.append(c);
427411
break;
428-
429412
case '[':
430413
if(inBrackets > 0)
431414
{
@@ -446,7 +429,6 @@ && isLetter(lookAhead(pattern, i)))
446429
sb.append('[');
447430
}
448431
break;
449-
450432
case ']':
451433
if(seenEscape)
452434
{
@@ -456,16 +438,15 @@ && isLetter(lookAhead(pattern, i)))
456438
}
457439
if(inBrackets <= 0)
458440
{
459-
sb.append('\\').append(']');
441+
sb.append("\\]");
460442
ignoreLastBracket = true;
461443
break;
462444
}
463445
final char lookBehind = lookBehind(sb);
464446
if(lookBehind == '[' && !ignoreLastBracket
465447
|| lookBehind == '^')
466448
{
467-
sb.append('\\');
468-
sb.append(']');
449+
sb.append("\\]");
469450
ignoreLastBracket = true;
470451
}
471452
else
@@ -490,7 +471,6 @@ && isLetter(lookAhead(pattern, i)))
490471
}
491472
}
492473
break;
493-
494474
case '!':
495475
if(inBrackets > 0)
496476
{
@@ -508,7 +488,6 @@ && isLetter(lookAhead(pattern, i)))
508488
sb.append(c);
509489
}
510490
break;
511-
512491
default:
513492
if(inCharClass)
514493
{
@@ -519,10 +498,10 @@ && isLetter(lookAhead(pattern, i)))
519498
sb.append(c);
520499
}
521500
break;
522-
} // end switch
501+
}
523502

524503
seenEscape = c == '\\';
525-
} // end for
504+
}
526505

527506
if(inBrackets > 0)
528507
{

testcontainers-advanced-imagebuilder/src/main/java/software/xdev/testcontainers/imagebuilder/transfer/DefaultTransferFilesCreator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ protected Map<Path, String> walkFilesAndDetermineTransfer(
154154
}
155155
}
156156

157+
@SuppressWarnings("java:S2789")
157158
protected Map.Entry<Path, String> determineFileForTransfer(
158159
final IgnoreNode ignoreNode,
159160
final Set<String> alwaysIncludedRelativePaths,
@@ -189,6 +190,7 @@ protected Map.Entry<Path, String> determineFileForTransfer(
189190
return outcomeDirs;
190191
}
191192

193+
@SuppressWarnings("java:S2789")
192194
protected Map.Entry<Path, String> determineParentDirectoryForTransfer(
193195
final IgnoreNode ignoreNode,
194196
final Set<String> alwaysIncludedRelativePaths,
@@ -236,6 +238,7 @@ protected String parentDirectory(final String dir)
236238
: null;
237239
}
238240

241+
@SuppressWarnings("java:S2789")
239242
protected Optional<Map.Entry<Path, String>> shouldIgnore(
240243
final IgnoreNode ignoreNode,
241244
final Set<String> alwaysIncludedRelativePaths,
@@ -258,6 +261,7 @@ else if(result == IgnoreNode.MatchResult.IGNORED)
258261
{
259262
return Optional.empty();
260263
}
264+
// NULL -> Continue
261265
return null;
262266
}
263267

0 commit comments

Comments
 (0)