Add support for PHP (#40)
diff --git a/README.md b/README.md
index 33fa06c..fd00bb4 100644
--- a/README.md
+++ b/README.md
@@ -59,19 +59,23 @@
#### 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 @@
$ 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 @@
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 @@
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 @@
)
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 @@
import (
"fmt"
"io/ioutil"
+ "math"
"os"
"regexp"
"strings"
@@ -86,10 +87,17 @@
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 @@
" 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