Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow _guess_delimiter to work with a single row of data #208

Merged
merged 6 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@ insert_final_newline = true
[composer.json]
indent_size = 4

[.travis.yml]
[Makefile]
indent_style = tab

[*.yml,*.yaml]
indent_size = 2
38 changes: 38 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
name: CI
on:
push:

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php_version:
- "7.4"
- "7.3"
- "7.2"
- "7.1"
steps:
- uses: actions/checkout@v2
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
- name: Cache composer dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-
- name: Install dependencies
run: composer update
- name: Validate dependencies
run: composer validate
- name: Run tests
run: vendor/bin/phpunit --configuration tests/phpunit.xml
24 changes: 0 additions & 24 deletions .travis.yml

This file was deleted.

18 changes: 17 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,21 @@ phpunit-dep:
exit 1 \
)

# Requires:
# - Docker: https://docker.com
# - act: https://github.com/nektos/act
local-ci:
ifeq (, $(shell which act))
define ACT_ERROR
Consider running the following to install 'act':

curl https://raw.githubusercontent.com/nektos/act/master/install.sh | sudo bash

The dependency 'act' was not found
endef
$(error ${ACT_ERROR})
endif
act -P ubuntu-latest=shivammathur/node:latest -W .github/workflows/ci.yml

.SILENT:
.PHONY: test phpunit-dep
.PHONY: test phpunit-dep local-ci
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@ composer install
composer run test
````

When pushing code to GitHub, tests will be executed using Travis CI. The relevant configuration is in the
file `.travis.yml`.
When pushing code to GitHub, tests will be executed using GitHub Actions. The relevant configuration is in the
file `.github/workflows/ci.yml`. To run the `test` action locally, you can execute the following command:

````bash
make local-ci
````

## Security

Expand Down
6 changes: 3 additions & 3 deletions src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ protected function _check_count($char, $array, $depth, $preferred) {
$first = null;
$equal = null;
$almost = false;
foreach ($array as $key => $value) {
foreach ($array as $value) {
if ($first == null) {
$first = $value;
} elseif ($value == $first && $equal !== false) {
Expand All @@ -1261,7 +1261,7 @@ protected function _check_count($char, $array, $depth, $preferred) {
}
}

if ($equal) {
if ($equal || $depth === 1) {
$match = $almost ? 2 : 1;
$pref = strpos($preferred, $char);
$pref = ($pref !== false) ? str_pad($pref, 3, '0', STR_PAD_LEFT) : '999';
Expand Down Expand Up @@ -1405,7 +1405,7 @@ protected function _guess_delimiter($search_depth, $preferred, $enclosure, $data
$is_newline = ($ch == "\n" && $pch != "\r") || $ch == "\r";
if ($ch == $enclosure) {
if (!$enclosed || $nch != $enclosure) {
$enclosed = $enclosed ? false : true;
$enclosed = !$enclosed;
} elseif ($enclosed) {
$i++;
}
Expand Down
1 change: 1 addition & 0 deletions tests/example_files/single_row.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
C1,C2,C3
9 changes: 9 additions & 0 deletions tests/methods/ParseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ public function testSingleColumn() {
self::assertEquals($expected, $this->csv->data);
}

public function testSingleRow() {
$this->csv->auto(__DIR__ . '/../example_files/single_row.csv');
self::assertEquals([], $this->csv->data, 'Single row is detected as header');
$this->csv->heading = false;
$this->csv->auto(__DIR__ . '/../example_files/single_row.csv');
$expected = [['C1', 'C2', 'C3']];
self::assertEquals($expected, $this->csv->data);
}

public function testMatomoData() {
// Matomo (Piwik) export cannot be read with
$this->csv->use_mb_convert_encoding = true;
Expand Down