You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@skywalking.apache.org by ke...@apache.org on 2021/07/13 12:40:34 UTC

[skywalking-eyes] branch main updated: Add support for PHP (#40)

This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/skywalking-eyes.git


The following commit(s) were added to refs/heads/main by this push:
     new 1b91757  Add support for PHP (#40)
1b91757 is described below

commit 1b91757cf0c5d0ea55a68c803dcd11a0189b7e83
Author: emschu <em...@mailbox.org>
AuthorDate: Tue Jul 13 14:40:27 2021 +0200

    Add support for PHP (#40)
---
 README.md                                          | 14 ++--
 assets/languages.yaml                              |  2 +
 assets/styles.yaml                                 |  8 ++
 pkg/comments/config.go                             | 12 +--
 pkg/header/fix.go                                  | 10 ++-
 pkg/header/fix_test.go                             | 98 ++++++++++++++++++++++
 .../include_test/with_license/testcase.php         | 22 +++++
 .../include_test/without_license/testcase.php      |  1 +
 8 files changed, 156 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md
index 33fa06c..fd00bb4 100644
--- a/README.md
+++ b/README.md
@@ -59,19 +59,23 @@ $ make build
 #### Check License Header
 
 ```bash
-$ bin/darwin/license-eye -c test/testdata/.licenserc_for_test_fix.yaml header check
+$ bin/darwin/license-eye -c test/testdata/.licenserc_for_test_check.yaml header check
 
-INFO Loading configuration from file: test/testdata/.licenserc_for_test.yaml
-INFO Totally checked 23 files, valid: 8, invalid: 8, ignored: 7, fixed: 0
-ERROR The following files don't have a valid license header:
+INFO Loading configuration from file: test/testdata/.licenserc_for_test_check.yaml 
+INFO Totally checked 30 files, valid: 12, invalid: 12, ignored: 6, fixed: 0 
+ERROR the following files don't have a valid license header: 
 test/testdata/include_test/without_license/testcase.go
 test/testdata/include_test/without_license/testcase.graphql
+test/testdata/include_test/without_license/testcase.ini
 test/testdata/include_test/without_license/testcase.java
 test/testdata/include_test/without_license/testcase.md
+test/testdata/include_test/without_license/testcase.php
 test/testdata/include_test/without_license/testcase.py
 test/testdata/include_test/without_license/testcase.sh
 test/testdata/include_test/without_license/testcase.yaml
 test/testdata/include_test/without_license/testcase.yml
+test/testdata/test-spdx-asf.yaml
+test/testdata/test-spdx.yaml 
 exit status 1
 ```
 
@@ -81,7 +85,7 @@ exit status 1
 $ bin/darwin/license-eye -c test/testdata/.licenserc_for_test_fix.yaml header fix
 
 INFO Loading configuration from file: test/testdata/.licenserc_for_test_fix.yaml
-INFO Totally checked 16 files, valid: 7, invalid: 8, ignored: 1, fixed: 8
+INFO Totally checked 20 files, valid: 10, invalid: 10, ignored: 0, fixed: 10 
 ```
 
 #### Resolve Dependencies' licenses
diff --git a/assets/languages.yaml b/assets/languages.yaml
index 0804a62..46035cd 100644
--- a/assets/languages.yaml
+++ b/assets/languages.yaml
@@ -2155,6 +2155,7 @@ HTML+PHP:
   codemirror_mode: php
   codemirror_mime_type: application/x-httpd-php
   language_id: 151
+  comment_style_id: PhpTag
 HTML+Razor:
   type: markup
   tm_scope: text.html.cshtml
@@ -3973,6 +3974,7 @@ PHP:
   aliases:
     - inc
   language_id: 272
+  comment_style_id: PhpTag
 PLSQL:
   type: programming
   ace_mode: sql
diff --git a/assets/styles.yaml b/assets/styles.yaml
index 9d70bba..2109470 100644
--- a/assets/styles.yaml
+++ b/assets/styles.yaml
@@ -89,3 +89,11 @@
   start: '{*'
   middle: ~
   end: '*}'
+
+- id: PhpTag
+  after: '(?mi)^<\?(php)?(\s+declare\s*\(\s*strict_types\s*=\s*[01]{1}\s*\)\s*;)?$'
+  start: '/*'
+  middle: ' *'
+  end: ' */'
+  ensure_after: '<?php'
+  ensure_before: '?>'
\ No newline at end of file
diff --git a/pkg/comments/config.go b/pkg/comments/config.go
index b323851..7125c68 100644
--- a/pkg/comments/config.go
+++ b/pkg/comments/config.go
@@ -27,11 +27,13 @@ import (
 )
 
 type CommentStyle struct {
-	ID     string `yaml:"id"`
-	After  string `yaml:"after"`
-	Start  string `yaml:"start"`
-	Middle string `yaml:"middle"`
-	End    string `yaml:"end"`
+	ID           string `yaml:"id"`
+	After        string `yaml:"after"`
+	Start        string `yaml:"start"`
+	Middle       string `yaml:"middle"`
+	End          string `yaml:"end"`
+	EnsureAfter  string `yaml:"ensure_after"`
+	EnsureBefore string `yaml:"ensure_before"`
 }
 
 func (style *CommentStyle) Validate() error {
diff --git a/pkg/header/fix.go b/pkg/header/fix.go
index 7e6d776..2cccf28 100644
--- a/pkg/header/fix.go
+++ b/pkg/header/fix.go
@@ -21,6 +21,7 @@ package header
 import (
 	"fmt"
 	"io/ioutil"
+	"math"
 	"os"
 	"regexp"
 	"strings"
@@ -86,10 +87,17 @@ func rewriteContent(style *comments.CommentStyle, content []byte, licenseHeader
 	afterPattern := regexp.MustCompile(style.After)
 	location := afterPattern.FindIndex(content)
 	if location == nil || len(location) != 2 {
+		if style.EnsureAfter != "" {
+			return append([]byte(style.EnsureAfter+"\n"+licenseHeader+style.EnsureBefore), content...)
+		}
 		return append([]byte(licenseHeader), content...)
 	}
+
+	// if files do not have an empty line at the end, the content slice index given
+	//  at index location[1]+1 could be out of range
+	startIdx := math.Min(float64(location[1]+1), float64(len(content)))
 	return append(content[0:location[1]],
-		append(append([]byte("\n"), []byte(licenseHeader)...), content[location[1]+1:]...)...,
+		append(append([]byte("\n"), []byte(licenseHeader)...), content[int64(startIdx):]...)...,
 	)
 }
 
diff --git a/pkg/header/fix_test.go b/pkg/header/fix_test.go
index dc6bfe0..5ab9503 100644
--- a/pkg/header/fix_test.go
+++ b/pkg/header/fix_test.go
@@ -201,6 +201,104 @@ import Foundation.Hashing.Hashable`},
 " Apache License 2.0
 echo 'Hello' | echo 'world!'
 `},
+		{
+			name:          "Php-1",
+			style:         comments.FileCommentStyle("test.php"),
+			content:       ``,
+			licenseHeader: getLicenseHeader("test.php", t.Error),
+			expectedContent: `<?php
+/*
+ * Apache License 2.0
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * Apache License 2.0
+ */
+?>`,
+		}, {
+			name:  "Php-2",
+			style: comments.FileCommentStyle("test.php"),
+			content: `<?php declare(strict_types=1);
+echo "Test";
+`,
+			licenseHeader: getLicenseHeader("test.php", t.Error),
+			expectedContent: `<?php declare(strict_types=1);
+/*
+ * Apache License 2.0
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * Apache License 2.0
+ */
+echo "Test";
+`,
+		}, {
+			name:  "Php-3",
+			style: comments.FileCommentStyle("test.php"),
+			content: `<?php
+echo "Test";
+`,
+			licenseHeader: getLicenseHeader("test.php", t.Error),
+			expectedContent: `<?php
+/*
+ * Apache License 2.0
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * Apache License 2.0
+ */
+echo "Test";
+`,
+		}, {
+			name:  "Php-4",
+			style: comments.FileCommentStyle("test.php"),
+			content: `<?php
+`,
+			licenseHeader: getLicenseHeader("test.php", t.Error),
+			expectedContent: `<?php
+/*
+ * Apache License 2.0
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * Apache License 2.0
+ */
+`,
+		}, {
+			name:  "Php-5",
+			style: comments.FileCommentStyle("test.php"),
+			content: `<?php
+/**
+ * This is a php docblock
+ */
+namespace test\test2;
+`,
+			licenseHeader: getLicenseHeader("test.php", t.Error),
+			expectedContent: `<?php
+/*
+ * Apache License 2.0
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * Apache License 2.0
+ */
+/**
+ * This is a php docblock
+ */
+namespace test\test2;
+`,
+		}, {
+			name:  "Php-6",
+			style: comments.FileCommentStyle("test.php"),
+			content: `<?
+/**
+ * This is a php docblock
+ */
+namespace test\test2;
+`,
+			licenseHeader: getLicenseHeader("test.php", t.Error),
+			expectedContent: `<?
+/*
+ * Apache License 2.0
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * Apache License 2.0
+ */
+/**
+ * This is a php docblock
+ */
+namespace test\test2;
+`,
+		},
 	}
 	for _, test := range tests {
 		t.Run(test.name, func(t *testing.T) {
diff --git a/test/testdata/include_test/with_license/testcase.php b/test/testdata/include_test/with_license/testcase.php
new file mode 100644
index 0000000..4b39ab5
--- /dev/null
+++ b/test/testdata/include_test/with_license/testcase.php
@@ -0,0 +1,22 @@
+<?php declare(strict_types=1);
+/*
+ * Licensed to Apache Software Foundation
+ * (ASF) under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Apache Software Foundation
+ * (ASF) licenses this file to you under
+ * the Apache License, Version 2.0
+ * (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.       See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
\ No newline at end of file
diff --git a/test/testdata/include_test/without_license/testcase.php b/test/testdata/include_test/without_license/testcase.php
new file mode 100644
index 0000000..a814366
--- /dev/null
+++ b/test/testdata/include_test/without_license/testcase.php
@@ -0,0 +1 @@
+<?php
\ No newline at end of file