This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
AnalyzerPHP.pas
2148 lines (1940 loc) · 69 KB
/
AnalyzerPHP.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//****************************************************************
//** Auto completion for custom PHP classes (ACCPC) **
//** **
//** Written by Stanislav Eckert, 2013-2015 **
//** Base plugin template by Damjan Zobo Cvetko **
//****************************************************************
unit AnalyzerPHP;
interface
uses
System.SysUtils, System.Classes, System.Contnrs, System.Generics.Collections,
CRC, Vcl.Dialogs, Vcl.Forms, Windows, ParsingIndicatorForms, Vcl.ComCtrls,
System.WideStrUtils;
type
// Analyzer sets
as_visibility = (asvUnknown, asvPublic, asvPrivate, asvProtected);
as_direction = (asdNone, asdLeft, asdRight);
crcdb = class
public
crc: UINT32;
sFile: String;
end;
Ttextpos = class
public
iStart: UINT32;
iEnd: UINT32;
constructor Create(iStart: UINT32; iEnd: UINT32);
end;
AnalyzerPHPConstant = class
public
name: String;
value: String;
case_sensitive: Boolean;
end;
AnalyzerPHPAttribute = class
public
visibility: as_visibility;
bStatic: Boolean;
bConst: Boolean;
name: String;
fileOffset: UINT32;
end;
AnalyzerPHPMethod = class
public
visibility: as_visibility;
bStatic: Boolean;
name: String;
parameters: TStringList;
fileOffset: UINT32;
constructor Create();
destructor Destroy(); override;
end;
AnalyzerPHPClass = class
public
name: String;
derivedFromClass: String;
attributes: System.Generics.Collections.TObjectList<AnalyzerPHPAttribute>;
methods: System.Generics.Collections.TObjectList<AnalyzerPHPMethod>;
fileOffset: UINT32;
constructor Create();
destructor Destroy(); override;
function countPublicAttributesAndMethods(checkType: Integer): Integer; // checkType: 0 = both, 1 = attributes only, 2 = methods only
end;
ClassLinkingListEntry = class
public
classIndex: UINT32;
fileIndices: System.Generics.Collections.TList<Integer>; // Must be signed integer
constructor Create();
destructor Destroy(); override;
end;
TAnalyzerPHP = class
public
// Attributes
isAnalyzing: Boolean;
classes: System.Generics.Collections.TObjectList<AnalyzerPHPClass>;
constants: System.Generics.Collections.TObjectList<AnalyzerPHPConstant>;
include_files: TStringList;
included_files: TStringList;
RootDirectory: String;
LastClass: String;
iPos: Integer;
iOldDirection: as_direction;
bGetParamValues: Boolean;
bGetParamTypeCasting: Boolean;
text: ^String;
// CRC file database
dbfiles: System.Generics.Collections.TObjectList<crcdb>;
// List for storing which classes are in which files (used for the Class Inspector)
classLinkingList: System.Generics.Collections.TObjectList<ClassLinkingListEntry>;
// Methods
constructor Create();
destructor Destroy(); override;
procedure AnalyzeDirectory();
function AnalyzePHP(currentFile: String = ''): boolean;
procedure PrepareTextForParsing(bReplaceWithPlaceholders: Boolean = false); // Try to write ReplaceTextRange() in Assembler
procedure Inherit();
function getNext(bFindKeywordOnly: Boolean = true): String;
function getPrevious(bFindKeywordOnly: Boolean = true): String;
procedure Reset(bResetAll: Boolean = false);
function getLastClass(): AnalyzerPHPClass;
procedure savePosition();
procedure restorePosition();
procedure dump(outputFile: String; mode: Integer); // for debug testing
private
alterText: Boolean; // Allow to set? Should always be set to true
scannedFolders: Integer;
scanningStart: DWORD;
// Attributes for backup
_iPos: Integer;
_iOldDirection: as_direction;
function isKeyword(sKeyword: String): boolean;
function AllBracketsClosed(var str: String): boolean;
function LastChar(var str: String; c: WideChar): Integer;
procedure ListFiles(SearchDir: String; ScanOnly: Boolean);
procedure LoadFile(sFile: String);
function getClassByName(class_name: String): AnalyzerPHPClass;
function getMethodByName(php_class: AnalyzerPHPClass; method_name: String): AnalyzerPHPMethod;
function getAttributeByName(php_class: AnalyzerPHPClass; attribute_name: String): AnalyzerPHPAttribute;
function getDBFilesIndexByFileName(filename: String): Integer;
end;
const
keywords: array[0..10] of string = ('class', 'private', 'public', 'protected', 'const', 'static', 'function', 'include', 'include_once', 'define', 'var');
implementation
uses
cccplugin, ccchelperfunctions, System.StrUtils, ClassesListDockingForms;
//---------------------------------------------------------------------------
// Class: AnalyzerPHPMethod
constructor AnalyzerPHPMethod.Create();
begin
self.parameters := TStringList.Create(true);
end;
//---------------------------------------------------------------------------
destructor AnalyzerPHPMethod.Destroy();
begin
//FreeAndNil(self.parameters);
self.parameters.Free();
self.parameters := nil;
end;
//---------------------------------------------------------------------------
// Class: AnalyzerPHPClass
constructor AnalyzerPHPClass.Create();
begin
self.attributes := System.Generics.Collections.TObjectList<AnalyzerPHPAttribute>.Create(true);
self.methods := System.Generics.Collections.TObjectList<AnalyzerPHPMethod>.Create(true);
end;
//---------------------------------------------------------------------------
destructor AnalyzerPHPClass.Destroy();
begin
self.attributes.Clear();
self.methods.Clear();
self.attributes.Free();
self.methods.Free();
self.attributes := nil;
self.methods := nil;
end;
//---------------------------------------------------------------------------
function AnalyzerPHPClass.countPublicAttributesAndMethods(checkType: Integer): Integer; // checkType: 0 = both, 1 = attributes only, 2 = methods only
var
i: Integer;
begin
Result := 0;
if (checkType = 0) or (checkType = 1) then
begin
// Count public attributes
for i := 0 to self.attributes.Count-1 do
begin
if (self.attributes.Items[i].visibility = asvPublic) then
Inc(Result);
end;
end;
if (checkType = 0) or (checkType = 2) then
begin
// Count public methods
for i := 0 to self.methods.Count-1 do
begin
if (self.methods.Items[i].visibility = asvPublic) then
Inc(Result);
end;
end;
end;
//---------------------------------------------------------------------------
// Class: ClassLinkingListEntry
constructor ClassLinkingListEntry.Create();
begin
self.fileIndices := System.Generics.Collections.TList<Integer>.Create();
end;
//---------------------------------------------------------------------------
destructor ClassLinkingListEntry.Destroy();
begin
self.fileIndices.Clear();
self.fileIndices.Free();
self.fileIndices := nil;
end;
//---------------------------------------------------------------------------
// Class: Ttextpos
constructor Ttextpos.Create(iStart: UINT32; iEnd: UINT32);
begin
self.iStart := iStart;
self.iEnd := iEnd;
end;
//---------------------------------------------------------------------------
// Class: TAnalyzerPHP
constructor TAnalyzerPHP.Create();
begin
self.isAnalyzing := false;
self.classes := System.Generics.Collections.TObjectList<AnalyzerPHPClass>.Create(true);
self.constants := System.Generics.Collections.TObjectList<AnalyzerPHPConstant>.Create(true);
self.include_files := TStringList.Create(true);
self.included_files := TStringList.Create(true);
self.dbfiles := System.Generics.Collections.TObjectList<crcdb>.Create(true);
self.classLinkingList := System.Generics.Collections.TObjectList<ClassLinkingListEntry>.Create(true);
self.iPos := 1;
self.iOldDirection := asdNone;
self.text := nil;
self.RootDirectory := '';
self.LastClass := '';
self.alterText := true;
self.bGetParamValues := false;
self.savePosition();
end;
//---------------------------------------------------------------------------
destructor TAnalyzerPHP.Destroy();
begin
// Delete all classes (this will invoke the destructor of each object (php class) which deletes their attributes & methods)
self.classes.Clear();
self.classes.Free();
self.classes := nil;
// Delete constants
self.constants.Clear();
self.constants.Free();
self.constants := nil;
// Delete stored CRC checksums from files in DB
self.dbfiles.Clear();
self.dbfiles.Free();
self.dbfiles := nil;
// Delete class linking list
self.classLinkingList.Clear();
self.classLinkingList.Free();
self.classLinkingList := nil;
// Delete other objects
self.include_files.Free();
self.include_files := nil;
self.included_files.Free();
self.included_files := nil;
end;
//---------------------------------------------------------------------------
function TAnalyzerPHP.isKeyword(sKeyword: string): Boolean;
var
i: UINT32;
isKeyword: Boolean;
begin
isKeyword := false;
for i := 0 to Length(keywords)-1 do
begin
if
(
(Length(sKeyword) = Length(keywords[i])) and // Is it faster first (& additionally) to check for length before comparing char by char for equality?
(sKeyword = keywords[i])
)
then begin
isKeyword := true;
break; // don't need here?
end;
end;
Result := isKeyword;
end;
//---------------------------------------------------------------------------
function TAnalyzerPHP.getNext(bFindKeywordOnly: Boolean = true): String;
var
returnNext: String;
sWord: String;
iStart: Integer;
iEnd: Integer;
begin
if not Assigned(self.text) then
begin
raise Exception.Create('AnalazerPHP: No text assigned');
end;
returnNext := '';
if
(
(Length(self.text^) > 0) and
(self.iPos > 0) and
(self.iPos <= Length(self.text^) + 1)
)
then begin
iStart := -1;
for iEnd := self.iPos to Length(self.text^) do
begin
if
(
(self.text^[iEnd] = #32) or // space
(self.text^[iEnd] = #9) or // tab
(self.text^[iEnd] = #10) or // line feed \n
(self.text^[iEnd] = #13) or // carriage return \r
(iEnd = Length(self.text^))
)
then begin
if
(
(iStart <> -1) and
(iEnd > iStart)
)
then
begin
sWord := Copy(self.text^, iStart, iEnd - iStart);
// Find keywords only?
if (bFindKeywordOnly) then
begin
// Search for keywords only and found one
if (self.isKeyword(LowerCase(sWord))) then
begin
// If we previously searched back, the internal pointer was positioned before the word.
// If we now start searching from that position, we will find the last searched word again
// (not if previously searched forth). To skip the last word, we need to skip one word.
if
(
(self.iOldDirection = asdRight) or
(self.iOldDirection = asdNone)
)
then begin
self.iOldDirection := asdRight;
break;
end else begin
self.iOldDirection := asdRight;
iStart := -1;
sWord := '';
end;
end
// Search for keywords only but currect word is not a keyword => reset & search further
else
begin
self.iOldDirection := asdRight;
iStart := -1;
sWord := '';
end;
end
// Search for everything? Return found word
else
begin
// If we previously searched back, the internal pointer was positioned before the word.
// If we now start searching from that position, we will find the last searched word again
// (not if previously searched forth). To skip the last word, we need to skip one word.
if
(
(self.iOldDirection = asdRight) or
(self.iOldDirection = asdNone)
)
then begin
self.iOldDirection := asdRight;
break;
end else begin
self.iOldDirection := asdRight;
iStart := -1;
sWord := '';
end;
end;
end;
end
else
begin
if (iStart = -1) then
begin
iStart := iEnd;
end;
end;
end;
self.iPos := iif(iEnd > Length(self.text^), Length(self.text^), iEnd);
returnNext := sWord;
end;
Result := returnNext;
end;
//---------------------------------------------------------------------------
function TAnalyzerPHP.getPrevious(bFindKeywordOnly: Boolean = true): String;
var
returnNext: String;
sWord: String;
iStart: Integer;
iEnd: Integer;
begin
if not Assigned(self.text) then
begin
raise Exception.Create('AnalazerPHP: No text assigned');
end;
returnNext := '';
if
(
(Length(self.text^) > 0) and
(self.iPos > 0) and
(self.iPos <= Length(self.text^) + 1)
)
then begin
// We need to decrement by 1 char to get the previous char, not next
if (self.iPos > 1) then begin
self.iPos := self.iPos - 1;
end;
iStart := -1;
for iEnd := self.iPos downto 1 do
begin
if
(
(self.text^[iEnd] = #32) or // space
(self.text^[iEnd] = #9) or // tab
(self.text^[iEnd] = #10) or // line feed \n
(self.text^[iEnd] = #13) or // carriage return \r
(iEnd = 1)
)
then begin
if
(
(iStart <> -1) and
(iEnd < iStart)
)
then
begin
sWord := Copy(self.text^, iEnd + Integer(iif(iEnd>1,1,0)), iStart - (iEnd + Integer(iif(iEnd>1,1,0))));
// Find keywords only?
if (bFindKeywordOnly) then
begin
// Search for keywords only and found one
if (self.isKeyword(LowerCase(sWord))) then
begin
// If we previously searched forth, the internal pointer was positioned after the word.
// If we now start searching from that position backwards, we will find the last searched word again
// (not if previously searched back). To skip the last word, we need to skip one word.
if
(
(self.iOldDirection = asdLeft) or
(self.iOldDirection = asdNone)
)
then begin
self.iOldDirection := asdLeft;
break;
end else begin
self.iOldDirection := asdLeft;
iStart := -1;
sWord := '';
end;
end
// Search for keywords only but currect word is not a keyword => reset & search further
else
begin
self.iOldDirection := asdLeft;
iStart := -1;
sWord := '';
end;
end
// Search for everything? Return found word
else
begin
// If we previously searched back, the internal pointer was positioned before the word.
// If we now start searching from that position, we will find the last searched word again
// (not if previously searched forth). To skip the last word, we need to skip one word.
if
(
(self.iOldDirection = asdLeft) or
(self.iOldDirection = asdNone)
)
then begin
self.iOldDirection := asdLeft;
break;
end else begin
self.iOldDirection := asdLeft;
iStart := -1;
sWord := '';
end;
end;
end;
end
else
begin
if (iStart = -1) then
begin
iStart := iEnd+1;
end;
end;
end;
self.iPos := iif(iEnd < 1, 1, iEnd);
returnNext := sWord;
end;
Result := returnNext;
end;
//---------------------------------------------------------------------------
procedure TAnalyzerPHP.savePosition();
begin
self._iPos := self.iPos;
self._iOldDirection := self.iOldDirection;
end;
//---------------------------------------------------------------------------
procedure TAnalyzerPHP.restorePosition();
begin
self.iPos := self._iPos;
self.iOldDirection := self._iOldDirection;
end;
//---------------------------------------------------------------------------
procedure TAnalyzerPHP.dump(outputFile: String; mode: Integer);
var
sl: TStringList;
iClass, i, j: Integer;
line: String;
begin
sl := TStringList.Create;
// Dump classes, attributes, methods
if (mode = 1) then
begin
for iClass := 0 to self.classes.Count-1 do
begin
// Add class name and, if available, the name of the class from which derived
line := self.classes.Items[iClass].name;
if Length(self.classes.Items[iClass].derivedFromClass) > 0 then
line := line + ': ' + self.classes.Items[iClass].derivedFromClass;
sl.Add(line);
// Add attributes
for j := 0 to self.classes.Items[iClass].attributes.Count-1 do
begin
case self.classes.Items[iClass].attributes.Items[j].visibility of
asvPublic: line := '+';
asvProtected: line := '#';
asvPrivate: line := '-';
else line := '?'; // no visibility is normally treated as public
end;
line := line + self.classes.Items[iClass].attributes.Items[j].name;
if
(
(self.classes.Items[iClass].attributes.Items[j].bStatic) or
(self.classes.Items[iClass].attributes.Items[j].bConst)
)
then begin
line := line + ' [';
if self.classes.Items[iClass].attributes.Items[j].bStatic then
line := line + 'static,';
if self.classes.Items[iClass].attributes.Items[j].bConst then
line := line + 'const,';
Delete(line, Length(line), 1); // Remove last comma
line := line + ']';
end;
sl.Add(line);
end;
// Add methods
for j := 0 to self.classes.Items[iClass].methods.Count-1 do
begin
case self.classes.Items[iClass].methods.Items[j].visibility of
asvPublic: line := '+';
asvProtected: line := '#';
asvPrivate: line := '-';
else line := '?'; // no visibility is normally treated as public
end;
line := line + self.classes.Items[iClass].methods.Items[j].name;
line := line + ' (' + StringReplace(self.classes.Items[iClass].methods.Items[j].parameters.Text, #13#10, ',', [rfReplaceAll]) + ')';
line := line + ' [' + iif(self.classes.Items[iClass].methods.Items[j].bStatic, 'static', '') + ']';
sl.Add(line);
end;
sl.Add('');
end;
end
// Dump class linking list
else if (mode = 2) then
begin
for i := 0 to self.classLinkingList.Count-1 do
begin
sl.Add(self.classes.Items[self.classLinkingList.Items[i].classIndex].name +' ['+ IntToStr(self.classLinkingList.Items[i].classIndex) +']:');
for j := 0 to self.classLinkingList.Items[i].fileIndices.Count-1 do
begin
if (self.classLinkingList.Items[i].fileIndices.Items[j] <> -1) then
sl.Add(' ' + self.dbfiles.Items[self.classLinkingList.Items[i].fileIndices.Items[j]].sFile + ' [' + IntToStr(self.classLinkingList.Items[i].fileIndices.Items[j]) +']');
end;
end;
end;
sl.SaveToFile(outputFile);
sl.Free;
end;
//---------------------------------------------------------------------------
function TAnalyzerPHP.AnalyzePHP(currentFile: String = ''): Boolean;
var
inValidConstruct: Boolean;
sKeyword: String;
sCheckKeyword: String;
s1, s2, s3, s4: String;
b1, b2: Boolean;
i1, i2, i3: Integer;
php_class: AnalyzerPHPClass;
php_attribute: AnalyzerPHPAttribute;
php_method: AnalyzerPHPMethod;
php_constant: AnalyzerPHPConstant;
begin
// Remove comments, strings, non PHP data, etc.
self.PrepareTextForParsing(true);
// Parse
// First try to go to PHP's valid starting tag
b1 := false;
repeat
sKeyword := self.getNext(false);
if (sKeyword = '<?php') then
begin
b1 := true;
end;
until (b1) or (Length(sKeyword) = 0);
// Starting tag not found -> exit
if (not b1) then
begin
Exit(false);
end;
inValidConstruct := false;
// Note: Don't look for PHP's end tag. PHP/Zend even forbits it! (crazy...)
while (true) do
begin
try
sKeyword := self.getNext();
sCheckKeyword := LowerCase(sKeyword);
// Interfaces (currently not supported)
if (sCheckKeyword = 'interface') then
begin
inValidConstruct := false;
end
// Class name (and parent of derived class)
else if (sCheckKeyword = 'class') then
begin
// Get & create PHP class
s1 := self.getNext(false); // Class name
// Fix problem with class names which contains the code block bracket ("{").
// Perform a global replacement before getting the next word. This is somewhat slow,
// but it should not happen often. Example: "class foo{" => "class foo {".
if s1[Length(s1)] = '{' then
begin
Delete(s1, Length(s1), 1);
self.text^ := StringReplace(self.text^, s1 + '{', s1 + ' {', []); // No case-sensitivity and no global replace. Replace only once. The outer loop will not become infinite with this anymore.
end;
s2 := LowerCase(self.getNext(false)); // bracket or extends
if (s2 = 'implements') then
begin
inValidConstruct := false;
end
else if (s2 = 'extends') then
begin
inValidConstruct := true;
self.LastClass := s1;
// Check if already added
b1 := false;
for i1 := 0 to self.classes.Count-1 do
begin
if (LowerCase(self.classes.Items[i1].name) = LowerCase(s1)) then
begin
b1 := true;
break;
end;
end;
// Not found
if (not b1) then
begin
i2 := self.classes.Add(AnalyzerPHPClass.Create());
php_class := self.classes.Last;
php_class.name := s1;
php_class.derivedFromClass := self.getNext(false); // name of base class
// Create class linking entry (if old file entry not found, getDBFilesIndexByFileName() returns -1 which means the class was defined in current scintilla view)
self.classLinkingList.Add(ClassLinkingListEntry.Create());
self.classLinkingList.Last.classIndex := i2;
i2 := self.getDBFilesIndexByFileName(currentFile);
if (not self.classLinkingList.Last.fileIndices.Contains(i2)) then
self.classLinkingList.Last.fileIndices.Add(i2);
end
// Found
else
begin
// If old class found, kill attributes and methods (because we don't know which of them are still used)
php_class := self.classes.Items[i1];
php_class.name := s1; // Reassign. Actually unneccessary but maybe the case-sensitivity changed..
php_class.derivedFromClass := self.getNext(false);
// Delete all attributes
php_class.attributes.Clear();
// Delete all methods
php_class.methods.Clear();
// Update class linking entry (if old file entry not found, getDBFilesIndexByFileName() returns -1 which means the class was defined in current scintilla view)
i2 := self.getDBFilesIndexByFileName(currentFile);
if (not self.classLinkingList.Items[i1].fileIndices.Contains(i2))
then
self.classLinkingList.Items[i1].fileIndices.Add(i2);
end;
end
else if (s2 = '{') then
begin
inValidConstruct := true;
// Even if already added, save last found class name
self.LastClass := s1;
// Check if already added
b1 := false;
for i1 := 0 to self.classes.Count-1 do
begin
if (LowerCase(self.classes.Items[i1].name) = LowerCase(s1)) then
begin
b1 := true;
break;
end;
end;
// Not found
if (not b1) then
begin
i2 := self.classes.Add(AnalyzerPHPClass.Create());
php_class := self.classes.Last;
php_class.name := s1;
// Create class linking entry (if old file entry not found, getDBFilesIndexByFileName() returns -1 which means the class was defined in current scintilla view)
self.classLinkingList.Add(ClassLinkingListEntry.Create());
self.classLinkingList.Last.classIndex := i2;
i2 := self.getDBFilesIndexByFileName(currentFile);
if (not self.classLinkingList.Last.fileIndices.Contains(i2)) then
self.classLinkingList.Last.fileIndices.Add(i2);
end
// Found
else
begin
// If old class found, kill attributes and methods (because we don't know which of them are still used)
php_class := self.classes.Items[i1];
php_class.name := s1; // Reassign. Actually unneccessary but maybe the case-sensitivity changed..
php_class.derivedFromClass := '';
// Delete all attributes
php_class.attributes.Clear();
// Delete all methods
php_class.methods.Clear();
// Update class linking entry (if old file entry not found, getDBFilesIndexByFileName() returns -1 which means the class was defined in current scintilla view)
i2 := self.getDBFilesIndexByFileName(currentFile);
if (not self.classLinkingList.Items[i1].fileIndices.Contains(i2))
then
self.classLinkingList.Items[i1].fileIndices.Add(i2);
end;
end;
end
// Note: Since PHP is a runtime interpreted language, we can't (and don't need to) collect additional files from include/require statements. Also, we don't need constant definitions.
(*
else if (sCheckKeyword == "include" || sCheckKeyword == "include_once")
{
// s1 may contain only the bracket (exmaple: 'include ( "xx")'). Then find next
do
{
s1 = this->getNext(false);
}
while (Length(s1) > 0 && s1.Pos("\"") == 0 && s1.Pos("'") == 0);
s2 = (s1.Pos("\"") > 0) ? "\"" : "'";
s1 = s1.Delete(1, s1.Pos(s2));
s1 = s1.SubString(1, s1.Pos(s2)-1);
if (Length(s1) > 0)
{
this->include_files.push_back(s1);
}
}
else if (sCheckKeyword == "define")
{
// Get all parameters with brackets
// s1 = all parameters
// s2 = next word
s1 = "";
do
{
s2 = this->getNext(false);
s1 = s1 + s2;
}
while (Length(s2) > 0 && !this->AllBracketsClosed(&s1));
// Remove first & last brackets
s1.Delete(1, s1.Pos("("));
s1 = s1.SubString(1, this->LastChar(&s1, ')')-1);
// Extract multiple parameters
if (s1.Pos(",") > 0)
{
s4 = ""; // constant name
s5 = ""; // constant value
s6 = ""; // handle constant name case-sensitive?
//## Get first parameter (constant name)
s2 = s1.SubString(1, s1.Pos(",")-1).Trim();
s1.Delete(1, s1.Pos(","));
// Remove quotes
s3 = (s2.Pos("\"") > 0) ? "\"" : "'";
s2.Delete(1, s2.Pos(s3));
s4 = s2.SubString(1, this->LastChar(&s2, s3[1])-1).Trim();
//## Get second parameter (constant value)
// Note 1: Value can be string, number, decimal, object, other constant,...
// so just trim first and look if enclosed in brackets, otherwise just take the entire value as-is
// Note 2: There is a third optional parameter for case-sensitivity
if (s1.Pos(",") > 0)
{
s2 = s1.SubString(1, s1.Pos(",")-1).Trim();
s1.Delete(1, s1.Pos(","));
}
else
{
s2 = s1.Trim();
s1 = ""; // set empty for pass third optional parameter
}
if
(
(Length(s2) > 1) && // atleast 2 chars (quotes)
(
(s2[1] == '"') &&
(*s2.LastChar() == '"')
) ||
(
(s2[1] == '\'') &&
(*s2.LastChar() == '\'')
)
)
{
// Remove first and last quotes
s2 = s2.SubString(2, Length(s2)-2);
}
// Note: Don't trim value! It might be a string, where leading & following spaces are intended.
s5 = s2;
//## Optionally: Get third parameter (case-sensivity)
s6 = s1.LowerCase().Trim();
if
(
(Length(s6) > 0) &&
(
(s6 == "true") ||
(s6 == "1")
)
)
{
s6 = "1";
}
else
{
s6 = "0";
}
//## Now create constant
// According to PHP documentation, case-insensitive constant names are lower-cased.
if (s6 == "1")
{
s4 = s4.LowerCase();
}
// Check if constant already got (then overwrite)
b1 = false;
for (i1=0; i1 < this->constants.size(); i1++)
{
if (this->constants[i1]->name == s4)
{
b1 = true;
php_constant = this->constants[i1];
break;
}
}
// Get or create & get constant
if (!b1)
{
this->constants.push_back(new AnalyzerPHPConstant());
php_constant = this->constants.back();
}
// Save all values
php_constant->name = s4;
php_constant->value = s5;
php_constant->case_sensitive = (s6 == "1");
}
}
*)
else if (sCheckKeyword = 'const') then
begin
if inValidConstruct then
begin
// TODO: Constants declared & defined with "const" prefix are supported to be inside classes only. But they are allowed outside them too (since PHP 5.3.0)...
if (self.LastClass <> '') then // This works only if max. one class in file & non-class constants not defined between two classes
begin
s1 := self.getNext(false);
// Get word starting position
i3 := self.iPos - Length(s1);
// Get attribute name only
if (Length(s1) > 0) then
begin
if (s1[1] = '$') then
begin
Delete(s1, 1, 1);
end;
if (Pos('=', s1) > 0) then
begin
s1 := Copy(s1, 1, Pos('=', s1)-1);
end
else if (Pos(';', s1) > 0) then
begin
s1 := Copy(s1, 1, Pos(';', s1)-1);
end;
end;
php_class := self.getLastClass();
php_class.attributes.Add(AnalyzerPHPAttribute.Create());
php_attribute := php_class.attributes.Last;
php_attribute.visibility := asvPublic; // constants in classes are always public (?), old: asvUnknown;
php_attribute.bStatic := false;
php_attribute.bConst := true;
php_attribute.name := s1;
php_attribute.fileOffset := i3;
end;
end;
end
else if
(
(sCheckKeyword = 'var') or
(sCheckKeyword = 'public') or
(sCheckKeyword = 'private') or