Skip to content

Commit

Permalink
Modernization work (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed May 4, 2024
1 parent 3460627 commit bda5f8d
Show file tree
Hide file tree
Showing 66 changed files with 507 additions and 110 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.1', '8.2']
php: ['8.1', '8.2', '8.3']
name: Testing on PHP ${{ matrix.php }}
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -27,4 +27,7 @@ jobs:
run: vendor/bin/phpunit

- name: Run phpstan
run: vendor/bin/phpstan analyze --no-progress --level=5 src/
run: vendor/bin/phpstan

- name: Validate coding standards
run: vendor/bin/phpcs
16 changes: 14 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
FROM php:8-cli
FROM php:8.1-cli

# install dependencies
RUN apt update \
&& apt install -y \
libicu-dev \
git \
zip \
&& pecl install xdebug \
&& docker-php-ext-enable \
xdebug \
&& docker-php-ext-install \
intl \
&& apt-get clean

# install composer
#
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ Here are some code samples, to show how the library is handled.
use Intervention\MimeSniffer\MimeSniffer;
use Intervention\MimeSniffer\Types\ImageJpeg;

// detect given string
// universal factory method
$sniffer = MimeSniffer::create($content);

// or detect given string
$sniffer = MimeSniffer::createFromString($content);

// or detect given file
$sniffer = MimeSniffer::createFromFilename('image.jpg');

// or detect from file pointer
$sniffer = MimeSniffer::createFromFilename(fopen('test.jpg', 'r'));

// returns object of detected type
$type = $sniffer->getType();

Expand Down Expand Up @@ -65,6 +71,9 @@ $type = $sniffer->setFromString($other_content)->getType();

// or with setter for filename
$type = $sniffer->setFromFilename('images/image.jpg')->getType();

// or with setter for file pointer
$type = $sniffer->setFromPointer(fopen('images/image.jpg', 'r'))->getType();
```

**Currently only the following file types can be detected. More will be added in a next release.**
Expand Down
15 changes: 11 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
}
],
"require": {
"php": "^7.3|^8.0"
"php": "^8.1"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^1"
"phpstan/phpstan": "^1",
"squizlabs/php_codesniffer": "^3.8",
"slevomat/coding-standard": "~8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -30,8 +32,13 @@
},
"autoload-dev": {
"psr-4": {
"Intervention\\MimeSniffer\\Test\\": "tests"
"Intervention\\MimeSniffer\\Tests\\": "tests"
}
},
"minimum-stability": "stable"
"minimum-stability": "stable",
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
8 changes: 7 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ services:
analysis:
build: ./
working_dir: /project
command: bash -c "composer install && ./vendor/bin/phpstan analyze --level=4 ./src"
command: bash -c "composer install && ./vendor/bin/phpstan analyze ./src"
volumes:
- ./:/project
standards:
build: ./
working_dir: /project
command: bash -c "composer install && ./vendor/bin/phpcs"
volumes:
- ./:/project
68 changes: 68 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0"?>
<ruleset name="Intervention">
<file>src/</file>
<file>tests/</file>
<arg name="colors"/>
<arg value="p"/>

<rule ref="PSR12"/>
<rule ref="Generic.Arrays.ArrayIndent"/>
<rule ref="Generic.ControlStructures.InlineControlStructure"/>
<rule ref="Generic.Formatting.SpaceAfterCast"/>
<rule ref="Generic.Formatting.SpaceAfterNot">
<properties>
<property name="spacing" value="0" />
</properties>
</rule>
<rule ref="Generic.Metrics.NestingLevel"/>
<rule ref="SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace"/>
<rule ref="SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation"/>
<rule ref="SlevomatCodingStandard.Classes.ClassConstantVisibility"/>
<rule ref="SlevomatCodingStandard.Classes.ClassMemberSpacing"/>
<rule ref="SlevomatCodingStandard.Classes.ConstantSpacing"/>
<rule ref="SlevomatCodingStandard.Classes.MethodSpacing"/>
<rule ref="SlevomatCodingStandard.Classes.ModernClassNameReference"/>
<rule ref="SlevomatCodingStandard.Classes.ParentCallSpacing"/>
<rule ref="SlevomatCodingStandard.Classes.PropertyDeclaration"/>
<rule ref="SlevomatCodingStandard.Classes.RequireSelfReference"/>
<rule ref="SlevomatCodingStandard.Classes.TraitUseDeclaration"/>
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
<properties>
<property name="rootNamespaces" type="array">
<element key="src" value="Intervention\MimeSniffer"/>
<element key="tests" value="Intervention\MimeSniffer\Tests"/>
</property>
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.DisallowGroupUse"/>
<rule ref="SlevomatCodingStandard.Namespaces.MultipleUsesPerLine"/>
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<properties>
<property name="searchAnnotations" value="true" />
</properties>
</rule>
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseSpacing"/>
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias"/>
<rule ref="SlevomatCodingStandard.Strings.DisallowVariableParsing"/>
<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="spacesCountAroundEqualsSign" value="0" />
</properties>
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.LongTypeHints"/>
<rule ref="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue"/>
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing"/>
<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing"/>
<rule ref="SlevomatCodingStandard.TypeHints.UnionTypeHintFormat">
<properties>
<property name="withSpaces" value="no" />
<property name="nullPosition" value="first" />
</properties>
</rule>
<rule ref="SlevomatCodingStandard.PHP.UselessSemicolon"/>
<rule ref="SlevomatCodingStandard.Whitespaces.DuplicateSpaces"/>
<rule ref="SlevomatCodingStandard.Variables.UnusedVariable"/>
<rule ref="SlevomatCodingStandard.Variables.UselessVariable"/>
</ruleset>
9 changes: 9 additions & 0 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
parameters:
level: 6
paths:
- src
exceptions:
check:
missingCheckedExceptionInThrows: true
uncheckedExceptionClasses:
- Error
8 changes: 5 additions & 3 deletions src/AbstractBinaryType.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

namespace Intervention\MimeSniffer;

abstract class AbstractBinaryType extends AbstractType
{
/**
* Prepare content (can be extended by child classes)
*
* @param string $content
* @param string $content
* @return string
*/
public function prepareContent(string $content): string
Expand All @@ -16,9 +18,9 @@ public function prepareContent(string $content): string
}

/**
* Determine of current type is binary
* {@inheritdoc}
*
* @return boolean
* @see TypeInterface::isBinary()
*/
public function isBinary(): bool
{
Expand Down
37 changes: 20 additions & 17 deletions src/AbstractType.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?php

declare(strict_types=1);

namespace Intervention\MimeSniffer;

abstract class AbstractType
use Intervention\MimeSniffer\Interfaces\TypeInterface;

abstract class AbstractType implements TypeInterface
{
/**
* Name of content type (mime type)
Expand All @@ -19,50 +23,49 @@ abstract class AbstractType
protected $pattern = "/^$/";

/**
* Determine if the given content matches the signature
* {@inheritdoc}
*
* @param string $content
* @return bool
* @see TypeInterface::matches()
*/
public function matches(string $content): bool
{
return preg_match($this->pattern, $this->prepareContent($content)) === 1;
}

/**
* Determine if the detected type is an image
* {@inheritdoc}
*
* @return boolean
* @see TypeInterface::isImage()
*/
public function isImage(): bool
{
return substr($this->name, 0, 5) === 'image';
}

/**
* Determine if the detected type is an video
* {@inheritdoc}
*
* @return boolean
* @see TypeInterface::isVideo()
*/
public function isVideo(): bool
{
return substr($this->name, 0, 5) === 'video';
}

/**
* Determine if the detected type is an audio file
* {@inheritdoc}
*
* @return boolean
* @see TypeInterface::isAudio()
*/
public function isAudio(): bool
{
return substr($this->name, 0, 5) === 'audio';
}

/**
* Determine if the detected type is an archive
* {@inheritdoc}
*
* @return boolean
* @see TypeInterface::isArchive()
*/
public function isArchive(): bool
{
Expand All @@ -77,7 +80,7 @@ public function isArchive(): bool
/**
* Prepare content (can be extended by child classes)
*
* @param string $content
* @param string $content
* @return string
*/
public function prepareContent(string $content): string
Expand All @@ -86,19 +89,19 @@ public function prepareContent(string $content): string
}

/**
* Determine of current type is binary
* {@inheritdoc}
*
* @return boolean
* @see TypeInterface::isBinary()
*/
public function isBinary(): bool
{
return false;
}

/**
* Cast type to string
* {@inheritdoc}
*
* @return string
* @see TypeInterface::__toString()
*/
public function __toString(): string
{
Expand Down
3 changes: 2 additions & 1 deletion src/Exceptions/NotMatchingException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php

declare(strict_types=1);

namespace Intervention\MimeSniffer\Exceptions;

use RuntimeException;

class NotMatchingException extends RuntimeException
{
# nothing to override
}

0 comments on commit bda5f8d

Please sign in to comment.