[TASK] Improve duplicate exception code check
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Build / Scripts / duplicateExceptionCodeCheck.sh
1 #!/bin/bash
2
3 #########################
4 #
5 # Find duplicate exception timestamps and list them.
6 # Additionally find exceptions that have no exception code.
7 #
8 # It expects to be run from the core root.
9 #
10 ##########################
11
12 cd typo3/
13
14 ignoreFiles=()
15 # auto generated file, shouldn't be checked
16 ignoreFiles+="sysext/core/Build/Configuration/Acceptance/Support/_generated/AcceptanceTesterActions.php"
17 # a exception in here throws up an code from a previous exception
18 ignoreFiles+="sysext/extbase/Classes/Core/Bootstrap.php"
19
20 foundNewFile=0
21 oldFilename=""
22 firstLineOfMatch=""
23 foundExceptionInFile=1
24 exceptionCodes=()
25
26 # grep
27 # '-r' recursive
28 # '--include '*.php'' in all .php files
29 # '-Pzoab' pcre regex, -zo remove all linebreaks for multiline match, treat all files as text, output position "filename:position: match", binary position
30 #
31 # (?:(?!Exception\()[\w\\])* negative lookahead. capture all alphanum and \ until we reach "Exception("
32 # eat "Exception("
33 # (?:(?!\);).|[\r\n])*\);[\r\n]+ negative lookahead again, eat everything including a \n until we reach the first ");", then line breaks
34
35 grep \
36 -r \
37 --include '*.php' \
38 -Pzoab \
39 'new (?:(?!Exception\()[\w\\])*Exception\((?:(?!\);).|[\r\n])*\);[\r\n]+' \
40 | \
41 while read line;
42 do
43 possibleFilename=`echo ${line} | cut -d':' -f1`
44 if [[ ${possibleFilename} =~ .php$ ]]; then
45 # the matched line consists of a file name match, we're dealing with a new match here.
46 foundNewFile=1
47 oldFilename=${currentFilename}
48 currentFilename=${possibleFilename}
49 else
50 foundNewFile=0
51 fi
52
53 # skip file if in blacklist
54 if [[ {$ignoreFiles[@]} =~ ${currentFilename} ]]; then
55 continue
56 fi
57
58 # check for match in previous file name
59 if [[ ${foundNewFile} -eq 1 ]] && [[ ${foundExceptionInFile} -eq 0 ]]; then
60 echo "File: $oldFilename"
61 echo "The created exception contains no 10 digit exception code as second argument, in or below this line:"
62 echo "$firstLineOfMatch"
63 exit 1
64 fi
65
66 # reset found flag if we're handling new file
67 if [[ ${foundNewFile} -eq 1 ]]; then
68 foundExceptionInFile=0
69 firstLineOfMatch=${line}
70 fi
71
72 # see if the line consists of an exception code
73 if [[ "$line" =~ .*([0-9]{10}).* ]]; then
74 foundExceptionInFile=1
75 exceptionCode=${BASH_REMATCH[1]}
76 # check if that code was registered already
77 if [[ {$exceptionCodes[@]} =~ ${exceptionCode} ]]; then
78 echo "Duplicate exception code ${exceptionCode} in file:"
79 echo ${currentFilename}
80 exit 1
81 fi
82 exceptionCodes+=${exceptionCode}
83 fi
84 done || exit 1
85
86 exit 0